Object properties defined by enum #818
-
Is is possible to restrict object properties (/property names) to values of an enum, and/or define the set of required properties based on an enum? For example, say I want to define a schema to validate a localization file format such as {
"en": {
"my_string_1": "My String 1",
"my_string_2": "My String 2"
},
"fr": {
"my_string_1": "My String 1",
"my_string_2": "My String 2"
}
} I have enums defined for both locale and translation tag, and I want to define schemas that are able to both restrict properties to the values defined in the enum and (ideally seperately) require that all those properties are present. Essentially, something equivalent to {
"type": "object",
"properties": {
"en": {
"type": "object",
"properties": {
"my_string_1": { ... },
...
},
"required": ["my_string_1", ...],
"additionalProperties": false
},
...
},
"additionalProperties": false
"required": ["en", ...]
} using $refs to the enums and without having to duplicate the enum values into the properties/required lists. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
I think I understand what you're wanting. Please let me know if I've missed something. The first keyword you're looking for is {
"type": "object",
"propertyNames": {
"enum": [ "en", "fr", /* ... */ ]
},
// ...
} You're already using the second keyword you need: {
"type": "object",
"propertyNames": {
"enum": [ "en", "fr", /* ... */ ]
},
"additionalProperties": {
"type": "object",
"properties": {
"my_string_1": { /* ... */ },
// ...
},
"required": ["my_string_1", /* ... */ ],
"additionalProperties": false
},
// ...
} The last piece you mentioned was requiring that all of the properties are present. For this, I'd just use {
"type": "object",
"propertyNames": {
"enum": [ "en", "fr", /* ... */ ]
},
"additionalProperties": {
"type": "object",
"properties": {
"my_string_1": { /* ... */ },
// ...
},
"required": ["my_string_1", /* ... */ ],
"additionalProperties": false
},
"minProperties": 5, // needs to match the length of /propertyNames/enum
"maxProperties": 5, // needs to match the length of /propertyNames/enum
// ...
} Unfortunately right now we don't have a way to have This will allow you to
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the response. I think I buried the lede a bit. What I'm looking for would be something more like (ignoring the second level of string tags for simplicity's sake): {
"type": "object",
"propertyNames": {
"$ref": "/locales.json"
},
"required": {
"$ref": "/locales.json"
}
} I specifically don't want to inline the enum values (or update min/max based on the counts), as they're already defined and maintained in a separate enum and I'm using/referencing them in multiple places. |
Beta Was this translation helpful? Give feedback.
-
I really think your ideal solution for this particular problem is to drop {
"type": "object",
"propertyNames": {
"enum": ["foo", "bar", "baz"]
},
"minProperties": 3
} This setup gives you:
|
Beta Was this translation helpful? Give feedback.
Right but the problem you're facing is that
required
takes an array of strings, not a schema, which means that you can't put the$ref
there. This is why I designeddata
the way I did.For what you want, you'd need to have locales.json be a file that just contains the array of values.
Then you'd use
data
like this:The way this works is that all of the values in
data
are replaced by the resolved values, which produces this (in memory):