From 6108369f979921f7a41564840afb6bb495743b0a Mon Sep 17 00:00:00 2001 From: Agnivesh Chaubey Date: Thu, 8 Feb 2024 08:53:23 +0530 Subject: [PATCH 1/2] add description and examples for `const` keyword (#107) This PR addresses #87. --- content/2020-12/validation/const.markdown | 60 +++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/content/2020-12/validation/const.markdown b/content/2020-12/validation/const.markdown index 4d4ddb5d..fc93b54c 100644 --- a/content/2020-12/validation/const.markdown +++ b/content/2020-12/validation/const.markdown @@ -14,3 +14,63 @@ related: - vocabulary: validation keyword: type --- + +The `const` keyword in restricts an instance to a specific value. Its usage is functionally similar to an `enum` with a single value. Instances validate successfully only if their property value deeply matches the specified constant. + +* Applies to various JSON data types, including numbers, strings, booleans, objects, and arrays. +* Takes precedence over other validation keywords like `type` and `enum`. + +{{}} +_**Note:** It is best practice to avoid using the `type` keyword or any other validation keyword with `const`, as `const` takes precedence over them. Therefore, it is better not to use them together._ +{{}} + +## Examples + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "const": "hello" +} +{{}} + +{{}} +"hello" +{{}} + +{{}} +"world" +{{}} + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "const": 3.14159 +} +{{}} + +{{}} +3.14159 +{{}} + +{{}} +"pi" +{{}} + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "const": { "name": "John Doe", "age": 30 } +} +{{}} + +{{}} +{} +{{}} + +{{}} + { "name": "John Doe", "age": 30 } +{{}} + +{{}} + { "name": "Robert", "age": 30 } +{{}} From 2541cbf8f2f676c7a4865e57353b5171b676bea8 Mon Sep 17 00:00:00 2001 From: Agnivesh Chaubey Date: Fri, 9 Feb 2024 22:15:45 +0530 Subject: [PATCH 2/2] add description and examples for `pattern` keyword (#108) This PR addresses #88 --- content/2020-12/validation/pattern.markdown | 99 +++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/content/2020-12/validation/pattern.markdown b/content/2020-12/validation/pattern.markdown index 8def4bee..596fed7a 100644 --- a/content/2020-12/validation/pattern.markdown +++ b/content/2020-12/validation/pattern.markdown @@ -14,3 +14,102 @@ related: - vocabulary: applicator keyword: patternProperties --- + +The `pattern` keyword in JSON Schema is designed to define a regular expression pattern that a string value within an instance must adhere to. This regular expression is specified as a string for the `pattern` keyword. It functions as follows: + +* Assigns a regular expression (following [ECMA-262](https://www.ecma-international.org/publications-and-standards/standards/ecma-262/) dialect) to the `pattern` keyword to define the required format. +* A string value is considered valid only if it successfully matches the specified pattern. +* The regular expressions used with `pattern` are not implicitly anchored, requiring a complete match for validation. Partial matches are not accepted. + +## Examples + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "string", + "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" +} +{{}} + +{{}} +"john.doe@example.com" +{{}} + +{{}} +"invalid@yahoo" +{{}} + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "string", + "pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$" +} +{{}} + +{{}} +"MyStrongPass89" +{{}} + +{{}} +"password" +{{}} + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "string", + "pattern": "^[a-zA-Z0-9_]+$", + "minLength": 5, + "maxLength": 15 +} +{{}} + +{{}} +"foo_bar123" +{{}} + +{{}} +"invalid#username" +{{}} + +{{}} +"username_toolong123" +{{}} +- _This keyword can be combined with other string-related keywords, such as `maxLength` and `minLength`, for comprehensive validation._ + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "string", + "pattern": "apple" +} +{{}} + +{{}} +"apple" +{{}} + +{{}} +"I love apples!" +{{}} + +- _When defining regular expressions, it's crucial to note that a string is considered valid if the expression matches anywhere within it, as demonstrated in the above example._ + +- _To avoid this and ensure that the entire string exactly matches the `pattern`, you would surround the regular expression with `^` and `$`. See the example below._ + +{{}} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "string", + "pattern": "^apple$" +} +{{}} + +{{}} +"apple" +{{}} + +{{}} +"I love apples!" +{{}}