@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/src/testers.js ADDED
@@ -0,0 +1,216 @@
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
+ isValidUriFull,
27
+ isValidUriRef,
28
+ isValidUriRefFull,
29
+ isValidUriTemplate,
30
+ isValidUrl,
31
+ isValidUrlFull,
32
+ isValidEmail,
33
+ isValidEmailFull,
34
+ isValidIdnEmail,
35
+ isValidHostname,
36
+ isValidIdnHostname,
37
+ isValidIPv4,
38
+ isValidIPv6,
39
+ isValidUUID,
40
+ isValidGUID,
41
+ isValidAlpha,
42
+ isValidAlphaNumeric,
43
+ isValidIdentifier,
44
+ isValidHtmlIdentifier,
45
+ isValidCssIdentifier,
46
+ isValidHexaDecimal,
47
+ isValidNumeric,
48
+ isValidIRI,
49
+ isValidIRIRef,
50
+ isValidHexColor,
51
+ isValidISBN10,
52
+ isValidISBN13,
53
+ isValidMACAddr,
54
+ isValidBase64,
55
+ isValidCountryAlpha2,
56
+ isValidIBAN,
57
+ } from '@jarenjs/core/text';
58
+
59
+ import {
60
+ isStringUpperCase,
61
+ isStringLowerCase,
62
+ isStringRegExp,
63
+ } from '@jarenjs/core/string';
64
+
65
+ import {
66
+ isDateTimeRFC3339,
67
+ isDateOnlyRFC3339,
68
+ isTimeOnlyRFC3339,
69
+ isValidISODateTime,
70
+ isValidISOTime,
71
+ isValidDuration,
72
+ } from '@jarenjs/core/dates';
73
+
74
+ import {
75
+ isValidInt8,
76
+ isValidInt16,
77
+ isValidInt32,
78
+ isValidInt64,
79
+ isValidUInt8,
80
+ isValidUInt16,
81
+ isValidUInt32,
82
+ isValidUInt64,
83
+ } from '@jarenjs/core/integer';
84
+
85
+ import {
86
+ isValidFloat16,
87
+ isValidFloat32,
88
+ isValidFloat64,
89
+ } from '@jarenjs/core/float';
90
+
91
+ import {
92
+ isValidJSONPointer,
93
+ isValidJSONPointerUriFragment,
94
+ isValidRelativeJSONPointer,
95
+ isValidJSONPathStrict,
96
+ } from '@jarenjs/json';
97
+
98
+ /** @typedef {(value: string) => boolean} StringFormatTester */
99
+ /** @typedef {(value: number) => boolean} NumberFormatTester */
100
+
101
+ /**
102
+ * String format testers (the string.js compiler group).
103
+ * @type {Record<string, StringFormatTester>}
104
+ */
105
+ export const stringFormatTesters = {
106
+ // Alphabetic & Case
107
+ 'alpha': isValidAlpha,
108
+ 'alphanumeric': isValidAlphaNumeric,
109
+ 'uppercase': isStringUpperCase,
110
+ 'lowercase': isStringLowerCase,
111
+ // Identifiers
112
+ 'identifier': isValidIdentifier,
113
+ 'html-identifier': isValidHtmlIdentifier,
114
+ 'css-identifier': isValidCssIdentifier,
115
+ // Numeric & Color
116
+ 'hexadecimal': isValidHexaDecimal,
117
+ 'numeric': isValidNumeric,
118
+ 'color': isValidHexColor,
119
+ // Regex
120
+ 'regex': isStringRegExp,
121
+ // URI
122
+ 'uri': isValidUri,
123
+ 'uri--full': isValidUriFull,
124
+ 'uri-reference': isValidUriRef,
125
+ 'uri-reference--full': isValidUriRefFull,
126
+ 'uri-template': isValidUriTemplate,
127
+ 'url': isValidUrl,
128
+ 'url--full': isValidUrlFull,
129
+ // IRI
130
+ 'iri': isValidIRI,
131
+ 'iri-reference': isValidIRIRef,
132
+ // Email
133
+ 'email': isValidEmail,
134
+ 'email--full': isValidEmailFull,
135
+ 'idn-email': isValidIdnEmail,
136
+ // Hostname
137
+ 'hostname': isValidHostname,
138
+ 'idn-hostname': isValidIdnHostname,
139
+ // IP Address
140
+ 'ipv4': isValidIPv4,
141
+ 'ipv6': isValidIPv6,
142
+ // UUID & GUID
143
+ 'uuid': isValidUUID,
144
+ 'guid': isValidGUID,
145
+ // ISBN
146
+ 'isbn10': isValidISBN10,
147
+ 'isbn13': isValidISBN13,
148
+ // Hardware Address
149
+ 'mac': isValidMACAddr,
150
+ // Encoding
151
+ 'base64': isValidBase64,
152
+ 'byte': isValidBase64,
153
+ // Country & Banking
154
+ 'country2': isValidCountryAlpha2,
155
+ 'iban': isValidIBAN,
156
+ };
157
+
158
+ /**
159
+ * JSON addressing format testers (the json.js compiler group).
160
+ * @type {Record<string, StringFormatTester>}
161
+ */
162
+ export const jsonFormatTesters = {
163
+ 'json-pointer': isValidJSONPointer,
164
+ 'json-pointer-uri-fragment': isValidJSONPointerUriFragment,
165
+ 'relative-json-pointer': isValidRelativeJSONPointer,
166
+ 'json-path': isValidJSONPathStrict,
167
+ };
168
+
169
+ /**
170
+ * Date and time format testers - the boolean twins of the parse-based
171
+ * compilers in datetime.js.
172
+ * @type {Record<string, StringFormatTester>}
173
+ */
174
+ export const dateTimeFormatTesters = {
175
+ 'date-time': isDateTimeRFC3339,
176
+ 'date': isDateOnlyRFC3339,
177
+ 'time': isTimeOnlyRFC3339,
178
+ 'duration': isValidDuration,
179
+ 'iso-date-time': isValidISODateTime,
180
+ 'iso-time': isValidISOTime,
181
+ };
182
+
183
+ /**
184
+ * Number format testers (the number.js compiler group). These take the
185
+ * NUMBER value, not a string.
186
+ * @type {Record<string, NumberFormatTester>}
187
+ */
188
+ export const numberFormatTesters = {
189
+ // Signed integer types
190
+ 'int8': isValidInt8,
191
+ 'int16': isValidInt16,
192
+ 'int32': isValidInt32,
193
+ 'int64': isValidInt64,
194
+ // Unsigned integer types
195
+ 'uint8': isValidUInt8,
196
+ 'uint16': isValidUInt16,
197
+ 'uint32': isValidUInt32,
198
+ 'uint64': isValidUInt64,
199
+ // Floating point types
200
+ 'float16': isValidFloat16,
201
+ 'float32': isValidFloat32,
202
+ 'float64': isValidFloat64,
203
+ 'float': isValidFloat32,
204
+ 'double': isValidFloat64,
205
+ };
206
+
207
+ /**
208
+ * Every format tester this package knows, in one flat registry.
209
+ * @type {Record<string, (value: any) => boolean>}
210
+ */
211
+ export const formatTesters = {
212
+ ...stringFormatTesters,
213
+ ...jsonFormatTesters,
214
+ ...dateTimeFormatTesters,
215
+ ...numberFormatTesters,
216
+ };