I'm a beginner to json (I do not have a history with JS) and only recently I've met with the need for json schema and it's validation. The problem could be described as: I have 5 keys (properties) in my json; A, B, C, D, E, of which A, B and C are required, but D and E are not, specifically, every combination of D, E and their abscence is valid. However, there cannot be any additional keys other than D and E. Here's a couple examples (Items in sets represent keys of the object):
{A, B, C, D, E} # valid
{A, B, C, D} # valid
{A, B, C, E} # valid
{A, B, C} # valid
{A, B, D, E} # invalid (C key is missing)
{A, B, C, D, Z} # invalid (Z is not an allowed additional key)
{A, B, C, Z} # invalid
First, I've tried writing my own validation function, which worked, but in our project we already use jsonschema.validate
in other places and my supervisor told me to try solving this problem with the aforementioned method.
Here's the schema that was closed to solving the problem;
my_schema = {
"type": "object",
"maxProperties": 5,
"required": [
"A", "B", "C"
],
"properties": {
"A": {"type": ["boolean", "number"]},
"B": {"type": "array"},
"C": {"type": "number"},
"D": {"type": "string"},
"E": {"type": "string"}
}
}
however a test case where the keys are:
{A, B, C, D, Z}
is passing as a valid schema whereas it should not.