json-schema-org/json-schema-spec

✨ Proposal: Add `uniqueProperties` to "Validation Keywords for Objects"

Closed this issue · 2 comments

Describe the inspiration for your proposal

I did want to create validation schema for following object:

{
  "1": "foo",
  "2": "bar",
  "3": "baz",
  "4": "qux",
  "5": "quux"
}

and I finished with such schema:

{
  	"$id": "https://example.com/schema/die-hard-schema",
  	"$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
      "enum-list": {
        "description": "Object with 5 properties",
        "type": "object",
        "propertyNames": {
            "enum": ["1", "2", "3", "4", "5"]
        },
        "maxContains": 5,
        "additionalProperties": false,
        "patternProperties": {
          ".*": {
            "type": "string"
          }
        }
    }
  }
}

But it still allows duplicated properties names. I'm missing equivalent of uniqueItems. This schema JSON should not be valid, yet there is no way to dscribe it using JSON Schema: https://www.jsonschemavalidator.net/s/YsCaX00k

Describe the proposal

Introduce uniqueProperties to "Validation Keywords for Objects", an equivalent of uniqueItems for Arrays.

It would use of the same object property name twice.

Describe alternatives you've considered

I did consider some JSON Schema vocabulary and found only this one kind of suitable: https://docs.json-everything.net/schema/vocabs/array-ext/ but I don't think I can achieve what I want with it.

Additional context

All I did find are explanations like this or this

It can be shortened to: "Object with duplicate properties names is a valid JSON, so there is no point in adding uniqueProperties constraint to JSON schema."

But so is JSON object with 20 properties and with 50 properties and even with 500 properties. It is still a valid object, yet we have maxProperties and minProperties constraints.

All I did find are explanations like this or this

It can be shortened to: "Object with duplicate properties names is a valid JSON, so there is no point in adding uniqueProperties constraint to JSON schema."

But so is JSON object with 20 properties and with 50 properties and even with 500 properties. It is still a valid object, yet we have maxProperties and minProperties constraints.

I think you have a conceptual misunderstanding... the problem is not that duplicate object property names are valid JSON, it's that duplicate property names are not even observable in the data model for JSON Document Instances against which JSON Schema validation is performed:

Whitespace and formatting concerns, including different lexical representations of numbers that are equal within the data model, are thus outside the scope of JSON Schema.

Since an object cannot have two properties with the same key, behavior for a JSON document that tries to define two properties with the same key in a single object is undefined.

Or more succinctly: https://json-schema.org/learn/glossary#json

JSON being a concrete format for representing data, and JSON Schema being a way to schematize data which is written in a JSON-compatible format


For example, { "foo": "bar" } and { "foo": "bar", "foo": "bar" } are literally indistinguishable in a validator that does not outright reject the second document as invalid (and AFAIK, no JSON Schema validator is that strict), just like the following JSON text pairs are all alternate representations of identical data per RFC 8259:

  • {"foo":0} vs. { "foo": 0 } (JSON Grammar: "Insignificant whitespace is allowed before or after any of the six structural characters")
  • { "hundred": 100 } vs. { "hundred": 1.000e+2 } (Numbers: "A number is represented in base 10 using decimal digits")
  • { "operators": "+\t-\t*\t/" } vs. { "operators": "\u002B\u0009\u002d\u0009\u002A\t\/" } (Strings: "Any character may be escaped")

Thanks, @gibson042 for the great explanation. I'm going to close this as out of scope for JSON Schema.