3

I have a pretty simple requirement:

If vehicleType IS NOT NULL, AND vehicleType IS CAR then topSpeed is required, else, topSpeed is not required.

Currently, if vehicleType (which is optional), is not provided, must have required property 'topSpeed' error is happening.

Any ideas what is wrong?

Here is a snippet of the JSON Schema:

"vehicle": {
    "type": "object",
    "properties": {
      "valuation": {
        "type": "number"
      },
      "vehicleType": {
        "type": "string",
        "enum": ["Car", "Airplane", "Boat", "Motorcycle", "Truck"]
      },
      "topSpeed": {
        "type": "number"
      }
    },

    "if": {
      "properties": {
        "vehicleType": {
          "const": "Car"
        }
      }
    },
    "then": {
      "required": ["topSpeed"]
    }
  }

1 Answer 1

4

The if schema is true because vehicleType is not required. Therefore the then branch is applied even if vehicleType is not present. Add "required": [ "vehicleType" ] to the if schema.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.