Allow any string for property definition Json Schema - jsonschema

I'm trying to use jsonschema to validate some simple json. I have the following:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"outputs": {
"type": "object",
"properties": {
"ANYSTRING": {
"type": "string",
"properties": {
"label": { "type": "string" },
"otherLabel": { "type": "string" }
}
}
}
}
}
}
Essentially, I want any of the following to be valid:
{
"outputs": {
"this is a sample string": { "label": "test" },
"another string": { },
"and one last one": { "otherLabel": "dummy" }
}
}
(How) Can I have this wildcard string property denoted above as "ANYSTRING"?

I suppose you could use additionalProperties. Side note: a thing of type “string” can’t have properties, but “object” can.
"outputs": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"label": { "type": "string" }
}
}
}
Shawn Silverman (JsonSchema Slack channel)

Related

Referring inner properties in the IF condition - JSON Schema

The following is a sample schema to depict the issue
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"person": {
"type": "object",
"properties": {
"age": {
"type": "string"
}
}
}
},
"properties": {
"child": {
"$ref": "#/definitions/person"
}
},
"required": [
"child"
],
"if": {
"properties": {
"person/age": {
"const": "3"
}
}
},
"then": {
"properties": {
"guardian": {
"$ref": "#/definitions/person"
}
},
"required": [
"guardian"
]
}
}
Is there a way to refer age inside the person object?
{"child":{"age":"3"}}. Should fail as guardian tag is missing
The above data should fail as the guardian object is missing.
Remember that if is just a regular schema validating against the instance. Just nest your properties like you would with any nested object structure.
{
"type": "object",
"properties": {
"child": {
"type": "object",
"properties": {
"age": { "const": "3" }
},
"required": ["age"]
}
},
"required": ["child"]
}
Note that the type and required keywords are necessary to not inadvertently trigger the then schema. For example, with out them, these would cause the then to trigger when you probably didn't want it to.
{}
{ "child": null }
{ "child": {} }

jsonschema with custom property

I want to define a JsonSchema with a customProperty in it, this property follows some rules, so in order to validate those, I need to define a JsonSchema that will validate it.
So far I've managed to describe it properly, but it only works for the first level of attributes, and I want it to be recursive...
From my comprehension it should work, I probably made a mistake I'm unable to see and at this point I don't know if it's a bug, impossible, or stupid...
I believe redefining every type should be a possibility , but obviously I'd rather not.
Here is an example of the Json I want to validate
{
"title": "TheObject",
"type": "object",
"properties": {
"aString": {
"type": "string",
"myCustomProperty": {}
},
"anObjet": {
"type": "object",
"myCustomProperty": {},
"properties": {
"anotherObject": {
"type": "object",
"myCustomProperty": {}, //if this line is removed it still validates which I don't want
"properties": {}
}
}
}
}
}
and here is the JsonSchema I've done so far:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"title": {"type": "string"},
"type": {"type": "string","enum": ["object"]},
"properties": {
"type": "object",
"patternProperties": {
".*": {
"$ref": "#/definitions/Field"
}
}
}
},
"definitions": {
"Field": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"myCustomProperty": {
"$ref": "#/definitions/myCustomProperty"
},
"patternProperties": {
"^(?!myCustomProperty).*": {
"$ref": "#/definitions/Field"
}
}
},
"required": [
"type",
"myCustomProperty"
]
},
"myCustomProperty": {
//Some rules
}
}
}
I found a solution, I wasn't far from what I wanted after all.
In my definition of "Field", I am describing an object that defines an object, and I was missing the "properties" field. In which I had to put my recursive reference.
The right jsonSchema is the following :
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"title": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"object"
]
},
"properties": {
"type": "object",
"patternProperties": {
".*": {
"$ref": "#/definitions/Field"
}
}
}
},
"definitions": {
"Field": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"myCustomProperty": {
"$ref": "#/definitions/myCustomProperty"
},
"properties": { <==================== here
"type": "object",
"patternProperties": {
".*": {
"$ref": "#/definitions/Field"
}
}
}
},
"required": [
"type",
"myCustomProperty"
]
},
"myCustomProperty": {/*rules*/}
}
}
So far it works as expected, but if someone has a more elegant suggestion please share!

