Skip to content

Commit

Permalink
Merge branch 'main' into keyword-exclusiveMaximum
Browse files Browse the repository at this point in the history
  • Loading branch information
AgniveshChaubey committed Feb 18, 2024
2 parents e789f84 + 828b446 commit 9b11fa8
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 17 deletions.
8 changes: 3 additions & 5 deletions assets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
$blue: #0969da;
$success: rgba(213, 255, 230, 1);
$danger: rgba(255, 213, 213, 1);
$warning: #ffff6a;

// Required components
@import "../vendor/bootstrap/scss/variables";
Expand Down Expand Up @@ -126,9 +127,6 @@ article table {
@extend .mt-4;
}

.alert {
background-color: #fffdbd;
padding: 10px;
border: 1px solid #ccc;
margin-bottom: 15px;
.alert > :last-child {
@extend .mb-0;
}
90 changes: 90 additions & 0 deletions content/2020-12/core/anchor.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,93 @@ related:
- vocabulary: core
keyword: $dynamicAnchor
---

The `$anchor` keyword is used to assign a unique identifier to a subschema within its schema resource. This identifier can then be referenced elsewhere using the `$ref` keyword.

* Its value must be a valid identifier starting with a letter and containing letters, digits, hyphens, underscores, colons, or periods.
* The `$anchor` keyword allows for the creation of plain reusable name fragments that aren't tied to specific structural locations, offering a flexible alternative to using JSON Pointer fragments, which require knowledge of the schema's structure.
* An anchor is resolved against the base URI of its schema resource.

## Examples

{{<schema `Schema with a named anchor (identifier)`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#string",
"$defs": {
"string": {
"$anchor": "string",
"type": "string"
}
}
}
{{</schema>}}

{{<instance-pass `An instance with a string is valid`>}}
"Hello World!"
{{</instance-pass>}}

{{<instance-fail `An instance with a number is invalid`>}}
44
{{</instance-fail>}}

{{<schema `Schema with identifiers having absolute URI`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"name": { "$ref": "https://example.com/person/name#name" },
"age": { "$ref": "https://example.com/person/age#age" }
},
"required": [ "name", "age" ],
"$defs": {
"name": {
"$id": "https://example.com/person/name",
"$anchor": "name",
"type": "string"
},
"age": {
"$id": "https://example.com/person/age",
"$anchor": "age",
"type": "integer"
}
}
}
{{</schema>}}

{{<instance-pass `An instance adhering to the schema is valid`>}}
{
"name": "John",
"age": 55
}
{{</instance-pass>}}

{{<instance-fail `The value of age must be an integer`>}}
{
"name": "foo",
"age": "bar"
}
{{</instance-fail>}}

{{<schema `Schema with location-independent identifier having base URI change in subschema`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/base",
"$ref": "https://example.com/nested#foo",
"$defs": {
"foo": {
"$id": "nested",
"$anchor": "foo",
"type": "integer"
}
}
}
{{</schema>}}

{{<instance-pass `An instance with integer is valid`>}}
99
{{</instance-pass>}}

{{<instance-fail `An instance with boolean is invalid`>}}
true
{{</instance-fail>}}
- Here the URI Reference of `foo` subschema is resolved to `https://example.com/nested` and the named anchor is used in the URI fragment to reference this subschema.
11 changes: 5 additions & 6 deletions content/2020-12/validation/const.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

{{<alert>}}
_**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._
{{</alert>}}

## Examples

{{<schema `Schema with a specific string value`>}}
Expand Down Expand Up @@ -55,11 +59,6 @@ The `const` keyword in JSON Schema restricts a property value to a single, speci
{{<schema `Schema with a fixed object structure`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "number" }
},
"const": { "name": "John Doe", "age": 30 }
}
{{</schema>}}
Expand Down
124 changes: 124 additions & 0 deletions content/2020-12/validation/maxLength.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,130 @@ The `maxLength` keyword is used to specify the maximum length of a string instan
"This is an invalid string"
{{</instance-fail>}}

{{<schema `Schema allowing either a string with a maximum of 20 characters or a numeric value`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": [ "string", "number" ],
"maxLength": 20
}

{{</schema>}}

{{<instance-pass `An instance with a string length less than or equal to 20 is valid`>}}
"This is a valid string"
{{</instance-pass>}}

{{<instance-fail `An instance with a string length greater than 20 is invalid`>}}
"This description is too long"
{{</instance-fail>}}

{{<instance-pass `An instance with a numeric value is valid`>}}
55
{{</instance-pass>}}

{{<schema `Schema for maximum string length validation with Unicode characters`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string",
"maxLength": 3
}
{{</schema>}}

{{<instance-pass `An instance with 3 or less characters is valid`>}}
"\u0066\u006F\u006F" // --> "foo"
{{</instance-pass>}}

{{<instance-fail `An instance with more than 3 characters is invalid`>}}
"\u0048\u0065\u006C\u006C\u006F" // --> "Hello"
{{</instance-fail>}}

The `maxLength` keyword is used to specify the maximum length of a string instance. It is used to enforce a constraint on the maximum number of characters allowed for a string instance.

* Applies only to string data types.
* Value must be a non-negative integer.
* String length is counted in characters, not bytes.
* Validation succeeds if the string length is less than or equal to the specified `maxLength`.

## Examples

{{<schema `Schema restricting string length to a maximum of 10 characters`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string",
"maxLength": 10
}
{{</schema>}}

{{<instance-pass `An instance with a string length less than or equal to 10 is valid`>}}
"foo"
{{</instance-pass>}}

{{<instance-fail `An instance with a string length greater than 10 is invalid`>}}
"This is an invalid string"
{{</instance-fail>}}

{{<schema `Schema allowing either a string with a maximum of 20 characters or a numeric value`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": [ "string", "number" ],
"maxLength": 20
}

{{</schema>}}

{{<instance-pass `An instance with a string length less than or equal to 20 is valid`>}}
"This is a valid string"
{{</instance-pass>}}

{{<instance-fail `An instance with a string length greater than 20 is invalid`>}}
"This description is too long"
{{</instance-fail>}}

{{<instance-pass `An instance with a numeric value is valid`>}}
55
{{</instance-pass>}}

{{<schema `Schema for maximum string length validation with Unicode characters`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string",
"maxLength": 3
}
{{</schema>}}

{{<instance-pass `An instance with 3 or less characters is valid`>}}
"\u0066\u006F\u006F" // --> "foo"
{{</instance-pass>}}

{{<instance-fail `An instance with more than 3 characters is invalid`>}}
"\u0048\u0065\u006C\u006C\u006F" // --> "Hello"
{{</instance-fail>}}

The `maxLength` keyword is used to specify the maximum length of a string instance. It is used to enforce a constraint on the maximum number of characters allowed for a string instance.

* Applies only to string data types.
* Value must be a non-negative integer.
* String length is counted in characters, not bytes.
* Validation succeeds if the string length is less than or equal to the specified `maxLength`.

## Examples

{{<schema `Schema restricting string length to a maximum of 10 characters`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string",
"maxLength": 10
}
{{</schema>}}

{{<instance-pass `An instance with a string length less than or equal to 10 is valid`>}}
"foo"
{{</instance-pass>}}

{{<instance-fail `An instance with a string length greater than 10 is invalid`>}}
"This is an invalid string"
{{</instance-fail>}}

{{<schema `Schema allowing either a string with a maximum of 20 characters or a numeric value`>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand Down
99 changes: 99 additions & 0 deletions content/2020-12/validation/pattern.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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>}}
12 changes: 6 additions & 6 deletions layouts/shortcodes/alert.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{ $heading := .Get 0 }}

<div class="alert">
<h5>{{ $heading | markdownify }}</h5>
<p>{{ .Inner | markdownify }}</p>
</div>
{{ $heading := .Get 0 }}

<div class="alert alert-warning" role="alert">
<h5>{{ $heading | markdownify }}</h5>
<p>{{ .Inner | markdownify }}</p>
</div>

0 comments on commit 9b11fa8

Please sign in to comment.