-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into keyword-minlength
- Loading branch information
Showing
2 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 `Schema with regular expression for email validation`>}} | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" | ||
} | ||
{{</schema>}} | ||
|
||
{{<instance-pass `An instance adhering to the regular expression is valid`>}} | ||
"[email protected]" | ||
{{</instance-pass>}} | ||
|
||
{{<instance-fail `An instance not adhering to the regular expression is invalid`>}} | ||
"invalid@yahoo" | ||
{{</instance-fail>}} | ||
|
||
{{<schema `Schema with regular expression for password rules`>}} | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "string", | ||
"pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$" | ||
} | ||
{{</schema>}} | ||
|
||
{{<instance-pass `An instance adhering to the regular expression is valid`>}} | ||
"MyStrongPass89" | ||
{{</instance-pass>}} | ||
|
||
{{<instance-fail `An instance not adhering to the regular expression is invalid`>}} | ||
"password" | ||
{{</instance-fail>}} | ||
|
||
{{<schema `Schema with regular expression for usernames, including length restrictions`>}} | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_]+$", | ||
"minLength": 5, | ||
"maxLength": 15 | ||
} | ||
{{</schema>}} | ||
|
||
{{<instance-pass `An instance with alphanumeric and underscore values, with a length between 5 and 15, is valid`>}} | ||
"foo_bar123" | ||
{{</instance-pass>}} | ||
|
||
{{<instance-fail `An instance with special character in invalid`>}} | ||
"invalid#username" | ||
{{</instance-fail>}} | ||
|
||
{{<instance-fail `An instance that matches the regex but goes out of bounds is invalid`>}} | ||
"username_toolong123" | ||
{{</instance-fail>}} | ||
- _This keyword can be combined with other string-related keywords, such as `maxLength` and `minLength`, for comprehensive validation._ | ||
|
||
{{<schema `Schema with regular expression for some specific pattern`>}} | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "string", | ||
"pattern": "apple" | ||
} | ||
{{</schema>}} | ||
|
||
{{<instance-pass `An instance matching the pattern is valid`>}} | ||
"apple" | ||
{{</instance-pass>}} | ||
|
||
{{<instance-pass `An instance is also valid if the pattern matches anywhere within the string`>}} | ||
"I love apples!" | ||
{{</instance-pass>}} | ||
|
||
- _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 `Schema with a regular expression enforcing an exact pattern match`>}} | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "string", | ||
"pattern": "^apple$" | ||
} | ||
{{</schema>}} | ||
|
||
{{<instance-pass `An instance matching the pattern is valid`>}} | ||
"apple" | ||
{{</instance-pass>}} | ||
|
||
{{<instance-fail `An instance containing characters other than "apple" is invalid`>}} | ||
"I love apples!" | ||
{{</instance-fail>}} |