Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON schema anyOf validation based on one of properties

I'm having difficulty figuring out how to validate an array of objects based on the value of one of the properties. So where I have a JSON object like:

{
    "items": [
        {
            "name": "foo",
            "otherProperty": "bar"
        },
        {
            "name": "foo2",
            "otherProperty2": "baz",
            "otherProperty3": "baz2"
        },
        {
            "name": "imInvalid"
        }
    ]
}

I would like to say that

  1. items can contain anyOf objects where name can be "foo" or "foo2"
  2. if it's "foo" then the only valid other property (required) is "otherProperty"
  3. if the name is "foo2" then the only valid other properties are "otherProperty2" and "otherProperty3" both required
  4. No other value for "name" than "foo" and "foo2" is valid
  5. The objects themselves are optional in the items array, and some might be repeated.

I've tried all kinds of things but I can't seem to get a failure when I validate. For example, the name "imInvalid" should be causing a validation error. This is my latest iteration of the schema. What am I missing?

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": ["items"],
    "properties": {
        "items": {
            "type": "array",
            "minItems": 1,
            "additionalProperties": false,
            "properties": {
                "name": {
                    "anyOf": [
                        {
                            "type": "object",
                            "required": ["name", "otherProperty"],
                            "additionalProperties": false,
                            "properties": {
                                "otherProperty": { "type": "string" },
                                "name": { "enum": [ "foo" ] }
                            }
                        },{
                            "type": "object",
                            "required": ["name", "otherProperty2", "otherProperty3" ],
                            "additionalProperties": false,
                            "properties": {
                                "otherProperty2": { "type": "string" },
                                "otherProperty3": { "type": "string" },
                                "name": { "enum": [ "foo2" ] }
                            }
                        }
                    ]
                }
            }
        }
    }
}
like image 663
Fo. Avatar asked Apr 14 '16 17:04

Fo.


People also ask

How does JSON Schema validate JSON data?

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.

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.

Can we validate JSON with schema?

JSON Schema is a powerful tool. It enables you to validate your JSON structure and make sure it meets the required API. You can create a schema as complex and nested as you need, all you need are the requirements. You can add it to your code as an additional test or in run-time.

What is additional properties in JSON Schema?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

What is validating data with JSON-Schema?

This post is part of a series called Validating Data With JSON-Schema. When you’re dealing with complex and structured data, you need to determine whether the data is valid or not. JSON-Schema is the standard of JSON documents that describes the structure and the requirements of your JSON data.

Is ONEOF JSON valid against more than one schema?

Message: JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 0, 1. Schema path: #/properties/num/oneOf I'm assuming I'm missing something obvious about how oneOf works, but I can't figure out what it might me. Would appreciate any help here, thanks!

What are the properties of a JSON Schema array?

Json schema arrays don't have properties, they have items. Within that properties you're defining name as an attribute that then holds other json schema objects. In the second branch of you're schema you defined otherProperty3 but in your sample that property is called anotherProperty3.

What is anyof and allof in react-JSONSchema-form?

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. When allOf is specified in a schema, react-jsonschema-form uses the json-schema-merge-allof library to merge the specified subschemas to create a combined subschema that is valid.


1 Answers

You've got the basic idea of using enum to separate what's matching, but there are a couple of mistakes here:

  • Json schema arrays don't have properties, they have items.
  • Within those properties you're defining name as an attribute that then holds other json schema objects.
  • In the second branch of your schema you defined otherProperty3 but in your sample that property is called anotherProperty3

Try this slightly modified version:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["items"],
"properties": {
    "items": {
        "type": "array",
        "minItems": 1,
        "additionalProperties": false,
        "items": {
            "anyOf": [
                {
                    "type": "object",
                    "required": ["name", "otherProperty"],
                    "additionalProperties": false,
                    "properties": {
                        "otherProperty": { "type": "string" },
                        "name": { "enum": [ "foo" ] }
                    }
                },{
                    "type": "object",
                    "required": ["name", "otherProperty2", "anotherProperty3" ],
                    "additionalProperties": false,
                    "properties": {
                        "otherProperty2": { "type": "string" },
                        "anotherProperty3": { "type": "string" },
                        "name": { "enum": [ "foo2" ] }
                    }
                }
            ]
        }
    }
}
}
like image 146
user1270191 Avatar answered Sep 29 '22 15:09

user1270191