Skip to content

Commit 950e960

Browse files
Docs: Improve format documentation (#963)
* fix: remove Format from string.md * fix: add Format to type.md * simplify the data and time intro in type.md * update type.md with new suggestions
1 parent 087d54b commit 950e960

File tree

2 files changed

+61
-126
lines changed

2 files changed

+61
-126
lines changed

pages/understanding-json-schema/reference/string.md

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -121,129 +121,3 @@ with an optional area code:
121121
// props { "indent": true, "valid": false }
122122
"(800)FLOWERS"
123123
```
124-
125-
## Format[#format]
126-
127-
The `format` keyword allows for basic semantic identification of certain
128-
kinds of string values that are commonly used. For example, because JSON
129-
doesn\'t have a \"DateTime\" type, dates need to be encoded as strings.
130-
`format` allows the schema author to indicate that the string value
131-
should be interpreted as a date. By default, `format` is just an
132-
annotation and does not effect validation.
133-
134-
Optionally, validator [implementations](../../learn/glossary#implementation) can provide a configuration option
135-
to enable `format` to function as an assertion rather than just an
136-
annotation. That means that validation will fail if, for example, a
137-
value with a `date` format isn\'t in a form that can be parsed as a
138-
date. This can allow values to be constrained beyond what the other
139-
tools in JSON Schema, including [Regular Expressions](../../understanding-json-schema/reference/regular_expressions) can
140-
do.
141-
142-
> Implementations may provide validation for only a subset of the built-in
143-
> formats or do partial validation for a given format. For example, some
144-
> implementations may consider a string an email if it contains a `@`,
145-
> while others might do additional checks for other aspects of a well
146-
> formed email address.
147-
148-
There is a bias toward networking-related formats in the JSON Schema
149-
specification, most likely due to its heritage in web technologies.
150-
However, custom formats may also be used, as long as the parties
151-
exchanging the JSON documents also exchange information about the custom
152-
format types. A JSON Schema validator will ignore any format type that
153-
it does not understand.
154-
155-
### Built-in formats[#built-in-formats]
156-
157-
The following is the list of formats specified in the JSON Schema
158-
specification.
159-
160-
#### Dates and times
161-
162-
Dates and times are represented in [RFC 3339, section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6). This is a subset
163-
of the date format also commonly known as [ISO8601 format](https://www.iso.org/iso-8601-date-and-time-format.html).
164-
165-
- `"date-time"`: Date and time together, for example,
166-
`2018-11-13T20:20:39+00:00`.
167-
- `"time"`: <StarInline label="New in draft 7" /> Time, for example, `20:20:39+00:00`
168-
- `"date"`: <StarInline label="New in draft 7" /> Date, for example, `2018-11-13`.
169-
- `"duration"`: <StarInline label="New in draft 2019-09" /> A duration as defined by the [ISO 8601 ABNF for \"duration\"](https://datatracker.ietf.org/doc/html/rfc3339#appendix-A).
170-
For example, `P3D` expresses a duration of 3 days.
171-
172-
<Keywords label="single: email single: idn-email single: format; email single: format; idn-email" />
173-
174-
#### Email addresses
175-
176-
- `"email"`: Internet email address, see [RFC 5321, section 4.1.2](http://tools.ietf.org/html/rfc5321#section-4.1.2).
177-
- `"idn-email"`: <StarInline label="New in draft 7" /> The internationalized form of an Internet email
178-
address, see [RFC 6531](https://tools.ietf.org/html/rfc6531).
179-
180-
<Keywords label="single: hostname single: idn-hostname single: format; hostname single: format; idn-hostname" />
181-
182-
#### Hostnames
183-
184-
- `"hostname"`: Internet host name, see [RFC 1123, section 2.1](https://datatracker.ietf.org/doc/html/rfc1123#section-2.1).
185-
- `"idn-hostname"`: <StarInline label="New in draft 7" /> An internationalized Internet host name, see
186-
[RFC5890, section 2.3.2.3](https://tools.ietf.org/html/rfc5890#section-2.3.2.3).
187-
188-
<Keywords label="single: ipv4 single: ipv6 single: format; ipv4 single: format; ipv6" />
189-
190-
#### IP Addresses
191-
192-
- `"ipv4"`: IPv4 address, according to dotted-quad ABNF syntax as
193-
defined in [RFC 2673, section 3.2](http://tools.ietf.org/html/rfc2673#section-3.2).
194-
- `"ipv6"`: IPv6 address, as defined in [RFC 2373, section 2.2](http://tools.ietf.org/html/rfc2373#section-2.2).
195-
196-
<Keywords label="single: uuid single: uri single: uri-reference single: iri single: iri-reference single: format; uuid single: format; uri single: format; uri-reference single: format; iri single: format; iri-reference" />
197-
198-
#### Resource identifiers
199-
200-
- `"uuid"`: <StarInline label="New in draft 2019-09" /> A Universally Unique Identifier as defined by [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122). Example:
201-
`3e4666bf-d5e5-4aa7-b8ce-cefe41c7568a`
202-
- `"uri"`: A universal resource identifier (URI), according to
203-
[RFC3986](http://tools.ietf.org/html/rfc3986).
204-
- `"uri-reference"`: <StarInline label="New in draft 6" /> A URI Reference (either a URI or a
205-
relative-reference), according to [RFC3986, section 4.1](http://tools.ietf.org/html/rfc3986#section-4.1).
206-
- `"iri"`: <StarInline label="New in draft 7" /> The internationalized equivalent of a \"uri\", according to
207-
[RFC3987](https://tools.ietf.org/html/rfc3987).
208-
- `"iri-reference"`: <StarInline label="New in draft 7" /> The internationalized equivalent of a
209-
\"uri-reference\", according to
210-
[RFC3987](https://tools.ietf.org/html/rfc3987)
211-
212-
If the values in the schema have the ability to be relative to a
213-
particular source path (such as a link from a webpage), it is generally
214-
better practice to use `"uri-reference"` (or `"iri-reference"`) rather
215-
than `"uri"` (or `"iri"`). `"uri"` should only be used when the path
216-
must be absolute.
217-
218-
<Keywords label="single: uri-template single: format; uri-template" />
219-
220-
#### URI template
221-
222-
- `"uri-template"`: <StarInline label="New in draft 6" /> A URI Template (of any level) according to
223-
[RFC6570](https://tools.ietf.org/html/rfc6570). If you don\'t
224-
already know what a URI Template is, you probably don\'t need this
225-
value.
226-
227-
<Keywords label="single: json-pointer single: relative-json-pointer single: format; json-pointer single: format; relative-json-pointer" />
228-
229-
#### JSON Pointer
230-
231-
- `"json-pointer"`: <StarInline label="New in draft 6" /> A JSON Pointer, according to
232-
[RFC6901](https://tools.ietf.org/html/rfc6901). There is more
233-
discussion on the use of JSON Pointer within JSON Schema in
234-
[Structuring a complex schema](../../understanding-json-schema/structuring). Note that this should be used only when
235-
the entire string contains only JSON Pointer content, e.g.
236-
`/foo/bar`. JSON Pointer URI fragments, e.g. `#/foo/bar/` should use
237-
`"uri-reference"`.
238-
- `"relative-json-pointer"`: <StarInline label="New in draft 7" /> A [relative JSON pointer](https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01).
239-
240-
<Keywords label="single: regex single: format; regex" />
241-
242-
#### Regular Expressions
243-
244-
- `"regex"`: <StarInline label="New in draft 7" /> A regular expression, which should be valid according to
245-
the [ECMA 262](https://www.ecma-international.org/publications-and-standards/standards/ecma-262/)
246-
[dialect](../../learn/glossary#dialect).
247-
248-
Be careful, in practice, JSON schema validators are only required to
249-
accept the safe subset of [regular expressions](../../understanding-json-schema/reference/regular_expressions) described elsewhere in this document.

pages/understanding-json-schema/reference/type.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,64 @@ types. For example, numeric types have a way of specifying a numeric
181181
range, that would not be applicable to other types. In this reference,
182182
these validation keywords are described along with each of their
183183
corresponding types in the following chapters.
184+
185+
## Format[#format]
186+
187+
The `format` keyword conveys semantic information for values that may be difficult or impossible to describe using JSON Schema. Typically, this semantic information is described by other documents. The JSON Schema Validation specification defines several formats, but this keyword also allows schema authors to define their own formats.
188+
189+
For example, because JSON doesn't have a "DateTime" type, dates need to be encoded as strings. `format` allows the schema author to indicate that the string value should be interpreted as a date. By default, `format` is just an annotation and does not affect validation.
190+
191+
Optionally, validator [implementations](../../learn/glossary#implementation) can provide a configuration option to enable `format` to function as an assertion rather than just an annotation. That means that validation fails when, for example, a value with a `date` format isn't in a form that can be parsed as a date. This allows values to be constrained beyond what other tools in JSON Schema, including [Regular Expressions](../../understanding-json-schema/reference/regular_expressions), can do.
192+
193+
> Implementations may provide validation for only a subset of the built-in formats or do partial validation for a given format. For example, some implementations may consider a string an email if it contains an `@`, while others might perform additional checks for other aspects of a well-formed email address.
194+
195+
The JSON Schema specification has a bias toward networking-related formats due to its roots in web technologies. However, custom formats may also be used if the parties exchanging the JSON documents share information about the custom format types. A JSON Schema validator will ignore any format type it does not understand.
196+
197+
### Built-in Formats
198+
199+
It should be noted that `format` is not limited to a specific set of valid values or types. Users may define their own custom keywords including ones that work with JSON data types other than `string`, such as `number`. Below, we cover the formats specified in the JSON Schema specification.
200+
201+
#### Dates and Times
202+
203+
Dates and times are represented in [RFC 3339, section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6). RFC 3339 is a specification from the Internet Engineering Task Force (IETF).
204+
205+
- `"date-time"`: Date and time together, for example, `2018-11-13T20:20:39+00:00`.
206+
- `"time"`: <StarInline label="New in draft 7" /> Time, for example, `20:20:39+00:00`.
207+
- `"date"`: <StarInline label="New in draft 7" /> Date, for example, `2018-11-13`.
208+
- `"duration"`: <StarInline label="New in draft 2019-09" /> A duration as defined by the [ISO 8601 ABNF for "duration"](https://datatracker.ietf.org/doc/html/rfc3339#appendix-A). For example, `P3D` expresses a duration of 3 days.
209+
210+
#### Email Addresses
211+
212+
- `"email"`: Internet email address, see [RFC 5321, section 4.1.2](http://tools.ietf.org/html/rfc5321#section-4.1.2).
213+
- `"idn-email"`: <StarInline label="New in draft 7" /> The internationalized form of an Internet email address, see [RFC 6531](https://tools.ietf.org/html/rfc6531).
214+
215+
#### Hostnames
216+
217+
- `"hostname"`: Internet host name, see [RFC 1123, section 2.1](https://datatracker.ietf.org/doc/html/rfc1123#section-2.1).
218+
- `"idn-hostname"`: <StarInline label="New in draft 7" /> An internationalized Internet host name, see [RFC5890, section 2.3.2.3](https://tools.ietf.org/html/rfc5890#section-2.3.2.3).
219+
220+
#### IP Addresses
221+
222+
- `"ipv4"`: IPv4 address, according to dotted-quad ABNF syntax as defined in [RFC 2673, section 3.2](http://tools.ietf.org/html/rfc2673#section-3.2).
223+
- `"ipv6"`: IPv6 address, as defined in [RFC 2373, section 2.2](http://tools.ietf.org/html/rfc2373#section-2.2).
224+
225+
#### Resource Identifiers
226+
227+
- `"uuid"`: <StarInline label="New in draft 2019-09" /> A Universally Unique Identifier as defined by [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122). Example: `3e4666bf-d5e5-4aa7-b8ce-cefe41c7568a`.
228+
- `"uri"`: A universal resource identifier (URI), according to [RFC3986](http://tools.ietf.org/html/rfc3986).
229+
- `"uri-reference"`: <StarInline label="New in draft 6" /> A URI Reference (either a URI or a relative-reference), according to [RFC3986, section 4.1](http://tools.ietf.org/html/rfc3986#section-4.1).
230+
- `"iri"`: <StarInline label="New in draft 7" /> The internationalized equivalent of a "uri", according to [RFC3987](https://tools.ietf.org/html/rfc3987).
231+
- `"iri-reference"`: <StarInline label="New in draft 7" /> The internationalized equivalent of a "uri-reference", according to [RFC3987](https://tools.ietf.org/html/rfc3987).
232+
233+
#### URI Template
234+
235+
- `"uri-template"`: <StarInline label="New in draft 6" /> A URI Template (of any level) according to [RFC6570](https://tools.ietf.org/html/rfc6570). If you don\'t already know what a URI Template is, you probably don\'t need this value.
236+
237+
#### JSON Pointer
238+
239+
- `"json-pointer"`: <StarInline label="New in draft 6" /> A JSON Pointer, according to [RFC6901](https://tools.ietf.org/html/rfc6901). There is more discussion on using JSON Pointer within JSON Schema in [Structuring a complex schema](../../understanding-json-schema/structuring). Note that this should be used only when the entire string contains only JSON Pointer content, e.g., `/foo/bar`. JSON Pointer URI fragments, e.g., `#/foo/bar/` should use `"uri-reference"`.
240+
- `"relative-json-pointer"`: <StarInline label="New in draft 7" /> A [relative JSON pointer](https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01).
241+
242+
#### Regular Expressions
243+
244+
- `"regex"`: <StarInline label="New in draft 7" /> A regular expression that should be valid according to the [ECMA 262](https://www.ecma-international.org/publications-and-standards/standards/ecma-262/) [dialect](../../learn/glossary#dialect). Be careful, in practice, JSON Schema validators are only required to accept the safe subset of [regular expressions](../../understanding-json-schema/reference/regular_expressions) described elsewhere in this document.

0 commit comments

Comments
 (0)