skip to Main Content

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


  1. 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:

    void Main()
    {
        try
        {
            JsonSchema schema = JsonSchema.Parse(@"{
      '$schema': 'https://json-schema.org/draft/2020-12/schema',
        'type': 'object',
        'patternProperties': {
                    '^.*$': {
                        'type': 'string'
                    }
                },
                'additionalProperties': false
        }");
    
    
            JObject o = JObject.Parse("{"property1":"value1","property2":"value2","property1":"value3"}", new JsonLoadSettings
            {
                DuplicatePropertyNameHandling = DuplicatePropertyNameHandling.Error
            });
    
            bool valid = o.IsValid(schema).Dump();
            // true
        }
        catch (JsonReaderException ex)
        {
            Console.Write($"Invalid json file, {ex.Message}.");
        }
    }
    
    

    the exception would be :

    [Invalid json file, Property with the name 'property1' already exists in the current JSON object. Path 'property1', line 1, position 55..]
    
    Login or Signup to reply.
  2. Short answer is yes.. but you have

    I used this uniqueItems section to help answer your questions, https://ajv.js.org/json-schema.html#uniqueitems

    1. use a 3rd party library called AJV https://ajv.js.org/ to create your own properties
    2. Now you can create the unique schema and validate wit with that.

    // you get the AJV sechma validator
    // go here you will see it, https://ajv.js.org/
    const Ajv = require('ajv');
    const ajv = new Ajv(); // Instantiate Ajv JSON Schema validator
    
    // Here I would create my custom or own JSON schema
    const myUniqueSschema = {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "object",
      "properties": {
        "items": {
          "type": "array",
          // this is what you should try, unique
          "uniqueItems": true 
        }
      }
    };
    

    make sure you install first

    npm install -g ajv-cli
    ajv compile -s schema.json -o validate_schema.js
    

    you can either write a function or modify the data to do it for you
    https://ajv.js.org/guide/modifying-data.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search