Skip to content

Commit 9b19a36

Browse files
committed
make format validate by default
1 parent 4b21ddd commit 9b19a36

File tree

1 file changed

+40
-50
lines changed

1 file changed

+40
-50
lines changed

specs/jsonschema-validation.md

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

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

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

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

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

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

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

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

354354
#### Custom format attributes
355355

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

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

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

375-
Implementations supporting formats SHOULD implement support for the following
376-
attributes:
377-
378-
- *date-time:* A string instance is valid against this attribute if it is a
370+
- *date-time*: A string instance is valid against this attribute if it is a
379371
valid representation according to the "date-time" ABNF rule (referenced above)
380-
- *date:* A string instance is valid against this attribute if it is a valid
372+
- *date*: A string instance is valid against this attribute if it is a valid
381373
representation according to the "full-date" ABNF rule (referenced above)
382-
- *time:* A string instance is valid against this attribute if it is a valid
374+
- *time*: A string instance is valid against this attribute if it is a valid
383375
representation according to the "full-time" ABNF rule (referenced above)
384-
- *duration:* A string instance is valid against this attribute if it is a valid
376+
- *duration*: A string instance is valid against this attribute if it is a valid
385377
representation according to the "duration" ABNF rule (referenced above)
386378

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

@@ -401,7 +391,7 @@ likely either be promoted to fully specified attributes or dropped.
401391

402392
These attributes apply to string instances.
403393

404-
A string instance is valid against these attributes if it is a valid Internet
394+
A string instance is valid against these format values if it is a valid Internet
405395
email address as follows:
406396

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

491481
Implementations that validate formats MUST accept at least the subset of
492-
ECMA-262 defined in {{regexinterop}}), and SHOULD accept all valid ECMA-262
482+
ECMA-262 defined in {{regexinterop}}, and SHOULD accept all valid ECMA-262
493483
expressions.
494484

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

0 commit comments

Comments
 (0)