Skip to content

dynamic enum on object's arbitrary key names? #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mewalig opened this issue Mar 3, 2020 · 4 comments
Closed

dynamic enum on object's arbitrary key names? #869

mewalig opened this issue Mar 3, 2020 · 4 comments

Comments

@mewalig
Copy link

mewalig commented Mar 3, 2020

Below is an example JSON Schema:

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "required": ["foo"] # "bar" would also be ok, but "baz" would be invalid since it's not a key of "properties"
}

Suppose we wanted to describe the schema of JSON Schema using JSON Schema. How would we describe the requirements that "required" can only contain values that exist as keys to "properties"?

{
  "type": "object",
  "properties": {
    "properties": {
      "type": "object"
    },
    "required": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": "what goes here???"
      }
    }
  }
}

This is the similar to (but not the same as) the questions posted at https://stackoverflow.com/questions/57556768/use-object-keys-as-type-in-json-schema and #855.

@handrews
Copy link
Contributor

handrews commented Mar 3, 2020

The general form of this question is being tracked in #855. It's one of the more controversial topics remaining in JSON Schema, so don't expect a quick or easy answer.

@handrews handrews closed this as completed Mar 3, 2020
@mewalig
Copy link
Author

mewalig commented Mar 4, 2020

Yes, I'm aware of #855 but my intent was precisely to create a narrower scope for the following reasons, neither of which can generally be said to hold true for the broader scope of #855:

  1. validators must at least handle this scope in order to validate the "required" keyword, so any discussion, concerns, etc related to actual implementation, validation etc can be informed by actual, well-used and real-world code bases
  2. this feature is necessary (I think...) if we want JSON Schema to be complete enough to describe itself. Granted, I'm not sure if "complete enough to describe itself" has any intrinsic value but it feels like a noble cause in principle...

@Relequestual
Copy link
Member

"bar" would also be ok, but "baz" would be invalid since it's not a key of "properties"

Where did you get that understanding from?

It's totally valid, and expected, to allow required to contain values not found in a properties object. In fact, I frequently use required without properties in the same schema object.

There are plenty of constructs of a JSON Schema which are valid but sensless or not useful, but it's impossible (I'd take an educated guess at this) to define them all, let alone constrain them all.

It's possible to create a JSON Schema which doesn't make any sense but is still valid. It's possible to combine schemas in a way which breaks expectations.

It sounds like what you want is a linter, not a validator. JSON Schema itself is not a linter. One is planned, but it's unlikely to be soon. You can check out an existing linter here: https://www.json-schema-linter.com/

@mewalig
Copy link
Author

mewalig commented Mar 4, 2020

Ah, thank you! I just assumed that anything in required had to be specified in properties and did not realize that wasn't the case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants