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 haveitems. - Within those properties you're defining
nameas an attribute that then holds other json schema objects. - In the second branch of your schema you defined
otherProperty3but in your sample that property is calledanotherProperty3
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" ] }
}
}
]
}
}
}
}
Answer from user1270191 on Stack OverflowLearnjsonschema
learnjsonschema.com › 2020-12 › applicator › anyof
anyOf (2020-12)
For example: ... As a reference, ... at least one of the given properties Schema ... { "$schema": "https://json-schema.org/draft/2020-12/schema", "anyOf": [ { "required": [ "foo" ] }, { "required": [ "bar" ] } ] }...
Videos
JSON Schema
json-schema.org › understanding-json-schema › reference › combining
JSON Schema - Boolean JSON Schema combination
Careful consideration should be ... can lead to increased processing times. Prefer anyOf where possible. The not keyword declares that an instance validates if it doesn't validate against the given subschema. For example, the following schema validates against anything that ...
Liquid Technologies
liquid-technologies.com › Reference › XmlStudio › JsonEditorNotation_XXXOf.html
AllOf, AnyOf, OneOf
AllOf : All of the contained schemas must validate against the instance value. AnyOf : One or more of the contained schemas must validate against the instance value.
Rjsf-team
rjsf-team.github.io › json schema › oneof, anyof, and allof
oneOf, anyOf, and allOf | react-jsonschema-form - GitHub Pages
import { RJSFSchema } from '@rjsf/utils'; import validator from '@rjsf/validator-ajv8'; const schema: RJSFSchema = { type: 'object', anyOf: [ { properties: { lorem: { type: 'string', }, }, required: ['lorem'], }, { properties: { lorem: { type: 'string', }, ipsum: { type: 'string', }, }, }, ], }; render(<Form schema={schema} validator={validator} />, document.getElementById('app')); 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. For example, the below schema evaluates to a combined subschema of {type: "boolean"}:
JSON Schema
json-schema.org › understanding-json-schema › reference › conditionals
JSON Schema - Conditional schema validation
{ "type": "object", "properties": ... "number" }, "tip": { "type": "number" } }, "anyOf": [ { "not": { "properties": { "restaurantType": { "const": "sit-down" } }, "required": ["restaurantType"] } }, { "required": ["tip"] } ]}...
Swagger
swagger.io › docs › specification › v3_0 › data-models › oneof-anyof-allof-not
oneOf, anyOf, allOf, not | Swagger Docs
It requires user to specify which ... inline or referenced schema must be a schema object, not a standard JSON schema. For that example, all of the following request bodies are valid: ... Use the anyOf keyword to validate the data against any amount of the given subsc...
Rjsf-team
rjsf-team.github.io › json schema › oneof, anyof, and allof
oneOf, anyOf, and allOf | react-jsonschema-form
import { RJSFSchema } from '@rjsf/utils'; import validator from '@rjsf/validator-ajv8'; const schema: RJSFSchema = { type: 'object', anyOf: [ { properties: { lorem: { type: 'string', }, }, required: ['lorem'], }, { properties: { lorem: { type: 'string', }, ipsum: { type: 'string', }, }, }, ], }; render(<Form schema={schema} validator={validator} />, document.getElementById('app')); When allOf is specified in a schema, react-jsonschema-form uses the @x0k/json-schema-merge library to merge the specified subschemas to create a combined subschema that is valid. For example, the below schema evaluates to a combined subschema of {type: "boolean"}:
JSON Schema
json-schema.org › blog › posts › applicability-json-schema-fundamentals-part-1
It all starts with applicability - JSON Schema Fundamentals part 1
March 20, 2022 - Our two truth tables above represent our allOf and anyOf keyword's boolean logic. A, B, and C represent the three subschemas from our earlier example and all possible combinations of their assertion results.
Opis
opis.io › json-schema › 2.x › multiple-subschemas.html
Applying multiple subschemas | Opis JSON Schema
{ "type": "string", "anyOf": [ {"const": 0}, {"const": 1} ] } ... An instance is valid against this keyword if is valid against exactly one schema defined by the value of this keyword. The value of this keyword must be an array of valid JSON schemas (objects or booleans).
Riverbed
support.riverbed.com › apis › steelscript › reschema › jsonschema.html
14.1.1.5. JSON Schema primer — steelscript documentation
Example with a base type of a number and using anyOf for validation: type: number description: 'Number 1-10 or 50-100' anyOf: - type: number minimum: 1 maximum: 10 - type: number minimum: 50 maximum: 100 · The allOf keyword is similar to anyOf except that an instance is valid if and only if ...
Linux find Examples
queirozf.com › entries › json-schema-examples
Json Schema Examples
August 17, 2016 - { "$schema": "http://json-schema.org/draft-04/schema#", "description": "Person object", "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "number" } }, "required":["name","age"] } ... "oneOf" can be used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. For example, if you need multiple required rules, you could do this:
The New Stack
thenewstack.io › home › json schema keywords allof and anyof in opa type checker
JSON Schema Keywords AllOf and AnyOf in OPA Type Checker - The New Stack
November 14, 2023 - For those who are familiar with JSON Schema language, it might look something like this: In other words, after entering your address line, you’re prompted to enter either (state AND city AND ZIP code) or (country AND postcode) (technically, you can enter both options under “anyOf” since it’s an inclusive “or”).
Cswr
cswr.github.io › JsonSchema › spec › generic_keywords
Generic Keywords - JSON Schema
For instance, if we were building an application that can accept either strings or integers we could use the following schema to check if the document is of the correct format. { "anyOf": [ { "type": "string" }, { "type": "integer" } ] } In this case JSON document "This is a string" satisfies ...
Learnjsonschema
learnjsonschema.com › 2020-12 › applicator › oneof
oneOf (2020-12) - Learn JSON Schema
In practice, the vast majority of schemas don’t require exclusive disjunction semantics but a simple disjunction. If you are not sure, the anyOf keyword is probably a better fit. Avoid this keyword unless you absolutely need exclusive disjunction semantics, which is rarely the case. As its name implies, this keyword enforces the instance to be valid against only one of its subschemas. Therefore, a JSON ...
GitHub
github.com › rjsf-team › react-jsonschema-form › issues › 3834
How to use anyOf with custom fields · Issue #3834 · rjsf-team/react-jsonschema-form
August 18, 2023 - Notice how my customField is named ... it to check "phoneCustomField>phone"? In example, the user must provide either a phone number OR an email in order to pass validation....
Author burks10
Cswr
cswr.github.io › JsonSchema › spec › multiple_types
Multiple Types - JSON Schema
{ "anyOf": [ { "type": "integer", "minimum": 2 }, { "type": "string", "minLength": 4 } ] } In general, when multiple (or no) types are defined in the schema, the rule for restrictions is as follows: each keyword restriction only applies when validating documents that are of the type that is ...
Jsonforms
jsonforms.io › examples › combinators
Combinators Example - JSON Forms
"anyOf": [ { "$ref": "#/definitions/address" }, { "$ref": "#/definitions/user" } ] } } } uischema.json · { "type": "VerticalLayout", "elements": [ { "type": "Control", "label": "Basic Information", "scope": "#/properties/addressOrUser" } ] } { "addressOrUser": { "street_address": "1600 ...