json-schema-org / json-schema-spec

The JSON Schema specification
http://json-schema.org/
Other
3.75k stars 264 forks source link

✨ Proposal: Support $ref-to-enum in object `required` #1551

Open etinquis opened 3 hours ago

etinquis commented 3 hours ago

Describe the inspiration for your proposal

High-Level goal: I want to be able to consolidate and re-use what are practically-speaking a static set of property names or object keys within a schema such that I am able to maintain the list in one place and reference them as either object property names or values, depending on context.

[My examples will assume I'm trying to define a schema for a localization data structure]

I start with a few enum schemas containing my keys.

{
    "$id": "/translation-slug.json",
    "title": "TranslationSlug",
    "enum": ["my_string_1", "my_string_2", /*...*/ ]
}
{
    "$id": "/locale.json",
    "title": "Locale",
    "enum": ["en", "fr", /*...*/ ]
}

I can reference this enum as values for properties on objects as so

{
    "$id": "/translation-assignment.json",
    "title": "TranslationAssignment",
    "type": "object",
    "properties": {
       "locale": { $ref: "/locale.json" },
       "slug": { $ref: "/translation-slug.json" },
       "translator": { $ref: '/translator.json' }
    },
   "required": ["locale", "slug", "translator"]
}
{
   "locale": "en",
   "slug": "my_string_2",
   "translator": "me"
}

I can reference this enum to define property names on objects as so

{
    "$id": "/translated-batch.json",
    "title": "TranslatedBatch",
    "type": "object",
    "propertyNames": {
       "$ref": "/locale.json"
    },
    "additionalProperties": {
       "type": "object",
       "propertyNames": {
          "$ref": "/translation-slug.json"
       },
       "additionalProperties": {
         "type": "string"
       }
    }
}
{
   "en": {
     "my_string_1": "..."
   },
   "de": {
     "my_string_2": "..."
   }
}

Now I want to define my 'source-of-truth' translated string file format schema, where I want validation to fail if any property is missing:

{
  "en": {
      "my_string_1": "...",
      "my_string_2": "...",
      "my_string_3": "..."
   },
   "fr": {
      /* my_string_1 is missing! */
      "my_string_2": "...",
      "my_string_3": "..."
    },
   /* de is missing! */
}

Describe the proposal

Possibilities:

An 'overload' on required directly allowing references to enum schemas

{
    "$id": "/translations.json",
    "title": "Translations",
    "type": "object",
    "propertyNames": {
       "$ref": "/locale.json"
    },
    "additionalProperties": {
       "type": "object",
       "propertyNames": {
          "$ref": "/translation-slug.json"
       },
       "additionalProperties": {
         "type": "string"
       },
       "required": { "$ref": "/translation-slug.json" }
    },
    "required": { "$ref": "/locales.json" }
}

A new required-adjacent property allowing references to enum schemas

{
    "$id": "/translations.json",
    "title": "Translations",
    "type": "object",
    "propertyNames": {
       "$ref": "/locale.json"
    },
    "additionalProperties": {
       "type": "object",
       "propertyNames": {
          "$ref": "/translation-slug.json"
       },
       "additionalProperties": {
         "type": "string"
       },
       "requiredPropertiesEnum": { "$ref": "/translation-slug.json" }
    },
    "requiredPropertiesEnum": { "$ref": "/locales.json" }
}

Some way to define propertyNames from an enum in a way that also makes them all required

{
    "$id": "/translations.json",
    "title": "Translations",
    "type": "object",
    "propertyNamesRequired": {
       "$ref": "/locale.json"
    },
    "additionalProperties": {
       "type": "object",
       "propertyNamesRequired": {
          "$ref": "/translation-slug.json"
       },
       "additionalProperties": {
         "type": "string"
       }
    }
}

Describe alternatives you've considered

The only way I understand there to be to implement this currently to duplicate and inline the values separately into 'required'.

{
    "$id": "/translations.json",
    "title": "Translations",
    "type": "object",
    "propertyNames": {
       "$ref": "/locale.json"
    },
    "additionalProperties": {
       "type": "object",
       "propertyNames": {
          "$ref": "/translation-slug.json"
       },
       "additionalProperties": {
         "type": "string"
       },
       "required": [ "my_string_1", "my_string_2", ... ]
    },
    "required": [ "en", "fr", ... ]
}

This imposes a potentially significant maintenance burden whereby changes to the enum must be duplicated in the subset of places where the full set should be required. Failure to propagate those changes also risks falsely validating schemas 'silently' or failing what should be valid schemas without some sort of linting procedure that is capable of identifying this pattern.

Additional context

https://github.com/orgs/json-schema-org/discussions/818 was my original Q&A discussion post looking for whether this was possible.

etinquis commented 2 hours ago

None of the possibilities above consider any form of composition of multiple enums, which may also be a nice feature if it's possible.

etinquis commented 2 hours ago

An potential alternate proposal:

Adjust the semantics of required such that it behaves similarly to propertyNames.

That is, required is a string-validating schema that evaluates against the properties present on an object, and where 'valid' resolves to required and 'invalid' resolves to not-required.

The current list definition is substituted with an enum schema with the inline values, which might provide backwards-compatibility?

EDIT: Or maybe that doesn't make sense. 🤔 It'd have to intersect with the properties defined, and somehow still be able to identify missing keys.

etinquis commented 2 hours ago

Alternative alternative proposal:

required is a string array-validating schema over all present property names, with the current list case being constructed via contains.

I assume this wouldn't surface errors very clearly.

EDIT: e.g.

{
   "required": ["property1", "property2"]
}

potentially equivalent to

{
  "required": {
    "type": "array",
    "allOf": [
      {
        "contains": {
          "const": "property1"
        }
      },
      {
        "contains": {
          "const": "property2"
        }
      }
    ],
    "additionalItems": true,
    "uniqueItems": true
  }
}

and I believe the enum case could be

{
   "required": {
     "type": "array",
     "items": {
          "$ref": "/enum.json"
     },
     "additionalItems": false,
     "uniqueItems": true
   }
}