@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/src/testers.js ADDED
@@ -0,0 +1,232 @@
1
+ //@ts-check
2
+
3
+ /**
4
+ * The canonical format-name -> tester registry.
5
+ *
6
+ * ONE place in the repo binds a JSON Schema `format` name to its predicate
7
+ * from `@jarenjs/core` / `@jarenjs/json`. Everything else derives from it:
8
+ * the validator-facing format compilers of this package (string.js,
9
+ * json.js, number.js pass these testers to their compiler factories) and
10
+ * the per-keystroke field validation of `@jarenjs/forms` (which merges its
11
+ * UI hints over this table). Add a format here first; the consumers pick
12
+ * it up.
13
+ *
14
+ * A tester is a plain synchronous predicate over the value the format
15
+ * applies to: a string for the string/json/datetime groups, a number for
16
+ * the number group. Testers carry no error handling, no schema coupling,
17
+ * no draft semantics - that is the compilers' job.
18
+ *
19
+ * The datetime testers are the boolean twins of the parse-based compilers
20
+ * in datetime.js (which additionally support `formatMinimum` /
21
+ * `formatMaximum`); both sides accept the same strings.
22
+ */
23
+
24
+ import {
25
+ isValidUri,
26
+ isValidUriRef,
27
+ isValidUriTemplate,
28
+ isValidUrl,
29
+ isValidEmail,
30
+ isValidIdnEmail,
31
+ isValidHostname,
32
+ isValidIdnHostname,
33
+ isValidIPv4,
34
+ isValidIPv6,
35
+ isValidUUID,
36
+ isValidGUID,
37
+ isValidAlpha,
38
+ isValidAlphaNumeric,
39
+ isValidIdentifier,
40
+ isValidHtmlIdentifier,
41
+ isValidCssIdentifier,
42
+ isValidHexaDecimal,
43
+ isValidNumeric,
44
+ isValidIRI,
45
+ isValidIRIRef,
46
+ isValidHexColor,
47
+ isValidISBN10,
48
+ isValidISBN13,
49
+ isValidMACAddr,
50
+ isValidBase64,
51
+ isValidCountryAlpha2,
52
+ isValidIBAN,
53
+ isValidIRegexp,
54
+ } from '@jarenjs/core/text';
55
+
56
+ import {
57
+ isStringUpperCase,
58
+ isStringLowerCase,
59
+ isStringRegExp,
60
+ } from '@jarenjs/core/string';
61
+
62
+ import {
63
+ isDateTimeRFC3339,
64
+ isDateOnlyRFC3339,
65
+ isTimeOnlyRFC3339,
66
+ isValidISODateTime,
67
+ isValidISOTime,
68
+ isValidDuration,
69
+ } from '@jarenjs/core/dates';
70
+
71
+ import {
72
+ isValidInt8,
73
+ isValidInt16,
74
+ isValidInt32,
75
+ isValidInt64,
76
+ isValidUInt8,
77
+ isValidUInt16,
78
+ isValidUInt32,
79
+ isValidUInt64,
80
+ } from '@jarenjs/core/integer';
81
+
82
+ import {
83
+ isValidFloat16,
84
+ isValidFloat32,
85
+ isValidFloat64,
86
+ } from '@jarenjs/core/float';
87
+
88
+ import {
89
+ isValidJSONPointer,
90
+ isValidJSONPointerUriFragment,
91
+ isValidRelativeJSONPointer,
92
+ isValidJSONPathStrict,
93
+ isValidJSONPathSegments,
94
+ } from '@jarenjs/json';
95
+
96
+ import {
97
+ isValidGeohash,
98
+ isValidWkt,
99
+ isValidGeoJson,
100
+ } from '@jarenjs/core/geo';
101
+
102
+ /** @typedef {(value: string) => boolean} StringFormatTester */
103
+ /** @typedef {(value: number) => boolean} NumberFormatTester */
104
+
105
+ /**
106
+ * String format testers (the string.js compiler group).
107
+ * @type {Record<string, StringFormatTester>}
108
+ */
109
+ export const stringFormatTesters = {
110
+ // Alphabetic & Case
111
+ 'alpha': isValidAlpha,
112
+ 'alphanumeric': isValidAlphaNumeric,
113
+ 'uppercase': isStringUpperCase,
114
+ 'lowercase': isStringLowerCase,
115
+ // Identifiers
116
+ 'identifier': isValidIdentifier,
117
+ 'html-identifier': isValidHtmlIdentifier,
118
+ 'css-identifier': isValidCssIdentifier,
119
+ // Numeric & Color
120
+ 'hexadecimal': isValidHexaDecimal,
121
+ 'numeric': isValidNumeric,
122
+ 'color': isValidHexColor,
123
+ // Regex
124
+ 'regex': isStringRegExp,
125
+ 'iregexp': isValidIRegexp,
126
+ // URI
127
+ 'uri': isValidUri,
128
+ 'uri-reference': isValidUriRef,
129
+ 'uri-template': isValidUriTemplate,
130
+ 'url': isValidUrl,
131
+ // IRI
132
+ 'iri': isValidIRI,
133
+ 'iri-reference': isValidIRIRef,
134
+ // Email
135
+ 'email': isValidEmail,
136
+ 'idn-email': isValidIdnEmail,
137
+ // Hostname
138
+ 'hostname': isValidHostname,
139
+ 'idn-hostname': isValidIdnHostname,
140
+ // IP Address
141
+ 'ipv4': isValidIPv4,
142
+ 'ipv6': isValidIPv6,
143
+ // UUID & GUID
144
+ 'uuid': isValidUUID,
145
+ 'guid': isValidGUID,
146
+ // ISBN
147
+ 'isbn10': isValidISBN10,
148
+ 'isbn13': isValidISBN13,
149
+ // Hardware Address
150
+ 'mac': isValidMACAddr,
151
+ // Encoding
152
+ 'base64': isValidBase64,
153
+ 'byte': isValidBase64,
154
+ // Country & Banking
155
+ 'country2': isValidCountryAlpha2,
156
+ 'iban': isValidIBAN,
157
+ };
158
+
159
+ /**
160
+ * JSON addressing format testers (the json.js compiler group).
161
+ * @type {Record<string, StringFormatTester>}
162
+ */
163
+ export const jsonFormatTesters = {
164
+ 'json-pointer': isValidJSONPointer,
165
+ 'json-pointer-uri-fragment': isValidJSONPointerUriFragment,
166
+ 'relative-json-pointer': isValidRelativeJSONPointer,
167
+ 'json-path': isValidJSONPathStrict,
168
+ 'json-path-segments': isValidJSONPathSegments,
169
+ };
170
+
171
+ /**
172
+ * Date and time format testers - the boolean twins of the parse-based
173
+ * compilers in datetime.js.
174
+ * @type {Record<string, StringFormatTester>}
175
+ */
176
+ export const dateTimeFormatTesters = {
177
+ 'date-time': isDateTimeRFC3339,
178
+ 'date': isDateOnlyRFC3339,
179
+ 'time': isTimeOnlyRFC3339,
180
+ 'duration': isValidDuration,
181
+ 'iso-date-time': isValidISODateTime,
182
+ 'iso-time': isValidISOTime,
183
+ };
184
+
185
+ /**
186
+ * Geospatial format testers (the geo.js compiler group). `geohash` and
187
+ * `wkt` take strings; `geojson` takes the OBJECT value — the quick
188
+ * yes-or-no twin of the GeoJSON meta-schema artifacts in `@jarenjs/json`,
189
+ * including the ring-closure invariant JSON Schema cannot express.
190
+ * @type {Record<string, (value: any) => boolean>}
191
+ */
192
+ export const geoFormatTesters = {
193
+ 'geohash': isValidGeohash,
194
+ 'wkt': isValidWkt,
195
+ 'geojson': isValidGeoJson,
196
+ };
197
+
198
+ /**
199
+ * Number format testers (the number.js compiler group). These take the
200
+ * NUMBER value, not a string.
201
+ * @type {Record<string, NumberFormatTester>}
202
+ */
203
+ export const numberFormatTesters = {
204
+ // Signed integer types
205
+ 'int8': isValidInt8,
206
+ 'int16': isValidInt16,
207
+ 'int32': isValidInt32,
208
+ 'int64': isValidInt64,
209
+ // Unsigned integer types
210
+ 'uint8': isValidUInt8,
211
+ 'uint16': isValidUInt16,
212
+ 'uint32': isValidUInt32,
213
+ 'uint64': isValidUInt64,
214
+ // Floating point types
215
+ 'float16': isValidFloat16,
216
+ 'float32': isValidFloat32,
217
+ 'float64': isValidFloat64,
218
+ 'float': isValidFloat32,
219
+ 'double': isValidFloat64,
220
+ };
221
+
222
+ /**
223
+ * Every format tester this package knows, in one flat registry.
224
+ * @type {Record<string, (value: any) => boolean>}
225
+ */
226
+ export const formatTesters = {
227
+ ...stringFormatTesters,
228
+ ...jsonFormatTesters,
229
+ ...geoFormatTesters,
230
+ ...dateTimeFormatTesters,
231
+ ...numberFormatTesters,
232
+ };