So I have a JSON schema with additionalProperties rule set to false like.
{
"type": "object",
"properties": {
"metadata": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
},
"c": {
"type": "string"
}
}
},
"street_type": {
"type": "string",
"enum": [
"Street",
"Avenue",
"Boulevard"
]
}
},
"additionalProperties": false
}
and a payload like
{
"metadata": {
"a": "aa",
"b": "bb",
"c": "cc",
"d": "dd"
}
}
Should I expect my JSON schema parser/validator to pass the validation, the JSON schema parser I am using com.github.fge.jsonschema.main.JsonSchema passes validation though metadata/d is not present in the schema with additionalProperties set to false,
This is very misleading, can someone direct me in the correct direction.
Is the additionalProperties JSON schema definition only applies to top-level fields and not to any nested level fields?
Is the additionalProperties JSON schema definition only applies to top-level fields and not to any nested level fields?
No you should be able to put it at whichever level you need as long as it is in a schema describing an object. In your case you simply put it at the wrong place. This should work:
{
"type": "object",
"properties": {
"metadata": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
},
"c": {
"type": "string"
}
},
"additionalProperties": false
},
"street_type": {
"type": "string",
"enum": [
"Street",
"Avenue",
"Boulevard"
]
}
}
}
Let say that you wanted to validate the following object as is:
{
a: {
b: {
c: {
d: 42
}
}
}
}
One valid schema for it would be:
{
"type": "object",
"additionalProperties": false,
"properties": {
"a": {
"type": "object",
"additionalProperties": false,
"properties": {
"b": {
"type": "object",
"additionalProperties": false,
"properties": {
"c": {
"type": "object",
"additionalProperties": false,
"properties": {
"d": {
"const": 42
}
}
}
}
}
}
}
}
}
The schema above is extremely verbose but is here for illustration purpose. You should be able to make it a bit more succinct by using $ref and combining schemas together.
Related
In JSON Schema, I can use require to ensure that a property exists on the same level of the hierarchy, but I'm having trouble validating for nested ones.
Suppose I have following JSON Schema:
{
"type": "object",
"properties": {
"my_type": {
"type": "string"
},
"t1_data": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
},
"t2_data": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
}
}
}
How would I specify the following validations?
if my_type == "type1", then t1_data.id must exist
if my_type == "type2", then t2_data.id must exist
if my_type is anything else, validation passes
I've tried using the require and anyOf constructs but I could only get them to work at the same level of the hierarchy.
Thanks,
A possible solution is to combine allOf and if-then. It is a little bit verbose but I am not aware of any shorter way. Here is the schema for the case "type1":
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"type": "object",
"properties": {
"my_type": {
"type": "string"
},
"t1_data": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
},
"t2_data": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
}
},
"allOf": [
{
"if": {
"properties": {
"my_type": {
"const": "type1"
}
}
},
"then": {
"properties": {
"t1_data": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"required": [ "id" ]
}
}
}
}
]
}
"type2" would be quite the same as next schema in the allOf array.
I have two types defined by a JSON Schema:
Type A
{
"type": "object",
"properties": {
"a": { "type": "string" }
}
}
Type X
{
"type": "object",
"properties": {
"x": { "type": "string" }
}
}
I want to create a schema combining A and X, but without additional properties. (only properties 'a' and/or 'x' should be present). I tried to use "allOf" but I can't add a restriction about additional properties. If I add "additionalProperties" in A or X, it doesn't work.
How should I process ? Of course, I don't want to repeat A into X
What you're looking for is the unevaluatedProperties feature available in jsonschema 2019-09 upwards. In contrast to additionalProperties, which only concerns the locally defined properties, unevaluatedProperties works across sub-schemas (but is more expensive to evaluate).
The schema
{
"anyOf": [
{
"type": "object",
"properties": {
"a": {
"type": "string"
}
}
},
{
"type": "object",
"properties": {
"x": {
"type": "string"
}
}
}
],
"unevaluatedProperties": false
}
Allows
{
"a": "foo",
"x": "bar"
}
But disallows
{
"a": "foo",
"x": "bar",
"t": "baz"
}
If you're stuck with an earlier jsonschema version, you have to manually define the properties of the sub-schemas in the parent like this:
{
"anyOf": [
{
"type": "object",
"properties": {
"a": {
"type": "string"
}
}
},
{
"type": "object",
"properties": {
"x": {
"type": "string"
}
}
}
],
"properties": {
"a": true,
"x": true
},
"additionalProperties": false
}
I am trying to get the "oneof" to allow for options in root items but can't find an example and what I try gives an error.
I can get it to work if it is under another item but not under the root {'s
Example - a Job Payment that has required fields (jobNum, payee, amount, type, ) and an option for the payment type (checkInfo or dollarAmt). I know this could be done other ways, but I need this method for a more complex schema.
{
"jobNum": "x216",
"payee": "John Doe",
"type": "check",
"amount": "112.25",
"checkInfo": {
"number": "386"
}
}
{
"JobNum": "x216",
"Payee": "John Doe",
"type" : "Cash",
"amount" : "112.25",
"cashInfo" : {
"dollarAmt" : "112",
"coinAmt" : "0.25"
}
}
The following gives me this error - "Unexpected token encountered when reading value for 'oneOf'. Expected StartObject, Boolean, got StartArray"
{
"description": "Job Payment",
"type": "object",
"required": [ "jobNum", "payee", "amount", "type"],
"properties": {
"jobNum": {
"type": "string"
},
"payee": {
"type": "string"
},
"amount": {
"type": "string"
},
"type": {"enum": [ "check", "cash" ]
},
"oneOf": [
{ "$ref": "#/definitions/ptCash" },
{ "$ref": "#/definitions/ptCheck" }
]
},
"definitions": {
"ptCash": {
"properties": {
"checkInfo": {
"number": "string"
}
},
"required": [ "checkInfo" ],
"additionalProperties": false
},
"ptCheck": {
"properties": {
"dollarAmt": {
"type": "string"
},
"coinAmt": {
"type": "string"
}
},
"required": [ "dollarAmt", "coinAmt" ],
"additionalProperties": false
}
},
"additionalProperties": false
}
There are a a few issues with your schema. I fixed it for you below. I won't explain all the changes I made because I think it is mostly pretty clear by reading the schema. If you want more detail on anything, just ask and I'll update the answer with more details.
The oneOf keyword can only appear in a schema. The properties keyword is an object whose values are schemas. When you put "oneOf" directly under properties, it's not interpreted as a keyword, it's interpreted as a property called "oneOf". The validator then complains because the value of property "oneOf" is expected to be a schema, not an array of schemas like the oneOf keyword.
Your use of additionalProperties doesn't work. This keyword doesn't work the way people often assume that it does. JSON Schema keywords are not aware of any state outside of the schema they are in. Let's look at the "ptCheck" branch of your oneOf first. This describes the property "number", says it is required and that there may be no keywords other than "number". Then your top level defines a the properties "jobNum", "payee", "amount", and "type", requires them all and allows no other properties. These two things can never be true at the same time. Even though your schema is valid, there is no JSON value that can ever be valid against this schema. That's why I moved the definitions of "checkInfo" and "cashInfo" to the top level and only put the required part in oneOf. The only downside to this approach is that you can pass both a "checkInfo" and a "cachInfo" object and it will validate. The extraneous property gets ignored. There are ways around this, but they are problematic enough that I don't advise using them.
I always advise people not to use "additionalProperties": false and to ignore unknown properties instead. The reason is that JSON Schema is a constraint system. Any valid JSON is valid against the empty schema ({}) and each keyword in the schema adds some constraint. This is a different approach to what people are used to when defining classes. An empty class describes nothing and valid values are added. We use "additionalProperties": false to get JSON Schema to behave more like defining a class, but trying to get JSON Schema to behave like something it isn't causes challenges like the one you see here.
{
"description": "Job Payment",
"type": "object",
"required": ["jobNum", "payee", "amount", "type"],
"properties": {
"jobNum": { "type": "string" },
"payee": { "type": "string" },
"amount": { "type": "string" },
"type": { "enum": ["check", "cash"] },
"checkInfo": {
"type": "object",
"properties": {
"number": { "type": "string" }
},
"required": ["number"]
},
"cashInfo": {
"type": "object",
"properties": {
"dollarAmt": { "type": "string" },
"coinAmt": { "type": "string" }
},
"required": ["dollarAmt", "coinAmt"]
}
},
"oneOf": [
{ "$ref": "#/definitions/ptCash" },
{ "$ref": "#/definitions/ptCheck" }
],
"definitions": {
"ptCheck": {
"type": "object",
"properties": {
"type": { "enum": ["check"] }
},
"required": ["checkInfo"]
},
"ptCash": {
"type": "object",
"properties": {
"type": { "enum": ["cash"] }
},
"required": ["cashInfo"]
}
},
"additionalProperties": false
}
oneOf should be placed in prope
Have to re-write rule for both ptCash and ptCheck by using type: object
Following schema should work with ptCheck:
{
"description": "Job Payment",
"type": "object",
"required": [ "jobNum", "payee", "amount", "type"],
"properties": {
"jobNum": {
"type": "string"
},
"payee": {
"type": "string"
},
"amount": {
"type": "string"
},
"type": {"enum": [ "check", "cash" ]
}
},
"oneOf": [
{ "$ref": "#/definitions/ptCash" },
{ "$ref": "#/definitions/ptCheck" }
],
"definitions": {
"ptCash": {
"properties": {
"checkInfo": {
"type": "object",
"required": ["number"],
"properties": {
"number": {
"type": "string"
}
}
}
},
"required": [ "checkInfo" ]
},
"ptCheck": {
"properties": {
"cashInfo": {
"type": "object",
"properties": {
"dollarAmt": {
"type": "string"
},
"coinAmt": {
"type": "string"
}
},
"required": ["dollarAmt", "coinAmt"]
}
},
"required": ["cashInfo"]
}
}
}
Provide some example as below:
import jsonschema
import simplejson as json
schema_filename = '47926398.json'
with open(schema_filename, 'r') as f:
schema_data = f.read()
schema = json.loads(schema_data)
# validate with checkInfo
json_obj = {
"jobNum": "x216",
"payee": "John Doe",
"type": "check",
"amount": "112.25",
"checkInfo": {
"number": "386"
}
}
jsonschema.validate(json_obj, schema)
# invalidate
json_obj = {
"jobNum": "x216",
"payee": "John Doe",
"type": "check",
"amount": "112.25",
"checkInfox": {
"number": "386"
}
}
jsonschema.validate(json_obj, schema)
# validate with cashInfo
json_obj = {
"jobNum": "x216",
"payee": "John Doe",
"type": "check",
"amount": "112.25",
"cashInfo": {
"dollarAmt": "400",
"coinAmt": "30"
}
}
jsonschema.validate(json_obj, schema)
# invalidate with cashInfo
json_obj = {
"jobNum": "x216",
"payee": "John Doe",
"type": "check",
"amount": "112.25",
"cashInfox": {
"dollarAmt": "400",
"coinAmt": "30"
}
}
jsonschema.validate(json_obj, schema)
# invalidate with cashInfo.dollarAmtx
json_obj = {
"jobNum": "x216",
"payee": "John Doe",
"type": "check",
"amount": "112.25",
"cashInfo": {
"dollarAmtx": "400",
"coinAmt": "30"
}
}
jsonschema.validate(json_obj, schema)
I have the following json object:
{
"my_items": [
{ "a": "primary", n: 1 },
{ "b": "secondary", n: 2 },
{ "b": "secondary", n: 3 }
]
}
All items in the my_items list are expected to be unique. Now, I need to validate the entire json object with the following rule:
it may contain zero or multiple items with "type": "secondary", but it absolutely must contain one and only one item with "type": "primary".
How can this be expressed using latest json-schema?
I'm come up with the following:
var schema = {
"definitions": {
"primary_item": {
"type": "object",
"properties": {
"a": {
"type":"string",
"enum":["primary"]
}
}
},
"secondary_item": {
"type": "object",
"properties": {
"b": {
"type": "string",
"enum":["secondary"]
}
}
}
},
"type": "object",
"properties": {
"my_items": {
"type": "array",
"minItems": 1,
"contains": {"$ref": "#/definitions/primary_item"},
"items": {
"anyOf": [
{"$ref": "#/definitions/primary_item"},
{"$ref": "#/definitions/secondary_item"}
],
"additionalProperties": false
}
}
},
"additionalProperties": false
};
var validate = ajv.compile(schema);
test({
"my_items": [
{"a": "primary"},
{"b": "secondary"},
{"b": "secondary"}
]
});
But the tests are failing, with the following errors:
Invalid: data.my_items[0] should NOT have additional properties, data.my_items[1] should NOT have additional properties, data.my_items[2] should NOT have additional properties
There is no way in JSON Schema to assert that an array contains one and only one of something. You can assert there is at least one, but that's the best you can do. The closest you can get is to require that the primary_item is the first element in the array.
{
"type": "object",
"properties": {
"my_items": {
"type": "array",
"items": [
{"$ref": "#/definitions/primary_item"}
],
"additionalItems": {"$ref": "#/definitions/secondary_item"}
}
},
"additionalProperties": false,
"definitions": {
"primary_item": {
"type": "object",
"properties": {
"a": { "enum":["primary"] }
},
"additionalProperties": false
},
"secondary_item": {
"type": "object",
"properties": {
"b": { "enum":["secondary"] }
},
"additionalProperties": false
}
}
}
EDIT
Responding to the comment
I wonder if one and only one could be expressed using two concepts: at least one + unique.
Yes. If your array items are unique you can do the following.
{
"type": "object",
"properties": {
"my_items": {
"type": "array",
"items": { "$ref": "#/definitions/my_item" },
"allOf": [{"$ref": "#/definitions/contains_primary_item"}],
"uniqueItems": true
}
},
"additionalProperties": false,
"definitions": {
"my_item": {
"type": "object",
"properties": {
"a": { "type": "string" }
},
"additionalProperties": false
},
"primary_item": {
"type": "object",
"properties": {
"a": { "enum":["primary"] }
},
"additionalProperties": false
},
"contains_primary_item": {
"not": {
"items": {
"not": { "$ref": "#/definitions/primary_item" }
}
}
}
}
}
I have the following json
{
"Dettype": "QTY",
"Details": [
{
"12568": {
"Id": 12568,
"qty":1,
"Freq":"2",
"Option": 0,
"promote":"yes"
},
"22456": {
"Id": 22456,
"qty":2,
"Freq":"3",
"Option": 1,
"promote":"no"
}
}
]
}
For the above json i need to write a json schema file which will valdiates the request.
but the problem is in array the key value for each item changes dynamically. If it is some constant value i can write but don't how to do the dynamic pattern
JSON schema i got
{
"type": "object",
"additionalProperties": true,
"properties": {
"Dettype": {
"type": "string"
},
"Details": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"**DYNAMIC VALUE**": {
"type": "object",
"additionalProperties": true,
"properties": {
"Id": {
"type": "integer"
},
"qty": {
"type": "integer"
},
"Freq": {
"type": "string"
},
"Option": {
"type": "integer"
},
"promote": {
"type": "string"
}
}
}
}
}
}
}
}
Can some one tell what changes need to be done for schema
This is what patternProperties is for.
Here it seems your object member keys are always digits; therefore you can write things like this:
"type": "object",
"patternProperties": {
"^\\d+$": {
"type": "object",
"etc": "etc"
}
}
You can also use additionalProperties if you want all properties to match some schema:
{
"type": "object",
"additionalProperties": {
"type": "object",
"etc": "etc"
}
}