@jarenjs/formats 0.8.3 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +128 -2
- package/dist/types/datetime.d.ts +109 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/json.d.ts +71 -0
- package/dist/types/number.d.ts +144 -0
- package/dist/types/string.d.ts +379 -0
- package/dist/types/testers.d.ts +31 -0
- package/package.json +28 -8
- package/src/datetime.js +373 -0
- package/src/index.js +31 -0
- package/src/json.js +90 -0
- package/src/number.js +219 -0
- package/src/string.js +548 -0
- package/src/testers.js +216 -0
- package/dist/index.js +0 -679
- package/dist/index.js.map +0 -7
- package/dist/index.min.js +0 -2
- package/dist/index.min.js.map +0 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Joham
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,129 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @jarenjs/formats
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Format validators for the JSON Schema `format` keyword, built on the text validators of [`@jarenjs/core`](../core). Includes all standard string formats (`date-time`, `date`, `time`, `duration`, `email`, `idn-email`, `hostname`, `idn-hostname`, `ipv4`, `ipv6`, `uri`, `uri-reference`, `uri-template`, `iri`, `iri-reference`, `uuid`, `regex`) plus many extras (`isbn10`, `mac`, `base64`, `alpha`, `color`, ...) and numeric formats (`int8` ... `uint64`, `float16` ... `float64`).
|
|
4
|
+
|
|
5
|
+
The JSON addressing formats are grouped separately in `jsonFormats`: `json-pointer`, `json-pointer-uri-fragment` and `relative-json-pointer` (RFC 6901), and `json-path`, which validates query strings against the complete [RFC 9535](https://www.rfc-editor.org/rfc/rfc9535.html) grammar using the parser of the JSONPath compiler in `@jarenjs/json`.
|
|
6
|
+
|
|
7
|
+
The name → predicate bindings live in one canonical table, exported as `formatTesters` (plus the per-group `stringFormatTesters`, `jsonFormatTesters`, `dateTimeFormatTesters`, `numberFormatTesters`): bare synchronous predicates without validator coupling. The format compilers above wrap these testers in the validator contract, and [`@jarenjs/forms`](../forms) merges its rendering hints over the same table for per-keystroke field validation — one registry, so the two layers can never drift apart.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
import { JarenValidator } from '@jarenjs/validate';
|
|
13
|
+
import * as formats from '@jarenjs/formats';
|
|
14
|
+
|
|
15
|
+
const jaren = new JarenValidator()
|
|
16
|
+
.addFormats(formats.stringFormats)
|
|
17
|
+
.addFormats(formats.numberFormats)
|
|
18
|
+
.addFormats(formats.dateTimeFormats)
|
|
19
|
+
.addFormats(formats.jsonFormats);
|
|
20
|
+
|
|
21
|
+
const validate = jaren.compile({ type: 'string', format: 'json-path' });
|
|
22
|
+
validate('$.store.book[?@.price < 10]'); // true
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Format assertion follows the specification per draft: asserted through draft 2019-09, annotation-only from draft 2020-12 on unless enabled via the `formatAssertion` option (`new JarenValidator({ formatAssertion: true })`) or a metaschema that declares the `format-assertion` vocabulary.
|
|
26
|
+
|
|
27
|
+
## ✍ The complete format list
|
|
28
|
+
|
|
29
|
+
### ✍ Formats for strings
|
|
30
|
+
|
|
31
|
+
These format validators are based on the [json-schema.org](https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats) website. They are grouped in `stringFormats` (with the date/time formats also available separately as `dateTimeFormats`).
|
|
32
|
+
|
|
33
|
+
#### 🗨 Formats for datetime
|
|
34
|
+
|
|
35
|
+
- `date-time` | according to [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6), time-zone is mandatory
|
|
36
|
+
- `date` | according to [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6), time-zone is mandatory
|
|
37
|
+
- `time` | according to [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6), time-zone is mandatory
|
|
38
|
+
|
|
39
|
+
- `duration` | duration from RFC3339
|
|
40
|
+
- `iso-date-time` | ISO 8601 date-time with optional timezone
|
|
41
|
+
- `iso-time` | ISO 8601 time with optional timezone
|
|
42
|
+
|
|
43
|
+
*Note: All date time formats can use formatMinimum / formatMaximum and formatExclusiveMinimum and formatExclusiveMaximum*
|
|
44
|
+
|
|
45
|
+
#### 🗨 Formats for url's, hostnames and emails
|
|
46
|
+
|
|
47
|
+
- `url` | full URL
|
|
48
|
+
- `url--full` | same as `url`, but more comprehensive
|
|
49
|
+
- `uri` | full URI
|
|
50
|
+
- `uri--full` | same as `uri`, but more comprehensive
|
|
51
|
+
- `uri-reference` | URI reference, including full and relative URIs
|
|
52
|
+
- `uri-reference--full` | same as `uri-reference`, but more comprehensive
|
|
53
|
+
- `uri-template` | URI template according to [RFC6570](https://datatracker.ietf.org/doc/html/rfc6570)
|
|
54
|
+
- `iri` | full URI with international characters
|
|
55
|
+
- `iri-reference` | full URI reference with with international characters
|
|
56
|
+
|
|
57
|
+
- `email` | email address
|
|
58
|
+
- `email--full` | same as email, but more comprehensive
|
|
59
|
+
- `hostname` | host name according to [RFC1034](https://datatracker.ietf.org/doc/html/rfc1034#section-3.5)
|
|
60
|
+
- `idn-hostname` | host name with international characters
|
|
61
|
+
- `idn-email` | email address with international characters
|
|
62
|
+
|
|
63
|
+
#### 🗨 Formats for identifiers
|
|
64
|
+
|
|
65
|
+
- `uuid` | Universally Unique IDentifier according to [RFC4122](https://datatracker.ietf.org/doc/html/rfc4122)
|
|
66
|
+
- `guid` | Globally Unique IDentifier according to Microsoft
|
|
67
|
+
|
|
68
|
+
- `identifier` | C-type identifier
|
|
69
|
+
- `html-identifier` | html element `id` attribute identifier according to [RFC7992](https://datatracker.ietf.org/doc/html/rfc7992#section-5.1)
|
|
70
|
+
- `css-identifier` | css class name identifier according to [RFC7993](https://datatracker.ietf.org/doc/html/rfc7993)
|
|
71
|
+
|
|
72
|
+
- `mac` | ethernet interface identifier (EUI-48) according to [IEEE820](https://en.wikipedia.org/wiki/MAC_address)
|
|
73
|
+
- `ipv4` | IP v4 address according to [RFC791](https://datatracker.ietf.org/doc/html/rfc791)
|
|
74
|
+
- `ipv6` | IP v6 address according to [RFC2460](https://datatracker.ietf.org/doc/html/rfc2460)
|
|
75
|
+
|
|
76
|
+
#### 🗨 Formats for json pointers and paths
|
|
77
|
+
|
|
78
|
+
These are grouped in `jsonFormats`.
|
|
79
|
+
|
|
80
|
+
- `json-pointer` | JSON-pointer according to [RFC6901](https://datatracker.ietf.org/doc/html/rfc6901)
|
|
81
|
+
- `json-pointer-uri-fragment` | JSON-pointer fragment according to [RFC6901](https://datatracker.ietf.org/doc/html/rfc6901#section-6)
|
|
82
|
+
- `relative-json-pointer` | relative JSON-pointer according to [draft-luff-relative-json-pointer-00](https://datatracker.ietf.org/doc/html/draft-luff-relative-json-pointer-00)
|
|
83
|
+
- `json-path` | JSONPath query according to [RFC9535](https://www.rfc-editor.org/rfc/rfc9535.html), checked against the complete grammar (including filter well-typedness) by the parser of the JSONPath compiler in `@jarenjs/json`
|
|
84
|
+
|
|
85
|
+
#### 🗨 Miscellaneous formats
|
|
86
|
+
|
|
87
|
+
- `alpha` | allow only ASCII alpha characters (a-zA-Z)
|
|
88
|
+
- `numeric` | allow only numeric characters (0-9)
|
|
89
|
+
- `alphanumeric` | allow only ASCII alpha numeric characters
|
|
90
|
+
- `hexadecimal` | allow only hexadecimal characters (0-9a-fA-F)
|
|
91
|
+
- `uppercase` | allow only upper case alpha characters
|
|
92
|
+
- `lowercase` | allow only lower case alpha characters
|
|
93
|
+
- `color` | web color hex string (starts with #, must be 3 or 6 hax characters)
|
|
94
|
+
- `regex` | tests whether a string is a valid regular expression
|
|
95
|
+
- `base64` | base64 encoded data
|
|
96
|
+
- `byte` | same as `base64` format
|
|
97
|
+
|
|
98
|
+
- `isbn10` | International Standard Book Number 10 digit number
|
|
99
|
+
- `isbn13` | International Standard Book Number 13 digit number
|
|
100
|
+
|
|
101
|
+
- `country2` | Country code by alpha-2 according to ISO3166-1 _!No tests exists!_
|
|
102
|
+
- `iban` | International Bank Account Number _!No tests exists!_
|
|
103
|
+
|
|
104
|
+
### ✍ Formats for numbers
|
|
105
|
+
|
|
106
|
+
These are grouped in `numberFormats`. Formats for numbers validate both numbers and strings as number types; combine them with the `type` keyword (e.g. `{ "type": "integer", "format": "int32" }`) when only real number types should be allowed.
|
|
107
|
+
|
|
108
|
+
#### 🗨 Formats integer numbers
|
|
109
|
+
|
|
110
|
+
- `int8` | signed 8 bit integer
|
|
111
|
+
- `uint8` | unsigned 8 bit integer
|
|
112
|
+
- `int16` | signed 16 bit integer
|
|
113
|
+
- `uint16` | unsigned 16 bit integer
|
|
114
|
+
- `int32` | signed 32 bit integer
|
|
115
|
+
- `uint32` | unsigned 32 integer
|
|
116
|
+
- `int64` | signed 64 integer
|
|
117
|
+
- `uint64` | unsigned 64 integer
|
|
118
|
+
|
|
119
|
+
#### 🗨 Formats floating point numbers
|
|
120
|
+
|
|
121
|
+
- `float16` | 16 bit floating point number
|
|
122
|
+
- `float32` | 32 bit floating point number
|
|
123
|
+
- `float64` | 64 bit floating point number
|
|
124
|
+
- `float` | 32 bit floating point number
|
|
125
|
+
- `double` | 64 bit floating point number
|
|
126
|
+
|
|
127
|
+
## Development
|
|
128
|
+
|
|
129
|
+
Unit tests live in `test/formats/` at the repository root; `test/formats/testers.test.js` enforces that every compiler registry's key set equals its tester group's, so the validator layer and the bare-predicate layer can never drift. The predicates themselves are implemented and tested in [`@jarenjs/core`](../core). See the repository [README](../../README.md) for the monorepo picture and the [ROADMAP](../../ROADMAP.md) for planned formats.
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export type JSONSchema = {
|
|
2
|
+
format?: string;
|
|
3
|
+
formatMinimum?: string;
|
|
4
|
+
formatExclusiveMinimum?: string;
|
|
5
|
+
formatMaximum?: string;
|
|
6
|
+
formatExclusiveMaximum?: string;
|
|
7
|
+
};
|
|
8
|
+
export type ValidationObject = {
|
|
9
|
+
options: {
|
|
10
|
+
skipErrors: boolean;
|
|
11
|
+
};
|
|
12
|
+
createErrorHandler: (expected: any, key: string, ...details: any[]) => (data: any, dataPath?: string) => boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Compiles a validator for the 'date-time' format.
|
|
16
|
+
* Validates date-time strings per RFC 3339 (ISO 8601 profile).
|
|
17
|
+
* Supports formatMinimum, formatMaximum, formatExclusiveMinimum, formatExclusiveMaximum.
|
|
18
|
+
*
|
|
19
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
20
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
21
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
22
|
+
* @example
|
|
23
|
+
* compileDateTimeFormat(schemaObj, { format: 'date-time' })('2024-01-15T12:30:00Z'); // true
|
|
24
|
+
* compileDateTimeFormat(schemaObj, { format: 'date-time' })('2024-01-15T12:30:00+01:00'); // true
|
|
25
|
+
* compileDateTimeFormat(schemaObj, { format: 'date-time' })('invalid'); // false (with error)
|
|
26
|
+
*/
|
|
27
|
+
export declare function compileDateTimeFormat(schemaObj: ValidationObject, jsonSchema: JSONSchema): (data: unknown, dataPath?: string) => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Compiles a validator for the 'date' format.
|
|
30
|
+
* Validates date-only strings (YYYY-MM-DD) per RFC 3339.
|
|
31
|
+
* Supports formatMinimum, formatMaximum, formatExclusiveMinimum, formatExclusiveMaximum.
|
|
32
|
+
*
|
|
33
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
34
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
35
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
36
|
+
* @example
|
|
37
|
+
* compileDateOnlyFormat(schemaObj, { format: 'date' })('2024-01-15'); // true
|
|
38
|
+
* compileDateOnlyFormat(schemaObj, { format: 'date' })('2024-13-45'); // false (with error)
|
|
39
|
+
*/
|
|
40
|
+
export declare function compileDateOnlyFormat(schemaObj: ValidationObject, jsonSchema: JSONSchema): (data: unknown, dataPath?: string) => boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Compiles a validator for the 'time' format.
|
|
43
|
+
* Validates time-only strings (HH:MM:SS or HH:MM:SS.sss) per RFC 3339.
|
|
44
|
+
* Supports formatMinimum, formatMaximum, formatExclusiveMinimum, formatExclusiveMaximum.
|
|
45
|
+
*
|
|
46
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
47
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
48
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
49
|
+
* @example
|
|
50
|
+
* compileTimeOnlyFormat(schemaObj, { format: 'time' })('12:30:00'); // true
|
|
51
|
+
* compileTimeOnlyFormat(schemaObj, { format: 'time' })('12:30:00.123'); // true
|
|
52
|
+
* compileTimeOnlyFormat(schemaObj, { format: 'time' })('25:00:00'); // false (with error)
|
|
53
|
+
*/
|
|
54
|
+
export declare function compileTimeOnlyFormat(schemaObj: ValidationObject, jsonSchema: JSONSchema): (data: unknown, dataPath?: string) => boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Compiles a validator for the 'duration' format.
|
|
57
|
+
* Validates duration strings per RFC 3339.
|
|
58
|
+
* Format: P[n]Y[n]M[n]DT[n]H[n]M[n]S or P[n]W
|
|
59
|
+
* Examples: P1Y2M3DT4H5M6S, P1W, PT1H
|
|
60
|
+
*
|
|
61
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
62
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
63
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
64
|
+
* @example
|
|
65
|
+
* compileDurationFormat(schemaObj, { format: 'duration' })('P1Y2M3DT4H5M6S'); // true
|
|
66
|
+
* compileDurationFormat(schemaObj, { format: 'duration' })('P1W'); // true
|
|
67
|
+
* compileDurationFormat(schemaObj, { format: 'duration' })('PT1H30M'); // true
|
|
68
|
+
* compileDurationFormat(schemaObj, { format: 'duration' })('P'); // false (with error)
|
|
69
|
+
*/
|
|
70
|
+
export declare function compileDurationFormat(schemaObj: ValidationObject, jsonSchema: JSONSchema): (data: unknown, dataPath?: string) => boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Compiles a validator for the 'iso-date-time' format.
|
|
73
|
+
* Validates ISO 8601 date-time strings with optional timezone.
|
|
74
|
+
* Unlike RFC 3339 date-time, the timezone is optional.
|
|
75
|
+
* Supports formatMinimum, formatMaximum, formatExclusiveMinimum, formatExclusiveMaximum.
|
|
76
|
+
*
|
|
77
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
78
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
79
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
80
|
+
* @example
|
|
81
|
+
* compileISODateTimeFormat(schemaObj, { format: 'iso-date-time' })('2024-01-15T12:30:00Z'); // true
|
|
82
|
+
* compileISODateTimeFormat(schemaObj, { format: 'iso-date-time' })('2024-01-15T12:30:00+01:00'); // true
|
|
83
|
+
* compileISODateTimeFormat(schemaObj, { format: 'iso-date-time' })('2024-01-15T12:30:00'); // true (no timezone)
|
|
84
|
+
* compileISODateTimeFormat(schemaObj, { format: 'iso-date-time' })('2024-13-15T12:30:00'); // false (with error)
|
|
85
|
+
*/
|
|
86
|
+
export declare function compileISODateTimeFormat(schemaObj: ValidationObject, jsonSchema: JSONSchema): (data: unknown, dataPath?: string) => boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Compiles a validator for the 'iso-time' format.
|
|
89
|
+
* Validates ISO 8601 time strings with optional timezone.
|
|
90
|
+
* Unlike RFC 3339 time, the timezone is optional.
|
|
91
|
+
* Supports formatMinimum, formatMaximum, formatExclusiveMinimum, formatExclusiveMaximum.
|
|
92
|
+
*
|
|
93
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
94
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
95
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
96
|
+
* @example
|
|
97
|
+
* compileISOTimeFormat(schemaObj, { format: 'iso-time' })('12:30:00Z'); // true
|
|
98
|
+
* compileISOTimeFormat(schemaObj, { format: 'iso-time' })('12:30:00+01:00'); // true
|
|
99
|
+
* compileISOTimeFormat(schemaObj, { format: 'iso-time' })('12:30:00'); // true (no timezone)
|
|
100
|
+
* compileISOTimeFormat(schemaObj, { format: 'iso-time' })('25:00:00'); // false (with error)
|
|
101
|
+
*/
|
|
102
|
+
export declare function compileISOTimeFormat(schemaObj: ValidationObject, jsonSchema: JSONSchema): (data: unknown, dataPath?: string) => boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Object mapping date/time format names to their compiler functions.
|
|
105
|
+
* Used for backward compatibility and aggregate imports.
|
|
106
|
+
*
|
|
107
|
+
* @type {Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>}
|
|
108
|
+
*/
|
|
109
|
+
export declare const formatValidators: Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type FormatValidator = (data: unknown, dataPath?: string) => boolean;
|
|
2
|
+
export type FormatCompiler = (schemaObj: import('./string.js').ValidationObject, jsonSchema: import('./string.js').JSONSchema) => FormatValidator;
|
|
3
|
+
/**
|
|
4
|
+
* A compiled format validator: tests one instance value against the
|
|
5
|
+
* format. `dataPath` is the value's RFC 6901 location, used in error
|
|
6
|
+
* reporting when error collection is enabled.
|
|
7
|
+
* @typedef {(data: unknown, dataPath?: string) => boolean} FormatValidator
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* A format compiler as consumed by the JarenValidator's addFormat and
|
|
11
|
+
* addFormats methods of @jarenjs/validate: called once per schema
|
|
12
|
+
* location at compile time with the compiling validation object and the
|
|
13
|
+
* schema declaring the format, it returns the FormatValidator invoked for
|
|
14
|
+
* each instance value. Typed structurally so @jarenjs/formats stays free
|
|
15
|
+
* of a dependency on @jarenjs/validate.
|
|
16
|
+
* @typedef {(schemaObj: import('./string.js').ValidationObject, jsonSchema: import('./string.js').JSONSchema) => FormatValidator} FormatCompiler
|
|
17
|
+
*/
|
|
18
|
+
export { formatValidators as dateTimeFormats } from './datetime.js';
|
|
19
|
+
export { formatValidators as stringFormats } from './string.js';
|
|
20
|
+
export { formatValidators as numberFormats } from './number.js';
|
|
21
|
+
export { formatValidators as jsonFormats } from './json.js';
|
|
22
|
+
export { formatTesters, stringFormatTesters, jsonFormatTesters, dateTimeFormatTesters, numberFormatTesters, } from './testers.js';
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export type JSONSchema = {
|
|
2
|
+
format?: string;
|
|
3
|
+
formatMinimum?: string;
|
|
4
|
+
formatExclusiveMinimum?: string;
|
|
5
|
+
formatMaximum?: string;
|
|
6
|
+
formatExclusiveMaximum?: string;
|
|
7
|
+
};
|
|
8
|
+
export type ValidationObject = {
|
|
9
|
+
options: {
|
|
10
|
+
skipErrors: boolean;
|
|
11
|
+
};
|
|
12
|
+
createErrorHandler: (expected: any, key: string, ...details: any[]) => (data: any, dataPath?: string) => boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {{format?: string, formatMinimum?: string, formatExclusiveMinimum?: string, formatMaximum?: string, formatExclusiveMaximum?: string}} JSONSchema
|
|
16
|
+
* @typedef {{
|
|
17
|
+
* options: {skipErrors: boolean},
|
|
18
|
+
* createErrorHandler: (expected: any, key: string, ...details: any[]) => (data: any, dataPath?: string) => boolean
|
|
19
|
+
* }} ValidationObject
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Compiles a validator for the 'json-pointer' format.
|
|
23
|
+
* Validates JSON Pointer strings per RFC 6901.
|
|
24
|
+
*
|
|
25
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
26
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
27
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
28
|
+
*/
|
|
29
|
+
export declare const compileJsonPointerFormat: (schemaObj: import("./string.js").ValidationObject, jsonSchema: import("./string.js").JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Compiles a validator for the 'json-pointer-uri-fragment' format.
|
|
32
|
+
* Validates JSON Pointer URI fragment strings (e.g., #/foo/bar).
|
|
33
|
+
*
|
|
34
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
35
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
36
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
37
|
+
*/
|
|
38
|
+
export declare const compileJsonPointerUriFragmentFormat: (schemaObj: import("./string.js").ValidationObject, jsonSchema: import("./string.js").JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Compiles a validator for the 'relative-json-pointer' format.
|
|
41
|
+
* Validates Relative JSON Pointer strings.
|
|
42
|
+
*
|
|
43
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
44
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
45
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
46
|
+
*/
|
|
47
|
+
export declare const compileRelativeJsonPointerFormat: (schemaObj: import("./string.js").ValidationObject, jsonSchema: import("./string.js").JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Compiles a validator for the 'json-path' format.
|
|
50
|
+
* Validates JSONPath query expressions strictly against the complete
|
|
51
|
+
* RFC 9535 grammar, using the parser of the JSONPath compiler in
|
|
52
|
+
* `@jarenjs/json`. This includes the well-typedness rules for
|
|
53
|
+
* function expressions, so queries like `$[?length(@)]` or comparisons
|
|
54
|
+
* against non-singular queries are rejected.
|
|
55
|
+
*
|
|
56
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
57
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
58
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
59
|
+
* @example
|
|
60
|
+
* compileJsonPathFormat(schemaObj, { format: 'json-path' })('$.store.book[0].title'); // true
|
|
61
|
+
* compileJsonPathFormat(schemaObj, { format: 'json-path' })("$..book[?@.price < 10]"); // true
|
|
62
|
+
* compileJsonPathFormat(schemaObj, { format: 'json-path' })('@.name'); // false (queries start at $)
|
|
63
|
+
* compileJsonPathFormat(schemaObj, { format: 'json-path' })('$.foo '); // false (trailing whitespace)
|
|
64
|
+
*/
|
|
65
|
+
export declare const compileJsonPathFormat: (schemaObj: import("./string.js").ValidationObject, jsonSchema: import("./string.js").JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Object mapping JSON-related format names to their compiler functions.
|
|
68
|
+
*
|
|
69
|
+
* @type {Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>}
|
|
70
|
+
*/
|
|
71
|
+
export declare const formatValidators: Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
export type JSONSchema = {
|
|
2
|
+
format?: string;
|
|
3
|
+
formatMinimum?: string;
|
|
4
|
+
formatExclusiveMinimum?: string;
|
|
5
|
+
formatMaximum?: string;
|
|
6
|
+
formatExclusiveMaximum?: string;
|
|
7
|
+
};
|
|
8
|
+
export type ValidationObject = {
|
|
9
|
+
options: {
|
|
10
|
+
skipErrors: boolean;
|
|
11
|
+
};
|
|
12
|
+
createErrorHandler: (expected: any, key: string, ...details: any[]) => (data: any, dataPath?: string) => boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Compiles a validator for the 'int8' format.
|
|
16
|
+
* Validates 8-bit signed integers (-128 to 127).
|
|
17
|
+
*
|
|
18
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
19
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
20
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
21
|
+
* @example
|
|
22
|
+
* compileInt8Format(schemaObj, { format: 'int8' })(127); // true
|
|
23
|
+
* compileInt8Format(schemaObj, { format: 'int8' })(128); // false (with error)
|
|
24
|
+
* compileInt8Format(schemaObj, { format: 'int8' })('64'); // true (coerced)
|
|
25
|
+
*/
|
|
26
|
+
export declare const compileInt8Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Compiles a validator for the 'int16' format.
|
|
29
|
+
* Validates 16-bit signed integers (-32768 to 32767).
|
|
30
|
+
*
|
|
31
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
32
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
33
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
34
|
+
*/
|
|
35
|
+
export declare const compileInt16Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Compiles a validator for the 'int32' format.
|
|
38
|
+
* Validates 32-bit signed integers (-2147483648 to 2147483647).
|
|
39
|
+
*
|
|
40
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
41
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
42
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
43
|
+
*/
|
|
44
|
+
export declare const compileInt32Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Compiles a validator for the 'int64' format.
|
|
47
|
+
* Validates 64-bit signed integers (approximate range in JavaScript).
|
|
48
|
+
*
|
|
49
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
50
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
51
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
52
|
+
*/
|
|
53
|
+
export declare const compileInt64Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Compiles a validator for the 'uint8' format.
|
|
56
|
+
* Validates 8-bit unsigned integers (0 to 255).
|
|
57
|
+
*
|
|
58
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
59
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
60
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
61
|
+
* @example
|
|
62
|
+
* compileUInt8Format(schemaObj, { format: 'uint8' })(255); // true
|
|
63
|
+
* compileUInt8Format(schemaObj, { format: 'uint8' })(256); // false (with error)
|
|
64
|
+
*/
|
|
65
|
+
export declare const compileUInt8Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Compiles a validator for the 'uint16' format.
|
|
68
|
+
* Validates 16-bit unsigned integers (0 to 65535).
|
|
69
|
+
*
|
|
70
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
71
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
72
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
73
|
+
*/
|
|
74
|
+
export declare const compileUInt16Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Compiles a validator for the 'uint32' format.
|
|
77
|
+
* Validates 32-bit unsigned integers (0 to 4294967295).
|
|
78
|
+
*
|
|
79
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
80
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
81
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
82
|
+
*/
|
|
83
|
+
export declare const compileUInt32Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Compiles a validator for the 'uint64' format.
|
|
86
|
+
* Validates 64-bit unsigned integers (approximate range in JavaScript).
|
|
87
|
+
*
|
|
88
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
89
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
90
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
91
|
+
*/
|
|
92
|
+
export declare const compileUInt64Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Compiles a validator for the 'float16' format.
|
|
95
|
+
* Validates 16-bit floating point numbers (IEEE 754 half-precision).
|
|
96
|
+
*
|
|
97
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
98
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
99
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
100
|
+
*/
|
|
101
|
+
export declare const compileFloat16Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Compiles a validator for the 'float32' format.
|
|
104
|
+
* Validates 32-bit floating point numbers (IEEE 754 single-precision).
|
|
105
|
+
*
|
|
106
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
107
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
108
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
109
|
+
*/
|
|
110
|
+
export declare const compileFloat32Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Compiles a validator for the 'float64' format.
|
|
113
|
+
* Validates 64-bit floating point numbers (IEEE 754 double-precision).
|
|
114
|
+
*
|
|
115
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
116
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
117
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
118
|
+
*/
|
|
119
|
+
export declare const compileFloat64Format: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Compiles a validator for the 'float' format.
|
|
122
|
+
* Alias for 'float32' - validates 32-bit floating point numbers.
|
|
123
|
+
*
|
|
124
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
125
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
126
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
127
|
+
*/
|
|
128
|
+
export declare const compileFloatFormat: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Compiles a validator for the 'double' format.
|
|
131
|
+
* Alias for 'float64' - validates 64-bit floating point numbers.
|
|
132
|
+
*
|
|
133
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
134
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
135
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
136
|
+
*/
|
|
137
|
+
export declare const compileDoubleFormat: (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Object mapping number format names to their compiler functions.
|
|
140
|
+
* Used for backward compatibility and aggregate imports.
|
|
141
|
+
*
|
|
142
|
+
* @type {Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>}
|
|
143
|
+
*/
|
|
144
|
+
export declare const formatValidators: Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>;
|