I want to create a JSON Schema that allows properties with names that are arbitrary, but unique.
I have been trying to do this with patternProperties, but it seems to allow duplicate property names.
For example, the following schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"patternProperties": {
"^.*$": {
"type": "string"
}
},
"additionalProperties": false
}
Validates this JSON:
{
"property1" : "value1",
"property2" : "value2",
"property1" : "value3"
}
But I want it to fail because the 1st and 3rd property have the same name. How can I do that?
2
Answers
IMHO…
JSON Schema doesn’t have any validation to detect duplicate properties in a JSON object.
The JSON spec doesn’t make any mention of duplicate properties so they are valid but not recommended.
to check on duplicated names:
the exception would be :
I used this uniqueItems section to help answer your questions, https://ajv.js.org/json-schema.html#uniqueitems
make sure you install first
you can either write a function or modify the data to do it for you
https://ajv.js.org/guide/modifying-data.html