Skip to content

Commit

Permalink
add description and examples for enum keyword (#46)
Browse files Browse the repository at this point in the history
Initial PR.

Juan, please consider providing some suggestions on the type of examples
I should cover, specifically in terms of complexity.

_Edit: This PR addresses #86_

---------

Co-authored-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
AgniveshChaubey and jviotti authored Feb 5, 2024
1 parent 31b142b commit 965e8b1
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions content/2020-12/validation/enum.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,59 @@ related:
- vocabulary: applicator
keyword: oneOf
---

The `enum` keyword specifies a validation constraint for an instance, defining a set of permissible values. The value of the `enum` keyword must be an array containing at least one element, and these elements should be unique. The validation succeeds if the value of the instance matches one of the elements in the `enum` array.

_**Note:** Using the `type` keyword along the `enum` keyword is considered an anti-pattern, as `enum` constraints instances tighter than `type`._

## Examples

{{<schema `Schema with string enum`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ "red", "green", "blue" ]
}
{{</schema>}}

{{<instance-pass `Instance with value present in the enum is valid`>}}
"green"
{{</instance-pass>}}

{{<instance-fail `Instance with value not present in the enum is invalid`>}}
"black"
{{</instance-fail>}}

{{<schema `Schema with number enum`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ 2, 46, 100 ]
}
{{</schema>}}

{{<instance-pass `Instance with value present in the enum is valid`>}}
45
{{</instance-pass>}}

{{<instance-fail `Instance with value not present in the enum is invalid`>}}
70
{{</instance-fail>}}

{{<instance-fail `Instance with value having different datatype is invalid`>}}
"2"
{{</instance-fail>}}

{{<schema `Schema with mixed-type enum`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ "red", 123, true, { "foo": "bar" }, [ 1, 2 ], null ]
}
{{</schema>}}

{{<instance-pass `Instance with value present in the enum is valid`>}}
true
{{</instance-pass>}}

{{<instance-fail `Instance with value not present in the enum is invalid`>}}
{ "foo": "baz" }
{{</instance-fail>}}
- _Without specifying a type, you can utilize enum to accept values of various types._

0 comments on commit 965e8b1

Please sign in to comment.