i have an object in which the "key" of the property will be set dynamically... what is the right way of defining this in a JSON Schema?
This is what my object looks like
{
"column_definitions": [
{
"Field_1": {
"type": "Numeric",
"isNullable": false
}
},
{
"Field_2": {
"type": "Boolean",
"isNullable": true
}
}
],
"row_values": [ ... ]
}
The "key" of the "column_definitions" will always be dynamic (it can be "Field_1" just as much as it can be "Field_24"
What is the proper to define this in JSON Schema?
I dont want to just say "type" : "object" because i want to be able to define the static properties "type" and "isNullable" Also, i cant use "oneOf" simply because i dont know what the "key" can potentially be and there is not a set potential values.
This is what i have so far:
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "SomeSchema",
"description": "SomeDescription",
"type": "object",
"properties":
{
"column_definitions": { "type": ["array", "null"], "items": { "$ref": "#/definitions/columnDef" }, "readOnly": true },
"row_values": { "type": ["array", "null"], "items": { "type": "object" }, "readOnly": true }
},
"definitions": {
"columnDef" : {
"type": "object",
"properties": {
"THIS_IS_MY_DYNAMIC_PROPERTY": {
"type": "object",
"properties": {
"type": { "type" : ["string", "null"], "enum": ["Text", "Boolean", "Numeric", "DateTime"], "readOnly": true },
"isNullable": { "type" : ["boolean", "null"], "readOnly": true }
}
}
}
}
}
}
Properties. The properties (key-value pairs) on an object are defined using the properties keyword. The value of properties is an object, where each key is the name of a property and each value is a schema used to validate that property.
In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.
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.
Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.
I think what you are looking for is the patternProperties
field, rather than the properties
one. Should look something like this, assuming you just want a match all pattern:
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "SomeSchema",
"description": "SomeDescription",
"type": "object",
"properties": {
"column_definitions": {
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/columnDef"
},
"readOnly": true
},
"row_values": {
"type": [
"array",
"null"
],
"items": {
"type": "object"
},
"readOnly": true
}
},
"definitions": {
"columnDef": {
"type": "object",
"patternProperties": {
".*": {
"type": "object",
"properties": {
"type": {
"type": [
"string",
"null"
],
"enum": [
"Text",
"Boolean",
"Numeric",
"DateTime"
],
"readOnly": true
},
"isNullable": {
"type": [
"boolean",
"null"
],
"readOnly": true
}
}
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With