Skip to content
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

v6 validation: "constant" #58

Closed
handrews opened this issue Sep 18, 2016 · 7 comments
Closed

v6 validation: "constant" #58

handrews opened this issue Sep 18, 2016 · 7 comments

Comments

@handrews
Copy link
Contributor

Originally written by @geraintluff at https://github.com/json-schema/json-schema/wiki/constant-(v5-proposal)

Proposed keywords

  • constant

Purpose

For ordinary use, this would be equivalent to a single-valued enum, simply tidier.

The only real difference comes in its behaviour with $data-substitution. $data-subsitution would be allowed for this keyword, which means that this keyword is not capable of specifying literal values of the form: {"$data":...}, because these would be interpreted as $data-substitutions.

However, literal values of this form can still be specified using enum, so there is no loss of functionality.

Values

The value of this keyword would be any value - however, it would be subject to $data-substitution.

Validation

Instances are only valid if they are exactly equal to the value of this keyword.

Example

Simple constant

{
    "type": "object",
    "properties": {
        "five": {
            "constant": 5
        }
    }
}

Valid: {}, {"five": 5}
Invalid: {"five": 0}, {"five": "5"}

Using $data to specify equality

{
    "type": "object",
    "properties": {
        "a": {"type": "string"},
        "b": {
            "constant": {"$data": "1/a"}
        }
    },
    "required": ["a", "b"]
}

Valid: {"a": "foo", "b": "foo"}, {"a": "bar", "b": "bar"}
Invalid: {"a": "foo", "b": "bar"}

Concerns

Similarity to enum

Unless $data is being used, the same effect can be obtained using fewer actual characters:

  • {"constant":"whatever"} - 23 characters
  • {"enum":["whatever"]} - 21 characters

However, when used in combination with $data, it opens up possibilities that are not otherwise available.

@handrews
Copy link
Contributor Author

I personally find constant to be useful independent of $data as it more clearly expresses the intent of the schema author to require a fixed value, and not an enum that (at the moment) happens to only have one entry.

@HotelDon
Copy link

Personally, I get the most use out of constant when combining it with $data, but you bring up an excellent point I hadn't considered with regards to ambiguity when you use enum. While I'm still pushing for $data, I think constant could stand on its own if it really needed to.

@sceutre
Copy link

sceutre commented Sep 28, 2016

Maybe "const" instead of "constant". Seems more natural/widely spread as a keyword, and trounces the "enum" form in character count ;-)

@awwright awwright added this to the draft-6 milestone Oct 8, 2016
@epoberezkin
Copy link
Member

It is useful, particularly in combination with $data keyword - it allows to validate joins in objects from ORM systems.

@fny
Copy link

fny commented Oct 31, 2016

If you're really concerned about character count, you could always use const:

{"constant":"whatever"} - 23 characters
{"enum":["whatever"]} - 21 characters
{"const":"whatever"} - 20 characters

handrews added a commit to handrews/json-schema-spec that referenced this issue Nov 8, 2016
This addresses issue json-schema-org#58, which got quite a few upvotes and has
a "draft 6" milestone.

I used the shorter "const" that several people suggested rather
than "constant", but obviously that's easy to change if the longer
form is preferred.

The wording was pretty much directly lifted from "enum".
handrews added a commit to handrews/json-schema-spec that referenced this issue Nov 16, 2016
This addresses issue json-schema-org#58, which got quite a few upvotes and has
a "draft 6" milestone.

I used the shorter "const" that several people suggested rather
than "constant", but obviously that's easy to change if the longer
form is preferred.

The wording was pretty much directly lifted from "enum".
handrews added a commit to handrews/json-schema-spec that referenced this issue Nov 17, 2016
This addresses issue json-schema-org#58, which got quite a few upvotes and has
a "draft 6" milestone.

I used the shorter "const" that several people suggested rather
than "constant", but obviously that's easy to change if the longer
form is preferred.

The wording was pretty much directly lifted from "enum".
@awwright
Copy link
Member

Landed in #139 and 6873d66 -- does this version look appealing to everyone?

@handrews
Copy link
Contributor Author

It's been over a week and no one has objected to the PR so I'm closing this as fixed. Obviously can be re-opened if someone gets back from vacation or something and wants to raise it.

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

No branches or pull requests

6 participants