diff --git a/content/2020-12/validation/const.markdown b/content/2020-12/validation/const.markdown index 7b2d2ec8..fc93b54c 100644 --- a/content/2020-12/validation/const.markdown +++ b/content/2020-12/validation/const.markdown @@ -15,11 +15,15 @@ related: keyword: type --- -The `const` keyword in JSON Schema restricts a property value to a single, specific constant, applicable to any data type, including null. Its usage is functionally similar to an `enum` with a single value. Instances validate successfully only if their property value precisely matches the specified constant. +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 {{}} @@ -55,11 +59,6 @@ The `const` keyword in JSON Schema restricts a property value to a single, speci {{}} { "$schema": "https://json-schema.org/draft/2020-12/schema", - "type": "object", - "properties": { - "name": { "type": "string" }, - "age": { "type": "number" } - }, "const": { "name": "John Doe", "age": 30 } } {{}} 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!" +{{}}