Skip to content

Commit 6bc30f9

Browse files
committed
make format validate by default
1 parent 08943f7 commit 6bc30f9

File tree

1 file changed

+40
-50
lines changed

1 file changed

+40
-50
lines changed

jsonschema-validation.md

+40-50
Original file line numberDiff line numberDiff line change
@@ -292,39 +292,46 @@ Structural validation alone may be insufficient to allow an application to
292292
correctly utilize certain values. The `format` annotation keyword is defined to
293293
allow schema authors to convey semantic information for a fixed subset of values
294294
which are accurately described by authoritative resources, be they RFCs or other
295-
external specifications.
295+
external specifications. Format values defined externally to this document
296+
SHOULD also be based on such authoritative resources in order to foster
297+
interoperability.
296298

297-
The value of this keyword is called a format attribute. It MUST be a string. A
298-
format attribute can generally only validate a given set of instance types. If
299-
the type of the instance to validate is not in this set, validation for this
300-
format attribute and instance SHOULD succeed. All format attributes defined in
301-
this section apply to strings, but a format attribute can be specified to apply
302-
to any instance types defined in the data model defined in the [core JSON
303-
Schema.](#json-schema)[^1]
299+
The value of this keyword MUST be a string. While this keyword can validate any
300+
type, each distinct value will generally only validate a given set of instance
301+
types. If the type of the instance to validate is not in this set, validation
302+
for this keyword SHOULD succeed. All format values defined in this section apply
303+
to strings, but a format value can be specified to apply to any instance types
304+
defined in the data model defined in the [core JSON Schema](#json-schema)[^1].
304305

305306
[^1]: Note that the `type` keyword in this specification defines an "integer"
306307
type which is not part of the data model. Therefore a format attribute can be
307308
limited to numbers, but not specifically to integers. However, a numeric format
308-
can be used alongside the `type` keyword with a value of "integer", or could be
309-
explicitly defined to always pass if the number is not an integer, which
309+
can be used alongside the `type` keyword with a value of "integer", or it could
310+
be explicitly defined to always pass if the number is not an integer, which
310311
produces essentially the same behavior as only applying to integers.
311312

312-
Implementing support for `format` as an annotation is REQUIRED (if the
313-
implementation supports annotation collection).
313+
Implementations SHOULD provide assertion behavior for the format values defined
314+
by this document[^2] and MUST refuse to process any schema which contains an
315+
unsupported format value.
314316

315-
Implementing support for `format` as an assertion is OPTIONAL. Implementations
316-
which choose to support assertion behavior:
317+
[^2]: Assertion behavior is called out very explicitly because it is a departure
318+
from previous iterations of this specification. Previously, `format` was an
319+
annotation-only keyword by default and implementations that supported assertion
320+
were required to offer some configuration that allowed users to explicitly
321+
enable assertion. Assertion is now a requirement in order to meet user
322+
expectations. See [json-schema-org/json-schema-spec #1520](https://github.com/json-schema-org/json-schema-spec/issues/1520) for more.
323+
324+
In addition to the assertion behavior, this keyword also produces its value as
325+
an annotation.
326+
327+
Implementations:
317328

318-
- MUST still collect the keyword's value as an annotation (if the implementation
319-
supports annotation collection),
320-
- MUST provide a configuration option to enable assertion behavior, defaulting
321-
to annotation-only behavior
322329
- SHOULD provide an implementation-specific best effort validation for each
323-
format attribute defined below;[^3]
324-
- MAY choose to implement validation of any or all format attributes as a no-op
325-
by always producing a validation result of true;[^4]
326-
- SHOULD use a common parsing library for each format, or a well-known regular
327-
expression;
330+
format attribute defined in this document;[^3]
331+
- MAY support format values not defined in this document, but such support MUST
332+
be configurable and disabled by default;
333+
- SHOULD use a common parsing library or a well-known regular expression for
334+
each format;
328335
- SHOULD clearly document how and to what degree each format attribute is
329336
validated.
330337

@@ -337,30 +344,18 @@ example, an instance string that does not contain an "@" is clearly not a valid
337344
email address, and an "email" or "hostname" containing characters outside of
338345
7-bit ASCII is likewise clearly invalid.
339346

340-
[^4]: This matches the current reality of implementations, which provide widely
341-
varying levels of validation, including no validation at all, for some or all
342-
format attributes. It is also designed to encourage relying only on the
343-
annotation behavior and performing semantic validation in the application, which
344-
is the recommended best practice.
345-
346-
The requirement for minimal validation of format attributes is
347+
The requirement for minimal validation of format values in general is
347348
intentionally vague and permissive, due to the complexity involved in many of
348349
the attributes. Note in particular that the requirement is limited to syntactic
349-
checking; it is not to be expected that an implementation would send an email,
350-
attempt to connect to a URL, or otherwise check the existence of an entity
351-
identified by a format instance.
350+
checking; implementations SHOULD NOT attempt to send an email, connect to a URL,
351+
or otherwise check the existence of an entity identified by a format instance.
352352

353353
#### Custom format attributes
354354

355355
Implementations MAY support custom format attributes. Save for agreement between
356356
parties, schema authors SHALL NOT expect a peer implementation to support such
357357
custom format attributes.
358358

359-
An implementation MUST NOT fail to collect unknown formats as annotations.
360-
361-
When configured for assertion behavior for `format`, implementations MUST fail
362-
upon encountering unknown formats.
363-
364359
### Defined Formats
365360

366361
#### Dates, Times, and Duration
@@ -371,22 +366,17 @@ Date and time format names are derived from [RFC 3339, section 5.6](#rfc3339).
371366
The duration format is from the ISO 8601 ABNF as given in Appendix A of RFC
372367
3339.
373368

374-
Implementations supporting formats SHOULD implement support for the following
375-
attributes:
376-
377-
- *date-time:* A string instance is valid against this attribute if it is a
369+
- *date-time*: A string instance is valid against this attribute if it is a
378370
valid representation according to the "date-time" ABNF rule (referenced above)
379-
- *date:* A string instance is valid against this attribute if it is a valid
371+
- *date*: A string instance is valid against this attribute if it is a valid
380372
representation according to the "full-date" ABNF rule (referenced above)
381-
- *time:* A string instance is valid against this attribute if it is a valid
373+
- *time*: A string instance is valid against this attribute if it is a valid
382374
representation according to the "full-time" ABNF rule (referenced above)
383-
- *duration:* A string instance is valid against this attribute if it is a valid
375+
- *duration*: A string instance is valid against this attribute if it is a valid
384376
representation according to the "duration" ABNF rule (referenced above)
385377

386378
Implementations MAY support additional attributes using the other format names
387-
defined anywhere in that RFC. If "full-date" or "full-time" are implemented, the
388-
corresponding short form ("date" or "time" respectively) MUST be implemented,
389-
and MUST behave identically. Implementations SHOULD NOT define extension
379+
defined anywhere in that RFC. Implementations SHOULD NOT define extension
390380
attributes with any name matching an RFC 3339 format unless it validates
391381
according to the rules of that format.[^5]
392382

@@ -400,7 +390,7 @@ likely either be promoted to fully specified attributes or dropped.
400390

401391
These attributes apply to string instances.
402392

403-
A string instance is valid against these attributes if it is a valid Internet
393+
A string instance is valid against these format values if it is a valid Internet
404394
email address as follows:
405395

406396
- *email:* As defined by the "Mailbox" ABNF rule in [RFC 5321, section
@@ -488,7 +478,7 @@ A regular expression, which SHOULD be valid according to the
488478
[ECMA-262](#ecma262) regular expression dialect.
489479

490480
Implementations that validate formats MUST accept at least the subset of
491-
ECMA-262 defined in {{regexinterop}}), and SHOULD accept all valid ECMA-262
481+
ECMA-262 defined in {{regexinterop}}, and SHOULD accept all valid ECMA-262
492482
expressions.
493483

494484
## Keywords for the Contents of String-Encoded Data {#content}

0 commit comments

Comments
 (0)