@jarenjs/formats 0.8.4 → 0.34.0

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 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,144 @@
1
- # Jaren Validator Formats Package
1
+ # @jarenjs/formats
2
2
 
3
- **this is a working package in need of documentation**
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 (`iregexp`, `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`, plus `json-path-segments` for the variable-rooted path strings of the Jaren query format.
6
+
7
+ The geospatial formats are grouped in `geoFormats`: `geohash`, `wkt` and `geojson`, backed by the spatial kernel in `@jarenjs/core/geo`.
8
+
9
+ The name → predicate bindings live in one canonical table, exported as `formatTesters` (plus the per-group `stringFormatTesters`, `jsonFormatTesters`, `geoFormatTesters`, `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.
10
+
11
+ ## Usage
12
+
13
+ ```javascript
14
+ import { JarenValidator } from '@jarenjs/validate';
15
+ import * as formats from '@jarenjs/formats';
16
+
17
+ const jaren = new JarenValidator()
18
+ .addFormats(formats.stringFormats)
19
+ .addFormats(formats.numberFormats)
20
+ .addFormats(formats.dateTimeFormats)
21
+ .addFormats(formats.jsonFormats)
22
+ .addFormats(formats.geoFormats);
23
+
24
+ const validate = jaren.compile({ type: 'string', format: 'json-path' });
25
+ validate('$.store.book[?@.price < 10]'); // true
26
+ ```
27
+
28
+ 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.
29
+
30
+ **Register the group before you use a name from it, and register the right one.** The groups are split, so `date-time` lives in `dateTimeFormats` and *not* in `stringFormats`, and `json-path` lives in `jsonFormats`. Registering only `stringFormats` and then writing `format: 'date-time'` leaves the keyword accepting every value — per spec, an unregistered format is an annotation and asserts nothing, so nothing anywhere reports it. For schemas you own, compile with `new JarenValidator({ unknownFormats: 'error' })`: the missing registration then fails at compile time instead of silently. This repository does that for every schema it ships, gated by `test/validate/our-schema-formats.test.js`.
31
+
32
+ **Register the compilers, not the testers.** `stringFormats` and `formatTesters` are both objects full of functions, but only the *compilers* take `(schemaObj, jsonSchema)` and return the per-value validator; a tester registered in a compiler's place compiles to nothing. That one throws under either `unknownFormats` setting, because it is never intentional. Use `formatTesters` directly — as [`@jarenjs/forms`](../forms) does for per-keystroke field validation — rather than through `addFormats`.
33
+
34
+ ## ✍ The complete format list
35
+
36
+ ### ✍ Formats for strings
37
+
38
+ 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`).
39
+
40
+ #### 🗨 Formats for datetime
41
+
42
+ - `date-time` | according to [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6), time-zone is mandatory
43
+ - `date` | according to [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6), time-zone is mandatory
44
+ - `time` | according to [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6), time-zone is mandatory
45
+
46
+ - `duration` | duration from RFC3339
47
+ - `iso-date-time` | ISO 8601 date-time with optional timezone
48
+ - `iso-time` | ISO 8601 time with optional timezone — the timezone offset is uniformly optional, so a zone-less time such as `12:30:00` validates
49
+
50
+ *Note: All date time formats can use formatMinimum / formatMaximum and formatExclusiveMinimum and formatExclusiveMaximum. The bounds are folded to epoch milliseconds at compile time and string values compare as numbers, so a validation allocates no `Date`; a raw `Date` instance as the value is still accepted and compares numerically.*
51
+
52
+ #### 🗨 Formats for url's, hostnames and emails
53
+
54
+ - `url` | http/https URL — a `uri` narrowed to the web schemes, so it must carry an authority and the RFC 3986 grammar still applies (`http://localhost:8080` and `http://127.0.0.1/` are URLs; `http://x/a|b` is not)
55
+ - `uri` | full URI according to [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986), parsed against the grammar by character code — an ASCII grammar throughout, so a string carrying non-ASCII characters is an `iri` and not a `uri`
56
+ - `uri-reference` | URI reference, absolute or relative, according to [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986)
57
+ - `uri-template` | URI template according to [RFC6570](https://datatracker.ietf.org/doc/html/rfc6570)
58
+ - `iri` | full URI with international characters, according to [RFC3987](https://datatracker.ietf.org/doc/html/rfc3987) — parsed against the grammar by character code, so percent-encoding must be well formed and `iprivate` is accepted in the query only
59
+ - `iri-reference` | full IRI reference, absolute or relative, according to [RFC3987](https://datatracker.ietf.org/doc/html/rfc3987)
60
+
61
+ - `email` | email address according to [RFC5321](https://datatracker.ietf.org/doc/html/rfc5321), including quoted-string local parts and `[192.0.2.1]` / `[IPv6:::1]` address literals
62
+ - `hostname` | host name according to [RFC1034](https://datatracker.ietf.org/doc/html/rfc1034#section-3.5)
63
+ - `idn-hostname` | host name with international characters
64
+ - `idn-email` | email address with international characters
65
+
66
+ #### 🗨 Formats for identifiers
67
+
68
+ - `uuid` | Universally Unique IDentifier according to [RFC4122](https://datatracker.ietf.org/doc/html/rfc4122)
69
+ - `guid` | Globally Unique IDentifier according to Microsoft
70
+
71
+ - `identifier` | C-type identifier
72
+ - `html-identifier` | html element `id` attribute identifier according to [RFC7992](https://datatracker.ietf.org/doc/html/rfc7992#section-5.1)
73
+ - `css-identifier` | css class name identifier according to [RFC7993](https://datatracker.ietf.org/doc/html/rfc7993)
74
+
75
+ - `mac` | ethernet interface identifier (EUI-48) according to [IEEE820](https://en.wikipedia.org/wiki/MAC_address)
76
+ - `ipv4` | IP v4 address according to [RFC791](https://datatracker.ietf.org/doc/html/rfc791)
77
+ - `ipv6` | IP v6 address according to [RFC2460](https://datatracker.ietf.org/doc/html/rfc2460)
78
+
79
+ #### 🗨 Formats for json pointers and paths
80
+
81
+ These are grouped in `jsonFormats`.
82
+
83
+ - `json-pointer` | JSON-pointer according to [RFC6901](https://datatracker.ietf.org/doc/html/rfc6901)
84
+ - `json-pointer-uri-fragment` | JSON-pointer fragment according to [RFC6901](https://datatracker.ietf.org/doc/html/rfc6901#section-6)
85
+ - `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)
86
+ - `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`
87
+ - `json-path-segments` | a variable-rooted path string — `$name` followed by optional [RFC9535](https://www.rfc-editor.org/rfc/rfc9535.html) segments (`$book.price[?@.isbn]`), the form the Jaren query format uses to address a bound variable. Not a valid RFC 9535 query on its own (the RFC's root identifier is `$` alone), so `json-path` rejects it; both formats recognize the five built-in function extensions and no others, because a format has to mean the same thing in every schema
88
+
89
+ #### 🗨 Miscellaneous formats
90
+
91
+ - `alpha` | allow only ASCII alpha characters (a-zA-Z)
92
+ - `numeric` | allow only numeric characters (0-9)
93
+ - `alphanumeric` | allow only ASCII alpha numeric characters
94
+ - `hexadecimal` | allow only hexadecimal characters (0-9a-fA-F)
95
+ - `uppercase` | allow only upper case alpha characters
96
+ - `lowercase` | allow only lower case alpha characters
97
+ - `color` | web color hex string (starts with #, must be 3 or 6 hax characters)
98
+ - `regex` | tests whether a string is a valid regular expression
99
+ - `iregexp` | tests whether a string is a valid I-Regexp according to [RFC9485](https://www.rfc-editor.org/rfc/rfc9485.html) — the interoperable subset that means the same thing in every regexp dialect, so it is stricter than `regex`: shorthand classes (`\d`, `\w`), lazy quantifiers, anchors and lookaround are all rejected
100
+ - `base64` | base64 encoded data
101
+ - `byte` | same as `base64` format
102
+
103
+ - `isbn10` | International Standard Book Number 10 digit number
104
+ - `isbn13` | International Standard Book Number 13 digit number
105
+
106
+ - `country2` | country code by alpha-2 according to [ISO3166-1](https://www.iso.org/iso-3166-country-codes.html) — the 249 assigned codes plus `XK`, the user-assigned code for Kosovo; matched case-insensitively
107
+ - `iban` | International Bank Account Number according to [ISO13616](https://www.iso.org/standard/81090.html) — checks the country's registered length, the alphanumeric body and the ISO 7064 MOD 97-10 check digits, so a transposed digit is caught; accepts both the compact electronic format (`NL91ABNA0417164300`) and the print format grouped in fours (`NL91 ABNA 0417 1643 00`)
108
+
109
+ ### ✍ Geospatial formats
110
+
111
+ These are grouped in `geoFormats`, backed by the spatial kernel in [`@jarenjs/core/geo`](../core).
112
+
113
+ - `geohash` | a base-32 geohash cell name, any length (`u173z`); the alphabet is lowercase and deliberately omits `a`, `i`, `l` and `o`
114
+ - `wkt` | a Well-Known Text geometry (ISO 19125 / OGC Simple Features): the seven tagged types with optional `Z`/`M`/`ZM` modifiers, `EMPTY`, consistent coordinate counts and closed polygon rings; an unmodified tag accepts 2 or 3 coordinates per point, as the field (PostGIS) does
115
+ - `geojson` | a structurally valid GeoJSON object per [RFC 7946](https://datatracker.ietf.org/doc/html/rfc7946) — unlike every other format this one applies to **objects**, and it enforces the invariant JSON Schema provably cannot: every linear ring closed
116
+
117
+ `geojson` exists *next to* the GeoJSON meta-schema artifacts in [`@jarenjs/json`](../json), not instead of them, and the division of labour is deliberate: the format is the one-keyword annotation that answers yes or no in a single call, while the meta-schema locates the failure and (in the `$query`-extended variant) also checks ring winding. Reach for the schema when you want a diagnosis; reach for the format when you only want the gate.
118
+
119
+ ### ✍ Formats for numbers
120
+
121
+ 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.
122
+
123
+ #### 🗨 Formats integer numbers
124
+
125
+ - `int8` | signed 8 bit integer
126
+ - `uint8` | unsigned 8 bit integer
127
+ - `int16` | signed 16 bit integer
128
+ - `uint16` | unsigned 16 bit integer
129
+ - `int32` | signed 32 bit integer
130
+ - `uint32` | unsigned 32 integer
131
+ - `int64` | signed 64 integer
132
+ - `uint64` | unsigned 64 integer
133
+
134
+ #### 🗨 Formats floating point numbers
135
+
136
+ - `float16` | 16 bit floating point number
137
+ - `float32` | 32 bit floating point number
138
+ - `float64` | 64 bit floating point number
139
+ - `float` | 32 bit floating point number
140
+ - `double` | 64 bit floating point number
141
+
142
+ ## Development
143
+
144
+ 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](../../docs/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,77 @@
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 'geohash' format.
23
+ * Validates geohash strings: any length, every character in the
24
+ * base-32 geohash alphabet.
25
+ *
26
+ * @param {ValidationObject} schemaObj - The validation object for error handling and options
27
+ * @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
28
+ * @returns {(data: unknown, dataPath?: string) => boolean} A validator function
29
+ * @example
30
+ * compileGeohashFormat(schemaObj, { format: 'geohash' })('u173z'); // true
31
+ * compileGeohashFormat(schemaObj, { format: 'geohash' })('u17a'); // false ('a' is not in the alphabet)
32
+ */
33
+ export declare const compileGeohashFormat: (schemaObj: import("./string.js").ValidationObject, jsonSchema: import("./string.js").JSONSchema) => (data: unknown, dataPath?: string) => boolean;
34
+ /**
35
+ * Compiles a validator for the 'wkt' format.
36
+ * Validates Well-Known Text geometry strings (ISO 19125 / OGC Simple
37
+ * Features): the seven tagged geometry types with optional Z/M/ZM
38
+ * modifiers, consistent coordinate counts, and closed polygon rings.
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
+ * @example
44
+ * compileWktFormat(schemaObj, { format: 'wkt' })('POINT (4.9041 52.3676)'); // true
45
+ * compileWktFormat(schemaObj, { format: 'wkt' })('POLYGON ((0 0, 4 0, 4 4, 1 1))'); // false (open ring)
46
+ */
47
+ export declare const compileWktFormat: (schemaObj: import("./string.js").ValidationObject, jsonSchema: import("./string.js").JSONSchema) => (data: unknown, dataPath?: string) => boolean;
48
+ /**
49
+ * Compiles a validator for the 'geojson' format. Unlike the string
50
+ * formats, this one applies to **objects**: any non-array object must be
51
+ * a structurally valid GeoJSON object (RFC 7946) — the coordinate
52
+ * nesting its `type` requires, positions inside the WGS 84 bounds, and
53
+ * every linear ring closed. Non-object values pass, following the rule
54
+ * that a format constrains only its own type.
55
+ *
56
+ * This is the quick, shallow judgment; the GeoJSON meta-schema artifacts
57
+ * in `@jarenjs/json` validate the same grammar more thoroughly (locating
58
+ * the failure, and — in the Jaren-extended variant — checking ring
59
+ * winding through `$query`). Reach for the schema when you want to know
60
+ * *what* is wrong; reach for the format when a one-keyword annotation is
61
+ * worth more than a diagnosis.
62
+ *
63
+ * @param {ValidationObject} schemaObj - The validation object for error handling and options
64
+ * @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
65
+ * @returns {(data: unknown, dataPath?: string) => boolean} A validator function
66
+ * @example
67
+ * compileGeoJsonFormat(schemaObj, { format: 'geojson' })({ type: 'Point', coordinates: [4.9, 52.4] }); // true
68
+ * compileGeoJsonFormat(schemaObj, { format: 'geojson' })({ type: 'Polygon', coordinates: [[[0,0],[1,0],[1,1],[2,2]]] }); // false (open ring)
69
+ * compileGeoJsonFormat(schemaObj, { format: 'geojson' })('not an object'); // true (wrong type is not this format's business)
70
+ */
71
+ export declare function compileGeoJsonFormat(schemaObj: ValidationObject, jsonSchema: JSONSchema): (data: unknown, dataPath?: string) => boolean;
72
+ /**
73
+ * Object mapping geospatial format names to their compiler functions.
74
+ *
75
+ * @type {Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>}
76
+ */
77
+ export declare const formatValidators: Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>;
@@ -0,0 +1,23 @@
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 { formatValidators as geoFormats } from './geo.js';
23
+ export { formatTesters, stringFormatTesters, jsonFormatTesters, geoFormatTesters, dateTimeFormatTesters, numberFormatTesters, } from './testers.js';
@@ -0,0 +1,95 @@
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
+ * Compiles a validator for the 'json-path-segments' format.
68
+ * Validates a *variable-rooted* path string: `$name` followed by
69
+ * optional RFC 9535 segments (`$book.price[?@.isbn]`). Such a string is
70
+ * not a valid RFC 9535 query — the RFC's root identifier is `$` alone —
71
+ * so `json-path` would reject it; this format is what gives the Jaren
72
+ * query format's variable-rooted paths the same schema-time
73
+ * well-formedness that absolute paths get from `json-path`.
74
+ *
75
+ * Both formats recognize the five built-in function extensions and no
76
+ * others: a format is a property of the string itself, so it must mean
77
+ * the same thing in every schema regardless of which custom extensions
78
+ * a particular host registered.
79
+ *
80
+ * @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
81
+ * @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
82
+ * @returns {(data: unknown, dataPath?: string) => boolean} A validator function
83
+ * @example
84
+ * compileJsonPathSegmentsFormat(schemaObj, { format: 'json-path-segments' })('$book.price'); // true
85
+ * compileJsonPathSegmentsFormat(schemaObj, { format: 'json-path-segments' })('$book'); // true
86
+ * compileJsonPathSegmentsFormat(schemaObj, { format: 'json-path-segments' })('$.price'); // false (that is json-path)
87
+ * compileJsonPathSegmentsFormat(schemaObj, { format: 'json-path-segments' })('$book.price['); // false
88
+ */
89
+ export declare const compileJsonPathSegmentsFormat: (schemaObj: import("./string.js").ValidationObject, jsonSchema: import("./string.js").JSONSchema) => (data: unknown, dataPath?: string) => boolean;
90
+ /**
91
+ * Object mapping JSON-related format names to their compiler functions.
92
+ *
93
+ * @type {Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>}
94
+ */
95
+ 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>;