xref: /aosp_15_r20/external/json-schema-validator/doc/duration.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1## Validating RFC 3339 durations
2
3JSON Schema Draft 2019-09 and later uses RFC 3339 to define dates and times.
4RFC 3339 bases its definition of duration of what is in the 1988 version of
5ISO 1801, which is over 35 years old and has undergone many changes with
6updates in 1991, 2000, 2004, 2019 and an amendment in 2022.
7
8There are notable differences between the current version of ISO 8601 and
9RFC 3339:
10* ISO 8601-2:2019 permits negative durations</li>
11* ISO 8601-2:2019 permits combining weeks with other terms (e.g. `P1Y13W`)
12
13There are also notable differences in how RFC 3339 defines a duration compared
14with how the Java Date/Time API defines it:
15* `java.time.Duration` accepts fractional seconds; RFC 3339 does not
16* `java.time.Period` does not accept a time component while RFC 3339 accepts both date and time components
17* `java.time.Duration` accepts days but not years, months or weeks
18
19By default, the duration validator performs a strict check that the value
20conforms to RFC 3339. You can relax this constraint by setting strict to false.
21
22```java
23SchemaValidatorsConfig config = new SchemaValidatorsConfig();
24config.setStrict("duration", false);
25JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema, config);
26```
27
28The relaxed check permits:
29* Fractional seconds
30* Negative durations
31* Combining weeks with other terms
32