-
Hello everyone, I am currently working on a project validating JSON data using JSON Schema. I've encountered a specific scenario for which I'm seeking guidance. I have data on the following structure: {"cards": {"Card test 1x": "value1", "another_key": "value2"}} Here are my validation requirements:
I have attempted to create a JSON Schema for this, but I'm facing challenges. Here's the current schema: {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"cards": {
"type": [
"object",
"null"
],
"minProperties": 1,
"patternProperties": {
"^Card.* (1|2|3|4|1/4)x$": {
"type": "string"
}
}
}
}
} However, it seems that the current schema allows data like I'm particularly looking for insights or corrections to the schema that would help enforce the required keys based on a regex pattern. Any help or suggestions would be greatly appreciated. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
The last constraint isn't possible to express in JSON Schema. There isn't a way to limit how many properties |
Beta Was this translation helpful? Give feedback.
-
You may try to achieve one of the pattern properties required by using double negation here. {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"assets": {
"anyOf": [
{"type": "null"},
{
"allOf": [
{
"not": {
"propertyNames": {
"pattern": "^(?!Card.* (1|2|3|4|1/4)x$)",
}
}
},
{
"patternProperties": {
".*": {"type": "string"}
},
},
]
},
]
}
},
} |
Beta Was this translation helpful? Give feedback.
-
Came here looking for the same answer. This would make sense to me. |
Beta Was this translation helpful? Give feedback.
You may try to achieve one of the pattern properties required by using double negation here.
First, use a negative lookahead regex to validate that the schema does not consist only from properties that are not matched, and after that, use a "not" keyword.