Skip to content

Commit db60fbc

Browse files
authored
Merge pull request #72 from shashank1010/change/improve-extendDraft-documentation
change: add an example to overwrite a formatter
2 parents 1ed8281 + 9738596 commit db60fbc

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,68 @@ const myDraft = extendDraft(draft2020, {
10571057
});
10581058
```
10591059

1060+
1061+
### Overwrite a format validator
1062+
The built-in format validators may not always align with your specific requirements. For instance, you might need to validate the output of an `<input type="time" />`, which produces values in formats like `HH:MM` or `HH:MM:SS`. In such cases, you can customize or overwrite the format validators to suit your needs using `extendDraft`
1063+
<details>
1064+
<summary>Example of overwriting a format validator</summary>
1065+
1066+
```ts
1067+
import { extendDraft, draft2020 } from "json-schema-library";
1068+
1069+
/**
1070+
* A Regexp that extends http://tools.ietf.org/html/rfc3339#section-5.6 spec.
1071+
* The specification requires seconds and timezones to be a valid date format.
1072+
*
1073+
* matchTimeSecondsAndTimeOptional matches:
1074+
* - HH:MM:SSz
1075+
* - HH:MM:SS(+/-)HH:MM
1076+
* - HH:MM:SS
1077+
* - HH:MMz
1078+
* - HH:MM(+/-)HH:MM
1079+
* - HH:MM
1080+
*/
1081+
const matchTimeSecondsAndTimeOptional =
1082+
/^(?<time>(?:([0-1]\d|2[0-3]):[0-5]\d(:(?<second>[0-5]\d|60))?))(?:\.\d+)?(?<offset>(?:z|[+-]([0-1]\d|2[0-3])(?::?[0-5]\d)?)?)$/i;
1083+
1084+
1085+
const customTimeFormatDraft = extendDraft(draft2020, {
1086+
formats: {
1087+
// This example extends the default time formatter which validates against RFC3339
1088+
time: ({ node, pointer, data }) => {
1089+
const { schema } = node;
1090+
if (typeof data !== "string" || data === "") {
1091+
return undefined;
1092+
}
1093+
1094+
// Use the Custom Regex to validate the date and time.
1095+
const matches = data.match(matchTimeSecondsAndTimeOptional);
1096+
if (!matches) {
1097+
return node.createError("format-date-time-error", { value: data, pointer, schema });
1098+
}
1099+
1100+
// leap second
1101+
if (matches.groups.second === "60") {
1102+
// Omitted the code here for brevity.
1103+
}
1104+
1105+
return undefined;
1106+
},
1107+
1108+
},
1109+
});
1110+
1111+
const { errors, valid } = compileSchema({
1112+
type: "string",
1113+
format: "time",
1114+
$schema: "https://json-schema.org/draft/2020-12/schema",
1115+
}, { drafts: [customTimeFormatDraft]}).validate("15:31:12");
1116+
1117+
console.assert(valid, errors.at(0)?.message);
1118+
```
1119+
1120+
</details>
1121+
10601122
### Keyword
10611123

10621124
**Keywords** hold the main logic for JSON Schema functionality. Each `Keyword` corresponds to a JSON Schema keyword like `properties`, `prefixItems`, `oneOf`, etc and offers implementations to `parse`, `validate`, `resolve` and `reduce`. Note that support for each implementation is optional, dependending on the feature requirements. The main properties of a `Keyword`:

0 commit comments

Comments
 (0)