Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the `If` `then` `else` condition in json schema?

Tags:

jsonschema

A relatively new addition to JSON Schema (draft-07) adds the if, then and else keywords. I cannot work out how to use these new key words correctly. Here is my JSON Schema so far:

{
  "type": "object",
  "properties": {
    "foo": {
      "type": "string"
    },
    "bar": {
      "type": "string"
    }
  },
  "if": {
    "properties": {
      "foo": {
        "enum": [
          "bar"
        ]
      }
    }
  },
  "then": {
    "required": [
      "bar"
    ]
  }
}

If the "foo" property equals "bar", Then the "bar" property is required. This works as expected.

However, if the "foo" property does not exist or input is empty then I don't want anything. how to acheive this?

empty input {}.

Found Errors:
Required properties are missing from object: bar. Schema path: #/then/required

I use online validation tool:

https://www.jsonschemavalidator.net/

like image 463
Mohammad Irfan Avatar asked Jul 26 '18 13:07

Mohammad Irfan


People also ask

What is a JSON Subschema?

JSON Schema is a language for declaring the structure of valid JSON data. There are validators that can decide whether a specific JSON document is valid with respect to a schema.

What is anyOf in JSON Schema?

react-jsonschema-form supports custom widgets for oneOf, anyOf, and allOf. A schema with oneOf is valid if exactly one of the subschemas is valid. A schema with anyOf is valid if at least one of the subschemas is valid. A schema with allOf is valid if all of the subschemas are valid.

How does JSON Schema match?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

Does JSON have schema validation?

JSON Schema Validation: The JSON Schema Validation specification is the document that defines the valid ways to define validation constraints. This document also defines a set of keywords that can be used to specify validations for a JSON API.


1 Answers

The if keyword means that, if the result of the value schema passes validation, apply the then schema, otherwise apply the else schema.

Your schema didn't work because you needed to require "foo" in your if schema, otherwise an empty JSON instance would pass validation of the if schema, and therefore apply the then schema, which requires "bar".

Second, you want "propertyNames":false, which prevents having any keys in the schema, unlike if you were to set "else": false which would make anything always fail validation.

{
  "type": "object",
  "properties": {
    "foo": {
      "type": "string"
    },
    "bar": {
      "type": "string"
    }
  },
  "if": {
    "properties": {
      "foo": {
        "enum": [
          "bar"
        ]
      }
    },
    "required": [
      "foo"
    ]
  },
  "then": {
    "required": [
      "bar"
    ]
  },
  "else": false
}
like image 189
Relequestual Avatar answered Oct 23 '22 18:10

Relequestual