Skip to content

Commit ce19f3e

Browse files
committed
docs: remove reference to older documentation
1 parent 1c836f2 commit ce19f3e

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

README.md

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
<a href="#overview"><b>Overview</b></a> · <a href="#schemanode-methods"><b>Methods</b></a> · <a href="#draft-customization"><b>Customization</b></a> · <a href="#keyword-extensions"><b>Extensions</b></a> · <a href="#breaking-changes">Breaking Changes</a>
1515
</div>
1616

17-
---
18-
19-
> ⚠️ This **documentation** refers to the upcoming release **version 10**, which can be installed by `npm install [email protected]`. For the latest release please refer to the **[documentation of version 9.3.5](https://github.com/sagold/json-schema-library/tree/v9.3.5)**. If you are on v10-rc refer to [documentation of version 10.0.0-rc8 or lower](https://github.com/sagold/json-schema-library/tree/v10.0.0-rc8)
20-
2117
**Quick start**
2218

2319
`npm install json-schema-library`
@@ -1057,9 +1053,10 @@ const myDraft = extendDraft(draft2020, {
10571053
});
10581054
```
10591055

1060-
10611056
### Overwrite a format validator
1057+
10621058
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`
1059+
10631060
<details>
10641061
<summary>Example of overwriting a format validator</summary>
10651062

@@ -1069,7 +1066,7 @@ import { extendDraft, draft2020 } from "json-schema-library";
10691066
/**
10701067
* A Regexp that extends http://tools.ietf.org/html/rfc3339#section-5.6 spec.
10711068
* The specification requires seconds and timezones to be a valid date format.
1072-
*
1069+
*
10731070
* matchTimeSecondsAndTimeOptional matches:
10741071
* - HH:MM:SSz
10751072
* - HH:MM:SS(+/-)HH:MM
@@ -1078,41 +1075,42 @@ import { extendDraft, draft2020 } from "json-schema-library";
10781075
* - HH:MM(+/-)HH:MM
10791076
* - HH:MM
10801077
*/
1081-
const matchTimeSecondsAndTimeOptional =
1078+
const matchTimeSecondsAndTimeOptional =
10821079
/^(?<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-
10841080

10851081
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-
}
1082+
formats: {
1083+
// This example extends the default time formatter which validates against RFC3339
1084+
time: ({ node, pointer, data }) => {
1085+
const { schema } = node;
1086+
if (typeof data !== "string" || data === "") {
1087+
return undefined;
1088+
}
10991089

1100-
// leap second
1101-
if (matches.groups.second === "60") {
1102-
// Omitted the code here for brevity.
1103-
}
1090+
// Use the Custom Regex to validate the date and time.
1091+
const matches = data.match(matchTimeSecondsAndTimeOptional);
1092+
if (!matches) {
1093+
return node.createError("format-date-time-error", { value: data, pointer, schema });
1094+
}
11041095

1105-
return undefined;
1106-
},
1096+
// leap second
1097+
if (matches.groups.second === "60") {
1098+
// Omitted the code here for brevity.
1099+
}
11071100

1108-
},
1101+
return undefined;
1102+
}
1103+
}
11091104
});
11101105

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");
1106+
const { errors, valid } = compileSchema(
1107+
{
1108+
type: "string",
1109+
format: "time",
1110+
$schema: "https://json-schema.org/draft/2020-12/schema"
1111+
},
1112+
{ drafts: [customTimeFormatDraft] }
1113+
).validate("15:31:12");
11161114

11171115
console.assert(valid, errors.at(0)?.message);
11181116
```

0 commit comments

Comments
 (0)