I’ve got some Json
{
"store_purchase_control": {
"pass_except_these": [
[
{
"vendor_name_list": {
"value_contains": [
"Amazon",
"Amazon Toys"
]
}
}
]
]
}
}
I’m trying to validate against json schema..
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Controls",
"description": "Controls",
"definitions": {
"transaction_amount_limit": {
"type": "integer"
},
"mcc": {
"type": "string",
"pattern": "[0-9][0-9][0-9][0-9]"
},
"val_contains": {
"type": "string"
},
"value_equals": {
"type": "string"
},
"range": {
"type": "object",
"required": ["from", "through"],
"properties": {
"from": {
"$ref": "#/definitions/mcc"
},
"through": {
"$ref": "#/definitions/mcc"
}
}
},
"store_list": {
"type": "array",
"items": {
"$ref": "#/definitions/range"
}
},
"vendor_name_list": {
"type": "array",
"items": {
"$ref": "#/definitions/val_contains"
}
},
"store_id_list": {
"type": "array",
"items": {
"$ref": "#/definitions/value_equals"
}
},
"allow_it": {
"type": "array",
"items": {
"anyOf": [
{"$ref": "#/definitions/vendor_name_list"},
{"$ref": "#/definitions/store_list"},
{"$ref": "#/definitions/store_id_list"}
]
}
},
"always_block": {
"type": "array",
"items": {
"anyOf": [
{"$ref": "#/definitions/vendor_name_list"},
{"$ref": "#/definitions/store_list"},
{"$ref": "#/definitions/store_id_list"}
]
}
},
"pass_except_these": {
"type": "array",
"items": {
"type": "array",
"items": {
"anyOf": [
{"$ref": "#/definitions/vendor_name_list"},
{"$ref": "#/definitions/store_list"},
{"$ref": "#/definitions/store_id_list"}
]
},
"minItems": 1,
"maxItems": 3
}
},
"block_all_except": {
"type": "array",
"items": {
"anyOf": [
{"$ref": "#/definitions/vendor_name_list"},
{"$ref": "#/definitions/store_list"},
{"$ref": "#/definitions/store_id_list"}
]
}
},
"store_purchase_control": {
"type": "object",
"properties": {
"pass_except_these": {"$ref": "#/definitions/pass_except_these"},
"allow_it": {"$ref": "#/definitions/allow_it"},
"always_block": {"$ref": "#/definitions/always_block"}
},
"additionalProperties": false
}
},
"type": "object",
"properties": {
"store_purchase_control": {"$ref": "#/definitions/store_purchase_control"}
},
"additionalProperties": false
}
Parsing this using ..
https://www.jsonschemavalidator.net/
JSON does not match any schemas from 'anyOf'.
Schema path:
#/definitions/pass_except_these/items/items/anyOf
Message:
Invalid type. Expected Array but got Object.
Schema path:
#/definitions/store_id_list/type
Message:
Invalid type. Expected Array but got Object.
Schema path:
#/definitions/store_list/type
Message:
Invalid type. Expected Array but got Object.
Schema path:
#/definitions/vendor_name_list/type
It seems to be unable to resolve the anyOf keyword
2
Answers
The error message "Invalid type. Expected Array but got Object" indicates that there is a mismatch between the expected and actual types in your JSON schema. In line 3 of your schema, you define the property "store_purchase_control" as an object, but in your JSON data, it is an object rather than an array.
To resolve this issue, you need to adjust your JSON data to match the schema definition. Here’s the corrected JSON data that aligns with your schema:
And if you can’t change the json you can change the schema validate.
To modify the JSON schema for validation, you can make changes to the schema definition based on your requirements. Here’s an example of how you can modify the schema:
It’s not clear what you’re trying to do with this, but it appears that you’re trying to add some of the schema constraints to the data.
This is the corrected data that matches the schema:
The
pass_except_these
property expects an array of arrays, where each item in the secondary array is one of:vendor_name_list
which is an array of stringsstore_list
which is an array ofrange
valuesstore_id_list
which is an array of stringsSo you have an array of arrays, where each item is also an array. You have three levels of arrays.
From the naming of things inside the schema, I expect that this might not be what you’re trying to do, but that’s the data that satisfies the schema. If you suspect the schema might be wrong, the question is not clear about that.