Json Schema: Require a property only when a specific property is present in a deep nested object

I need to build a json schema (draft 4) that requires a property based on the presence of a property in another nested object. I already searched and tried a lot of things (anyOf, oneOf, not, dependencies) with no luck.
Maybe this is not possible to in json schema?
This is my simplified schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["dog"],
"properties": {
"dog": {
"type": "object",
"required": ["bananas"],
"properties": {
"bananas": { "$ref": "bananas.json" },
"thing": {
"type": "object",
"properties": {
"total": { "type": "string" }
}
}
}
}
}
}
And this is bananas.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["banana"],
"definitions": {
"non-empty-string": {
"type": "string",
"minLength": 1
}
},
"properties": {
"banana": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["unit"],
"properties": {
"unit": { "type": "string" },
"thing": {
"type": "object",
"anyOf": [
{ "required": [ "tax_transfers" ] },
{ "required": [ "tax_retentions" ] }
],
"properties": {
"tax_transfers": {
"type": "object",
"required": ["tax_transfer"],
"properties": {
"tax_transfer": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"rate": { "type": "string" }
}
}
}
}
},
"tax_retentions": {
"type": "object",
"required": ["tax_retention"],
"properties": {
"tax_retention": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"rate": { "type": "string" }
}
}
}
}
}
}
}
}
}
}
}
}
I need that when one or more objects in the array have a 'thing' property (at bananas -> banana -> thing).
Then the property 'thing' at (dog -> thing) should be required.
Any help would be really appreciated.
You need two things to express your constraint. The first is "contains" and the other is "implication". I've organized each in the definitions section.
Contains
The items keyword allows us to require that all items in an array are valid against a schema. If it is not true that all of the items in the array are not valid against the schema, then we know that at least one item is valid.
{
"not": {
"items": { "not": { ... schema ... } }
}
}
If you are able to upgrade to JSON Schema draft-06, a contains keyword was added to make this much easier.
{
"contains": { ... schema ... }
}
Implication
Implication allows you to do something like a conditional. The condition schema implies the constraint schema if either the condition is true, or the constraint is true (or both are true). It's effectively the same as saying, if the condition is true then the constraint must also be true.
{
"anyOf": [
{ "not": { ... condition schema ... } },
{ ... constraint schema ... }
]
}
JSON Schema draft-07 adds the if-then-else keywords in attempt to address this case better. I personally dislike the way this was done enough that I'll stick with the implication pattern for this kind of thing, but here it is in case you want to try it.
{
"if": { ... schema ... },
"then": { ... schema ... },
"else": { ... schema ... }
}
All together
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["dog"],
"properties": {
"dog": {
"type": "object",
"required": ["bananas"],
"properties": {
"bananas": { "$ref": "bananas.json" },
"thing": { "type": "object" }
}
}
},
"allOf": [
{ "$ref": "#/definitions/banana-things-implies-dog-things" }
],
"definitions": {
"banana-has-things": {
"properties": {
"dog": {
"properties": {
"bananas": {
"properties": {
"banana": {
"not": {
"items": { "not": { "required": ["things"] } }
}
}
}
}
}
}
}
},
"banana-things-implies-dog-things": {
"anyOf": [
{ "not": { "$ref": "#/definitions/banana-has-things" }},
{
"properties": {
"dog": { "required": ["things"] }
}
}
]
}
}
}

Choosing between several property sets in jdorn/json-editor

I have defined two different property sets in my JSON schema and I'm trying to put together an editor that will allow the user to choose one of several property sets and then fill the corresponding properties.
Here is my schema:
{
"type": "object",
"title": "Test Configuration",
"properties": {
"master_property_set": {
"title": "Testing oneOf",
"oneOf": [
{
"type": "object",
"title": "Property set 1",
"properties": {
"property1": {
"type": "string"
},
"property2": {
"type": "string"
}
}
},
{
"type": "object",
"title": "Property set 2",
"properties": {
"property3": {
"type": "string"
},
"property4": {
"type": "string"
}
}
}
]
}
}
}
Problem is, when I switch to Property set 2, my data still contains property1 and property2 as empty strings and they are displayed in the editor. They should get removed. What am I doing wrong?
The setup can be tested here: http://goo.gl/j91of7
I've found the answer in the docs. Turns out no_additional_properties property of the editor needs to be set to true.
{
"type": "object",
"title": "Test Configuration",
"properties": {
"master_property_set": {
"title": "Testing oneOf",
"oneOf": [
{
"type": "object",
"title": "Property set 1",
"properties": {
"property1": {
"type": "string"
},
"property2": {
"type": "string"
}
},
"additionalProperties":false
},
{
"type": "object",
"title": "Property set 2",
"properties": {
"property3": {
"type": "string"
},
"property4": {
"type": "string"
}
},
"additionalProperties":false
}
]
}
}
}

