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