0

I wanted a dependency schema using mongodb loopback. My Post requests:

REQUEST TYPE 1

{
    “url”: “google.com”
    “type”: “redirect”
}

REQUEST TYPE 2

{
    “url”: “yahoo.com”
    “type”: “anchor”
}

My model function looks like

 @property({
    type: 'string',
    required: true,
  })
  type: string;
        
  @property({
    type: 'string',
    required: true,
    jsonSchema: {
      maxLength: 2000,
      pattern: '^[\\]*$', //using some regex
    },
  })
  url: string;

My problem is I want this url pattern to be checked only if type in the payload is redirect. I read about json schema dependencies. But it is not working. Nor giving any error. I am trying something like:

@property({
    type: 'string',
    required: true,
    jsonSchema: {
    maxLength: 2000,
    "dependencies": {
    if(type == “redirect){
       pattern: '^[\\]*$', //using some regex }
     }
    },
  })
  url: string;

1 Answers1

0

You can use conditions in jsonSchema using dependancies.

Link : https://react-jsonschema-form.readthedocs.io/en/latest/usage/dependencies/

In your case it seems you can use oneOf.

Your schema can look like:

@property({
  type: 'string',
  required: true,
  oneOf: [
    {pattern: 'your 1st pattern'},
    {pattern: 'your 2nd pattern'},
  ],
  jsonSchema: {
    maxLength: 2000,
  },
})
url: string;

This schema with oneOf is valid if exactly one of the subschemas is valid.

Dhruv Thakkar
  • 415
  • 1
  • 6
  • 18