JsonSchema is not validated with oneOf

Need help to find the bug with this schema. It has oneOf operator.
Schema is here :
`{
"type": "object",
"required": [
"type",
"body"
],
"properties": {
"type": {
"description": "type of the document to post",
"type": "string",
"enum": [
"123",
"456"
]
},
"body": {
"type": "object",
"description": "body",
"oneOf": [{
"$ref": "#/definitions/abc",
"$ref": "#/definitions/def"
}]
}
},
"definitions": {
"abc": {
"type": "array",
"description": "abc",
"properties" : {
"name" : { "type" : "string" }
}
},
"def": {
"type": "array",
"description": "users","properties" : {
"name" : { "type" : "string" }
}
}
}
}`
My Json is this :
`{
"type": "123",
"body": {
"abc": [{
"name": "test"
}]
}
}`
It does not validate with tv4 and I also tried this online tool. It works without oneOf operator. Otherwise it does not validate it any tool.
Edit :
After reading the answers I modified the schema. New schema is :
{
"type": "object",
"properties": {
"type": {
"description": "type of the document to post",
"type": "string",
},
"body": {
"type": "object",
"description": "body",
"properties": {
"customers": {
"type": "array"
}
},
"anyOf": [
{
"title": "customers prop",
"properties": {
"customers": {
"type": "array",
"description": "customers",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
]
}
}
}
And json is here
{
"type": "customer",
"body": {
"none": [
{
"name": "test"
}
]
}
}
But it validates. I want to enforce one of "customers" or "users" in the body. To test I have removed users from the body.
Pl help.
The issue is that the data is passing both of your sub-schemas. oneOf means "match exactly one" - if you want "match at least one", then use anyOf.
In fact, both of your sub-schemas will pass all data. The reason is that properties is ignored when dealing with arrays.
What you presumably wanted to do instead is specify properties for the items in the array. For this, you need the items keyword:
"definitions": {
"abc": {
"type": "array",
"items": {
"type": "object",
"properties" : {
"name" : { "type" : "string" }
}
}
}
}
(You'll also need to add some distinct constraints - at the moment, both the "abc" and "def" definitions are identical apart from description, which makes the oneOf impossible because it will always match both or neither.)
Since you have the type at root level, you probably want the oneOf statement to check that an object with type "customer" has customers in the body (even though I would suggest skipping the body and placing customers and users directly in root object).
This works with your example, will require that an object with type "customer" has a body with "customers", and to clarify the matching, I let customer have the property "name" while the user has "username":
{
"type": "object",
"properties": {
"type": { "type": "string" },
"body": {
"type": "object",
"properties": {
"customers": {
"type": "array",
"items": { "$ref": "#/definitions/customer" }
},
"users": {
"type": "array",
"items": { "$ref": "#/definitions/user" }
}
}
}
},
"definitions": {
"customer": {
"type": "object",
"properties": { "name": { "type": "string" } },
"required": [ "name" ]
},
"user": {
"type": "object",
"properties": { "username": { "type": "string" } },
"required": [ "username" ]
}
},
"oneOf": [
{
"properties": {
"type": {
"pattern": "customer"
},
"body": {
"required": [ "customers" ]
}
}
},
{
"properties": {
"type": {
"pattern": "user"
},
"body": {
"required": [ "users" ]
}
}
}
]
}
When using "type": "array" then the item type is defined in the "items" property not "properties" property... Also both types in oneOf are same, but only one must match.
Try
...
"definitions": {
"abc": {
"type": "array",
"description": "abc",
"items" : {
"name" : { "type" : "string" }
}
},
"def": {
"type": "array",
"description": "users",
"items" : {
"username" : { "type" : "string" }
}
}
}