From 965e8b15262beb86ce2b674bca55778728f97e82 Mon Sep 17 00:00:00 2001 From: Agnivesh Chaubey Date: Mon, 5 Feb 2024 22:22:09 +0530 Subject: [PATCH] add description and examples for `enum` keyword (#46) 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 --- content/2020-12/validation/enum.markdown | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/content/2020-12/validation/enum.markdown b/content/2020-12/validation/enum.markdown index 897f2129..6a18c98c 100644 --- a/content/2020-12/validation/enum.markdown +++ b/content/2020-12/validation/enum.markdown @@ -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": "https://json-schema.org/draft/2020-12/schema", + "enum": [ "red", "green", "blue" ] +} +{{}} + +{{}} +"green" +{{}} + +{{}} +"black" +{{}} + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 2, 46, 100 ] +} +{{}} + +{{}} +45 +{{}} + +{{}} +70 +{{}} + +{{}} +"2" +{{}} + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ "red", 123, true, { "foo": "bar" }, [ 1, 2 ], null ] +} +{{}} + +{{}} +true +{{}} + +{{}} +{ "foo": "baz" } +{{}} +- _Without specifying a type, you can utilize enum to accept values of various types._