@railway-ts/pipelines 0.1.29 → 0.2.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/README.md +4 -4
- package/dist/composition/index.d.mts +367 -0
- package/dist/composition/index.mjs +37 -59
- package/dist/composition/index.mjs.map +1 -1
- package/dist/{result/index.d.ts → index-DDkNG41b.d.mts} +63 -138
- package/dist/option/index.d.mts +7 -0
- package/dist/option/index.mjs +1 -116
- package/dist/option-BQCaaEBP.mjs +909 -0
- package/dist/option-BQCaaEBP.mjs.map +1 -0
- package/dist/result/index.d.mts +7 -0
- package/dist/result/index.mjs +1 -187
- package/dist/schema/{index.d.ts → index.d.mts} +227 -258
- package/dist/schema/index.mjs +1646 -982
- package/dist/schema/index.mjs.map +1 -1
- package/docs/GETTING_STARTED.md +1 -1
- package/docs/RECIPES.md +3 -3
- package/package.json +21 -70
- package/dist/composition/index.cjs +0 -94
- package/dist/composition/index.cjs.map +0 -1
- package/dist/composition/index.d.cts +0 -372
- package/dist/composition/index.d.ts +0 -372
- package/dist/option/index.cjs +0 -139
- package/dist/option/index.cjs.map +0 -1
- package/dist/option/index.d.cts +0 -1
- package/dist/option/index.d.ts +0 -1
- package/dist/option/index.mjs.map +0 -1
- package/dist/result/index.cjs +0 -221
- package/dist/result/index.cjs.map +0 -1
- package/dist/result/index.d.cts +0 -1062
- package/dist/result/index.mjs.map +0 -1
- package/dist/schema/index.cjs +0 -1169
- package/dist/schema/index.cjs.map +0 -1
- package/dist/schema/index.d.cts +0 -1265
package/dist/schema/index.cjs
DELETED
|
@@ -1,1169 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// src/result/result.ts
|
|
4
|
-
var RESULT_BRAND = /* @__PURE__ */ Symbol("RESULT_BRAND");
|
|
5
|
-
function ok(value) {
|
|
6
|
-
return {
|
|
7
|
-
ok: true,
|
|
8
|
-
value,
|
|
9
|
-
[RESULT_BRAND]: "ok"
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
function err(error) {
|
|
13
|
-
return { ok: false, error, [RESULT_BRAND]: "error" };
|
|
14
|
-
}
|
|
15
|
-
function isOk(result) {
|
|
16
|
-
return result.ok;
|
|
17
|
-
}
|
|
18
|
-
function isErr(result) {
|
|
19
|
-
return !result.ok;
|
|
20
|
-
}
|
|
21
|
-
function mapErr(result, fn) {
|
|
22
|
-
return result.ok ? result : err(fn(result.error));
|
|
23
|
-
}
|
|
24
|
-
function match(result, patterns) {
|
|
25
|
-
if (isOk(result)) {
|
|
26
|
-
const okFn = patterns.ok;
|
|
27
|
-
return okFn(result.value);
|
|
28
|
-
} else {
|
|
29
|
-
const errFn = patterns.err;
|
|
30
|
-
return errFn(result.error);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function fromTry(f) {
|
|
34
|
-
try {
|
|
35
|
-
return ok(f());
|
|
36
|
-
} catch (error) {
|
|
37
|
-
return err(error instanceof Error ? error.message : String(error));
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/schema/core.ts
|
|
42
|
-
var _abortEarly = false;
|
|
43
|
-
function _getAbortEarly() {
|
|
44
|
-
return _abortEarly;
|
|
45
|
-
}
|
|
46
|
-
function _withAbortEarly(enabled, fn) {
|
|
47
|
-
const prev = _abortEarly;
|
|
48
|
-
_abortEarly = enabled;
|
|
49
|
-
try {
|
|
50
|
-
return fn();
|
|
51
|
-
} finally {
|
|
52
|
-
_abortEarly = prev;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
function object(schema, { strict = true } = {}) {
|
|
56
|
-
return (obj, parentPath = []) => {
|
|
57
|
-
if (obj === null || typeof obj !== "object") {
|
|
58
|
-
return err([{ path: parentPath, message: "Expected an object" }]);
|
|
59
|
-
}
|
|
60
|
-
const abortEarly = _getAbortEarly();
|
|
61
|
-
const allErrors = [];
|
|
62
|
-
const validatedObj = {};
|
|
63
|
-
if (strict !== false) {
|
|
64
|
-
const extraKeys = Object.keys(obj).filter((key) => !Object.prototype.hasOwnProperty.call(schema, key));
|
|
65
|
-
if (extraKeys.length > 0) {
|
|
66
|
-
if (abortEarly) {
|
|
67
|
-
return err([{ path: [...parentPath, extraKeys[0]], message: `Unexpected field: '${extraKeys[0]}'` }]);
|
|
68
|
-
}
|
|
69
|
-
for (const key of extraKeys) {
|
|
70
|
-
allErrors.push({
|
|
71
|
-
path: [...parentPath, key],
|
|
72
|
-
message: `Unexpected field: '${key}'`
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
if (abortEarly && allErrors.length > 0) return err(allErrors);
|
|
78
|
-
const pendingResults = [];
|
|
79
|
-
for (const key in schema) {
|
|
80
|
-
if (!Object.prototype.hasOwnProperty.call(schema, key)) continue;
|
|
81
|
-
const validator = schema[key];
|
|
82
|
-
if (!validator) continue;
|
|
83
|
-
if (abortEarly && allErrors.length > 0) break;
|
|
84
|
-
const value = obj[key];
|
|
85
|
-
const fieldPath = [...parentPath, key];
|
|
86
|
-
const result = validator(value, fieldPath);
|
|
87
|
-
if (result instanceof Promise) {
|
|
88
|
-
if (abortEarly && allErrors.length > 0) continue;
|
|
89
|
-
pendingResults.push(
|
|
90
|
-
result.then((r) => {
|
|
91
|
-
if (isOk(r)) {
|
|
92
|
-
validatedObj[key] = r.value;
|
|
93
|
-
} else {
|
|
94
|
-
allErrors.push(...r.error);
|
|
95
|
-
}
|
|
96
|
-
})
|
|
97
|
-
);
|
|
98
|
-
} else if (isOk(result)) {
|
|
99
|
-
validatedObj[key] = result.value;
|
|
100
|
-
} else {
|
|
101
|
-
allErrors.push(...result.error);
|
|
102
|
-
if (abortEarly) break;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (pendingResults.length > 0) {
|
|
106
|
-
return Promise.all(pendingResults).then(() => {
|
|
107
|
-
if (allErrors.length > 0) return err(allErrors);
|
|
108
|
-
return ok(validatedObj);
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
if (allErrors.length > 0) return err(allErrors);
|
|
112
|
-
return ok(validatedObj);
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function required(validator, message = "Field is required") {
|
|
116
|
-
return (value, path = []) => {
|
|
117
|
-
if (value === void 0 || value === null) {
|
|
118
|
-
return err([{ path, message }]);
|
|
119
|
-
}
|
|
120
|
-
return validator(value, path);
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
function optional(validator) {
|
|
124
|
-
return (value, path = []) => {
|
|
125
|
-
if (value === void 0 || value === null) {
|
|
126
|
-
return ok(void 0);
|
|
127
|
-
}
|
|
128
|
-
return validator(value, path);
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
function nullable(message = "Must be null") {
|
|
132
|
-
return (value, path = []) => {
|
|
133
|
-
if (value !== null) {
|
|
134
|
-
return err([{ path, message }]);
|
|
135
|
-
}
|
|
136
|
-
return ok(null);
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
function emptyAsOptional(validator) {
|
|
140
|
-
return (value, path = []) => {
|
|
141
|
-
if (value === "" || Array.isArray(value) && value.length === 0 || typeof value === "object" && value !== null && Object.keys(value).length === 0) {
|
|
142
|
-
return ok(void 0);
|
|
143
|
-
}
|
|
144
|
-
return optional(validator)(value, path);
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
function literal(expectedValue, message = `Value must be '${String(expectedValue)}'`) {
|
|
148
|
-
return (value, path = []) => {
|
|
149
|
-
if (value !== expectedValue) {
|
|
150
|
-
return err([{ path, message }]);
|
|
151
|
-
}
|
|
152
|
-
return ok(expectedValue);
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// src/schema/utils.ts
|
|
157
|
-
var ROOT_ERROR_KEY = "_root";
|
|
158
|
-
function chain(...validators) {
|
|
159
|
-
return (value, path = []) => {
|
|
160
|
-
let result = ok(value);
|
|
161
|
-
for (const validator of validators) {
|
|
162
|
-
if (isErr(result)) return result;
|
|
163
|
-
const nextResult = validator(result.value, path);
|
|
164
|
-
result = isErr(nextResult) ? nextResult : ok(nextResult.value);
|
|
165
|
-
}
|
|
166
|
-
return result;
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
function chainAsync(...validators) {
|
|
170
|
-
return async (value, path = []) => {
|
|
171
|
-
let current = value;
|
|
172
|
-
for (const validator of validators) {
|
|
173
|
-
const result = await validator(current, path);
|
|
174
|
-
if (isErr(result)) return result;
|
|
175
|
-
current = result.value;
|
|
176
|
-
}
|
|
177
|
-
return ok(current);
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
function validate(value, validator, pathOrOptions = [], maybeOptions) {
|
|
181
|
-
const path = Array.isArray(pathOrOptions) ? pathOrOptions : [];
|
|
182
|
-
const options = Array.isArray(pathOrOptions) ? maybeOptions : pathOrOptions;
|
|
183
|
-
if (options?.abortEarly) {
|
|
184
|
-
return _withAbortEarly(true, () => validator(value, path));
|
|
185
|
-
}
|
|
186
|
-
return validator(value, path);
|
|
187
|
-
}
|
|
188
|
-
function is(value, validator) {
|
|
189
|
-
const result = _withAbortEarly(true, () => validator(value));
|
|
190
|
-
if (result instanceof Promise) {
|
|
191
|
-
return result.then((r) => r.ok);
|
|
192
|
-
}
|
|
193
|
-
return result.ok;
|
|
194
|
-
}
|
|
195
|
-
function formatErrors(errors) {
|
|
196
|
-
return errors.reduce(
|
|
197
|
-
(acc, error) => {
|
|
198
|
-
const formattedPath = error.path.reduce((path, segment, index) => {
|
|
199
|
-
if (/^\d+$/.test(segment)) {
|
|
200
|
-
return `${path}[${segment}]`;
|
|
201
|
-
} else {
|
|
202
|
-
return index === 0 ? segment : `${path}.${segment}`;
|
|
203
|
-
}
|
|
204
|
-
}, "");
|
|
205
|
-
acc[formattedPath || ROOT_ERROR_KEY] = error.message;
|
|
206
|
-
return acc;
|
|
207
|
-
},
|
|
208
|
-
{}
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
function validateAndFormatResult(input, schema, options) {
|
|
212
|
-
const run = () => {
|
|
213
|
-
const result = schema(input);
|
|
214
|
-
if (result instanceof Promise) {
|
|
215
|
-
return result.then(
|
|
216
|
-
(r) => match(r, {
|
|
217
|
-
ok: (data) => ({ valid: true, data }),
|
|
218
|
-
err: (errors) => ({ valid: false, errors: formatErrors(errors) })
|
|
219
|
-
})
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
return match(result, {
|
|
223
|
-
ok: (data) => ({ valid: true, data }),
|
|
224
|
-
err: (errors) => ({ valid: false, errors: formatErrors(errors) })
|
|
225
|
-
});
|
|
226
|
-
};
|
|
227
|
-
if (options?.abortEarly) {
|
|
228
|
-
return _withAbortEarly(true, run);
|
|
229
|
-
}
|
|
230
|
-
return run();
|
|
231
|
-
}
|
|
232
|
-
function transform(transformer) {
|
|
233
|
-
return (value) => ok(transformer(value));
|
|
234
|
-
}
|
|
235
|
-
function refine(predicate, message = "Custom validation failed") {
|
|
236
|
-
return (value, path = []) => {
|
|
237
|
-
if (!predicate(value)) {
|
|
238
|
-
return err([{ path, message }]);
|
|
239
|
-
}
|
|
240
|
-
return ok(value);
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
function refineAsync(predicate, message = "Custom validation failed") {
|
|
244
|
-
return async (value, path = []) => {
|
|
245
|
-
if (!await predicate(value)) {
|
|
246
|
-
return err([{ path, message }]);
|
|
247
|
-
}
|
|
248
|
-
return ok(value);
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
function refineAt(targetPath, predicate, message) {
|
|
252
|
-
return (value, parentPath = []) => {
|
|
253
|
-
if (!predicate(value)) {
|
|
254
|
-
const fieldPath = Array.isArray(targetPath) ? targetPath : [targetPath];
|
|
255
|
-
return err([{ path: [...parentPath, ...fieldPath], message }]);
|
|
256
|
-
}
|
|
257
|
-
return ok(value);
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
function refineAtAsync(targetPath, predicate, message) {
|
|
261
|
-
return async (value, parentPath = []) => {
|
|
262
|
-
if (!await predicate(value)) {
|
|
263
|
-
const fieldPath = Array.isArray(targetPath) ? targetPath : [targetPath];
|
|
264
|
-
return err([{ path: [...parentPath, ...fieldPath], message }]);
|
|
265
|
-
}
|
|
266
|
-
return ok(value);
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
function when(predicate, thenValidator, elseValidator) {
|
|
270
|
-
return (value, path = []) => {
|
|
271
|
-
if (predicate(value)) {
|
|
272
|
-
return thenValidator(value, path);
|
|
273
|
-
}
|
|
274
|
-
return elseValidator ? elseValidator(value, path) : ok(value);
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
function whenAsync(predicate, thenValidator, elseValidator) {
|
|
278
|
-
return async (value, path = []) => {
|
|
279
|
-
if (predicate(value)) {
|
|
280
|
-
return thenValidator(value, path);
|
|
281
|
-
}
|
|
282
|
-
return elseValidator ? elseValidator(value, path) : ok(value);
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// src/schema/string.ts
|
|
287
|
-
function string(message = "Must be a string") {
|
|
288
|
-
return (value, path = []) => {
|
|
289
|
-
if (typeof value !== "string") {
|
|
290
|
-
return err([{ path, message }]);
|
|
291
|
-
}
|
|
292
|
-
return ok(value);
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
function minLength(min2, message = `Must be at least ${min2} characters`) {
|
|
296
|
-
return (value, path = []) => {
|
|
297
|
-
if (value.length < min2) {
|
|
298
|
-
return err([{ path, message }]);
|
|
299
|
-
}
|
|
300
|
-
return ok(value);
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
function maxLength(max2, message = `Must be at most ${max2} characters`) {
|
|
304
|
-
return (value, path = []) => {
|
|
305
|
-
if (value.length > max2) {
|
|
306
|
-
return err([{ path, message }]);
|
|
307
|
-
}
|
|
308
|
-
return ok(value);
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
function pattern(regex, message = "Invalid format") {
|
|
312
|
-
return (value, path = []) => {
|
|
313
|
-
if (!regex.test(value)) {
|
|
314
|
-
return err([{ path, message }]);
|
|
315
|
-
}
|
|
316
|
-
return ok(value);
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
function nonEmpty(message = "Field must not be empty") {
|
|
320
|
-
return (value, path = []) => {
|
|
321
|
-
if (value.trim().length === 0) {
|
|
322
|
-
return err([{ path, message }]);
|
|
323
|
-
}
|
|
324
|
-
return ok(value);
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
function email(message = "Invalid email format") {
|
|
328
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
329
|
-
return pattern(emailRegex, message);
|
|
330
|
-
}
|
|
331
|
-
function url(message = "Invalid URL format") {
|
|
332
|
-
return (value, path = []) => {
|
|
333
|
-
try {
|
|
334
|
-
new URL(value);
|
|
335
|
-
return ok(value);
|
|
336
|
-
} catch {
|
|
337
|
-
return err([{ path, message }]);
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
function phoneNumber(message = "Invalid phone number format") {
|
|
342
|
-
const phoneRegex = /^\+?[\d\-()]{8,20}$/;
|
|
343
|
-
return (value, path = []) => {
|
|
344
|
-
const cleanedValue = value.replaceAll(/\s+/g, "");
|
|
345
|
-
if (!phoneRegex.test(cleanedValue) || cleanedValue.replaceAll(/\D/g, "").length < 7) {
|
|
346
|
-
return err([{ path, message }]);
|
|
347
|
-
}
|
|
348
|
-
return ok(value);
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
function uuid(message = "Invalid UUID format") {
|
|
352
|
-
const uuidRegex = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i;
|
|
353
|
-
return pattern(uuidRegex, message);
|
|
354
|
-
}
|
|
355
|
-
function isoDate(message = "Invalid ISO date format") {
|
|
356
|
-
const isoDateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
357
|
-
return (value, path = []) => {
|
|
358
|
-
if (!isoDateRegex.test(value)) {
|
|
359
|
-
return err([{ path, message }]);
|
|
360
|
-
}
|
|
361
|
-
const date2 = /* @__PURE__ */ new Date(value + "T00:00:00Z");
|
|
362
|
-
if (Number.isNaN(date2.getTime()) || date2.toISOString().slice(0, 10) !== value) {
|
|
363
|
-
return err([{ path, message }]);
|
|
364
|
-
}
|
|
365
|
-
return ok(value);
|
|
366
|
-
};
|
|
367
|
-
}
|
|
368
|
-
function isoTime(message = "Invalid ISO time format") {
|
|
369
|
-
const isoTimeRegex = /^(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|([+-])(\d{2}):(\d{2}))?$/;
|
|
370
|
-
return (value, path = []) => {
|
|
371
|
-
const match2 = isoTimeRegex.exec(value);
|
|
372
|
-
if (!match2) {
|
|
373
|
-
return err([{ path, message }]);
|
|
374
|
-
}
|
|
375
|
-
const hours = Number(match2[1]);
|
|
376
|
-
const minutes = Number(match2[2]);
|
|
377
|
-
const seconds = Number(match2[3]);
|
|
378
|
-
if (hours > 23 || minutes > 59 || seconds > 59) {
|
|
379
|
-
return err([{ path, message }]);
|
|
380
|
-
}
|
|
381
|
-
if (match2[4]) {
|
|
382
|
-
const tzHours = Number(match2[5]);
|
|
383
|
-
const tzMinutes = Number(match2[6]);
|
|
384
|
-
if (tzHours > 23 || tzMinutes > 59) {
|
|
385
|
-
return err([{ path, message }]);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
return ok(value);
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
function isoDateTime(message = "Invalid ISO datetime format") {
|
|
392
|
-
const isoDateTimeRegex = /^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|([+-])(\d{2}):(\d{2}))?$/;
|
|
393
|
-
return (value, path = []) => {
|
|
394
|
-
const match2 = isoDateTimeRegex.exec(value);
|
|
395
|
-
if (!match2) {
|
|
396
|
-
return err([{ path, message }]);
|
|
397
|
-
}
|
|
398
|
-
const datePart = match2[1];
|
|
399
|
-
const date2 = /* @__PURE__ */ new Date(datePart + "T00:00:00Z");
|
|
400
|
-
if (Number.isNaN(date2.getTime()) || date2.toISOString().slice(0, 10) !== datePart) {
|
|
401
|
-
return err([{ path, message }]);
|
|
402
|
-
}
|
|
403
|
-
const hours = Number(match2[2]);
|
|
404
|
-
const minutes = Number(match2[3]);
|
|
405
|
-
const seconds = Number(match2[4]);
|
|
406
|
-
if (hours > 23 || minutes > 59 || seconds > 59) {
|
|
407
|
-
return err([{ path, message }]);
|
|
408
|
-
}
|
|
409
|
-
if (match2[5]) {
|
|
410
|
-
const tzHours = Number(match2[6]);
|
|
411
|
-
const tzMinutes = Number(match2[7]);
|
|
412
|
-
if (tzHours > 23 || tzMinutes > 59) {
|
|
413
|
-
return err([{ path, message }]);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
return ok(value);
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
function isoDuration(message = "Invalid ISO duration format") {
|
|
420
|
-
const isoDurationRegex = /^P(?:\d+W|(?:\d+Y)?(?:\d+M)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?)$/;
|
|
421
|
-
return (value, path = []) => {
|
|
422
|
-
if (!isoDurationRegex.test(value) || value === "P" || value.endsWith("T")) {
|
|
423
|
-
return err([{ path, message }]);
|
|
424
|
-
}
|
|
425
|
-
return ok(value);
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
// src/schema/array.ts
|
|
430
|
-
function array(itemValidator) {
|
|
431
|
-
return (value, parentPath = []) => {
|
|
432
|
-
if (!Array.isArray(value)) {
|
|
433
|
-
return err([{ path: parentPath, message: "Expected an array" }]);
|
|
434
|
-
}
|
|
435
|
-
const abortEarly = _getAbortEarly();
|
|
436
|
-
const allErrors = [];
|
|
437
|
-
const validatedItems = Array.from({ length: value.length });
|
|
438
|
-
const pendingResults = [];
|
|
439
|
-
for (const [i, item] of value.entries()) {
|
|
440
|
-
if (abortEarly && allErrors.length > 0) break;
|
|
441
|
-
const itemPath = [...parentPath, i.toString()];
|
|
442
|
-
const result = itemValidator(item, itemPath);
|
|
443
|
-
if (result instanceof Promise) {
|
|
444
|
-
if (abortEarly && allErrors.length > 0) continue;
|
|
445
|
-
const index = i;
|
|
446
|
-
pendingResults.push(
|
|
447
|
-
result.then((r) => {
|
|
448
|
-
if (isOk(r)) {
|
|
449
|
-
validatedItems[index] = r.value;
|
|
450
|
-
} else {
|
|
451
|
-
allErrors.push(...r.error);
|
|
452
|
-
}
|
|
453
|
-
})
|
|
454
|
-
);
|
|
455
|
-
} else if (isOk(result)) {
|
|
456
|
-
validatedItems[i] = result.value;
|
|
457
|
-
} else {
|
|
458
|
-
allErrors.push(...result.error);
|
|
459
|
-
if (abortEarly) break;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
if (pendingResults.length > 0) {
|
|
463
|
-
return Promise.all(pendingResults).then(() => {
|
|
464
|
-
if (allErrors.length > 0) return err(allErrors);
|
|
465
|
-
return ok(validatedItems);
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
if (allErrors.length > 0) return err(allErrors);
|
|
469
|
-
return ok(validatedItems);
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
function minItems(min2, message = `Array must have at least ${min2} items`) {
|
|
473
|
-
return (value, path = []) => {
|
|
474
|
-
if (value.length < min2) {
|
|
475
|
-
return err([{ path, message }]);
|
|
476
|
-
}
|
|
477
|
-
return ok(value);
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
function maxItems(max2, message = `Array must have at most ${max2} items`) {
|
|
481
|
-
return (value, path = []) => {
|
|
482
|
-
if (value.length > max2) {
|
|
483
|
-
return err([{ path, message }]);
|
|
484
|
-
}
|
|
485
|
-
return ok(value);
|
|
486
|
-
};
|
|
487
|
-
}
|
|
488
|
-
function notEmpty(message = "Array must not be empty") {
|
|
489
|
-
return minItems(1, message);
|
|
490
|
-
}
|
|
491
|
-
function unique(message = "Array must contain unique values", keyExtractor) {
|
|
492
|
-
return (value, path = []) => {
|
|
493
|
-
const seen = /* @__PURE__ */ new Set();
|
|
494
|
-
for (const [i, item] of value.entries()) {
|
|
495
|
-
const key = keyExtractor ? keyExtractor(item) : item;
|
|
496
|
-
if (seen.has(key)) {
|
|
497
|
-
return err([
|
|
498
|
-
{
|
|
499
|
-
path: [...path, i.toString()],
|
|
500
|
-
message
|
|
501
|
-
}
|
|
502
|
-
]);
|
|
503
|
-
}
|
|
504
|
-
seen.add(key);
|
|
505
|
-
}
|
|
506
|
-
return ok(value);
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
function oneOf(allowedValues, message = `Value must be one of: ${allowedValues.join(", ")}`) {
|
|
510
|
-
return (value, path = []) => {
|
|
511
|
-
if (!allowedValues.includes(value)) {
|
|
512
|
-
return err([{ path, message }]);
|
|
513
|
-
}
|
|
514
|
-
return ok(value);
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
function stringEnum(allowedValues, message = `Value must be one of: ${allowedValues.join(", ")}`) {
|
|
518
|
-
return chain(string("Value must be a string"), (value, path = []) => {
|
|
519
|
-
if (!allowedValues.includes(value)) {
|
|
520
|
-
return err([{ path, message }]);
|
|
521
|
-
}
|
|
522
|
-
return ok(value);
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
// src/schema/tuple.ts
|
|
527
|
-
function tuple(validators) {
|
|
528
|
-
return (value, parentPath = []) => {
|
|
529
|
-
if (!Array.isArray(value)) {
|
|
530
|
-
return err([{ path: parentPath, message: "Expected an array" }]);
|
|
531
|
-
}
|
|
532
|
-
if (value.length !== validators.length) {
|
|
533
|
-
return err([
|
|
534
|
-
{
|
|
535
|
-
path: parentPath,
|
|
536
|
-
message: `Expected tuple of length ${validators.length}, got ${value.length}`
|
|
537
|
-
}
|
|
538
|
-
]);
|
|
539
|
-
}
|
|
540
|
-
const abortEarly = _getAbortEarly();
|
|
541
|
-
const allErrors = [];
|
|
542
|
-
const validatedItems = Array.from({ length: validators.length });
|
|
543
|
-
const pendingResults = [];
|
|
544
|
-
for (const [i, validator] of validators.entries()) {
|
|
545
|
-
if (abortEarly && allErrors.length > 0) break;
|
|
546
|
-
const itemPath = [...parentPath, i.toString()];
|
|
547
|
-
const item = value.at(i);
|
|
548
|
-
const result = validator(item, itemPath);
|
|
549
|
-
if (result instanceof Promise) {
|
|
550
|
-
if (abortEarly && allErrors.length > 0) continue;
|
|
551
|
-
const index = i;
|
|
552
|
-
pendingResults.push(
|
|
553
|
-
result.then((r) => {
|
|
554
|
-
if (isOk(r)) {
|
|
555
|
-
validatedItems[index] = r.value;
|
|
556
|
-
} else {
|
|
557
|
-
allErrors.push(...r.error);
|
|
558
|
-
}
|
|
559
|
-
})
|
|
560
|
-
);
|
|
561
|
-
} else if (isOk(result)) {
|
|
562
|
-
validatedItems[i] = result.value;
|
|
563
|
-
} else {
|
|
564
|
-
allErrors.push(...result.error);
|
|
565
|
-
if (abortEarly) break;
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
if (pendingResults.length > 0) {
|
|
569
|
-
return Promise.all(pendingResults).then(() => {
|
|
570
|
-
if (allErrors.length > 0) return err(allErrors);
|
|
571
|
-
return ok(validatedItems);
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
if (allErrors.length > 0) return err(allErrors);
|
|
575
|
-
return ok(validatedItems);
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
function tupleOf(elementValidator, length) {
|
|
579
|
-
return (value, parentPath = []) => {
|
|
580
|
-
if (!Array.isArray(value)) {
|
|
581
|
-
return err([{ path: parentPath, message: "Expected an array" }]);
|
|
582
|
-
}
|
|
583
|
-
if (value.length !== length) {
|
|
584
|
-
return err([
|
|
585
|
-
{
|
|
586
|
-
path: parentPath,
|
|
587
|
-
message: `Expected tuple of length ${length}, got ${value.length}`
|
|
588
|
-
}
|
|
589
|
-
]);
|
|
590
|
-
}
|
|
591
|
-
const abortEarly = _getAbortEarly();
|
|
592
|
-
const allErrors = [];
|
|
593
|
-
const validatedItems = Array.from({ length });
|
|
594
|
-
const pendingResults = [];
|
|
595
|
-
for (const [i, item] of value.entries()) {
|
|
596
|
-
if (abortEarly && allErrors.length > 0) break;
|
|
597
|
-
const itemPath = [...parentPath, i.toString()];
|
|
598
|
-
const result = elementValidator(item, itemPath);
|
|
599
|
-
if (result instanceof Promise) {
|
|
600
|
-
if (abortEarly && allErrors.length > 0) continue;
|
|
601
|
-
const index = i;
|
|
602
|
-
pendingResults.push(
|
|
603
|
-
result.then((r) => {
|
|
604
|
-
if (isOk(r)) {
|
|
605
|
-
validatedItems[index] = r.value;
|
|
606
|
-
} else {
|
|
607
|
-
allErrors.push(...r.error);
|
|
608
|
-
}
|
|
609
|
-
})
|
|
610
|
-
);
|
|
611
|
-
} else if (isOk(result)) {
|
|
612
|
-
validatedItems[i] = result.value;
|
|
613
|
-
} else {
|
|
614
|
-
allErrors.push(...result.error);
|
|
615
|
-
if (abortEarly) break;
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
if (pendingResults.length > 0) {
|
|
619
|
-
return Promise.all(pendingResults).then(() => {
|
|
620
|
-
if (allErrors.length > 0) return err(allErrors);
|
|
621
|
-
return ok(validatedItems);
|
|
622
|
-
});
|
|
623
|
-
}
|
|
624
|
-
if (allErrors.length > 0) return err(allErrors);
|
|
625
|
-
return ok(validatedItems);
|
|
626
|
-
};
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
// src/schema/number.ts
|
|
630
|
-
function number(message = "Must be a number") {
|
|
631
|
-
return (value, path = []) => {
|
|
632
|
-
if (typeof value !== "number" || Number.isNaN(value)) {
|
|
633
|
-
return err([{ path, message }]);
|
|
634
|
-
}
|
|
635
|
-
return ok(value);
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
function min(value, message = `Must be at least ${value}`) {
|
|
639
|
-
return (input, path = []) => {
|
|
640
|
-
if (input < value) {
|
|
641
|
-
return err([{ path, message }]);
|
|
642
|
-
}
|
|
643
|
-
return ok(input);
|
|
644
|
-
};
|
|
645
|
-
}
|
|
646
|
-
function max(value, message = `Must be at most ${value}`) {
|
|
647
|
-
return (input, path = []) => {
|
|
648
|
-
if (input > value) {
|
|
649
|
-
return err([{ path, message }]);
|
|
650
|
-
}
|
|
651
|
-
return ok(input);
|
|
652
|
-
};
|
|
653
|
-
}
|
|
654
|
-
function between(min2, max2, message = `Must be between ${min2} and ${max2}`) {
|
|
655
|
-
return (input, path = []) => {
|
|
656
|
-
if (input < min2 || input > max2) {
|
|
657
|
-
return err([{ path, message }]);
|
|
658
|
-
}
|
|
659
|
-
return ok(input);
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
|
-
function integer(message = "Must be an integer") {
|
|
663
|
-
return (value, path = []) => {
|
|
664
|
-
if (!Number.isInteger(value)) {
|
|
665
|
-
return err([{ path, message }]);
|
|
666
|
-
}
|
|
667
|
-
return ok(value);
|
|
668
|
-
};
|
|
669
|
-
}
|
|
670
|
-
function positive(message = "Must be a positive number") {
|
|
671
|
-
return (value, path = []) => {
|
|
672
|
-
if (value <= 0) {
|
|
673
|
-
return err([{ path, message }]);
|
|
674
|
-
}
|
|
675
|
-
return ok(value);
|
|
676
|
-
};
|
|
677
|
-
}
|
|
678
|
-
function negative(message = "Must be a negative number") {
|
|
679
|
-
return (value, path = []) => {
|
|
680
|
-
if (value >= 0) {
|
|
681
|
-
return err([{ path, message }]);
|
|
682
|
-
}
|
|
683
|
-
return ok(value);
|
|
684
|
-
};
|
|
685
|
-
}
|
|
686
|
-
function nonZero(message = "Must not be zero") {
|
|
687
|
-
return (value, path = []) => {
|
|
688
|
-
if (value === 0) {
|
|
689
|
-
return err([{ path, message }]);
|
|
690
|
-
}
|
|
691
|
-
return ok(value);
|
|
692
|
-
};
|
|
693
|
-
}
|
|
694
|
-
function divisibleBy(divisor, message = `Must be divisible by ${divisor}`) {
|
|
695
|
-
return (value, path = []) => {
|
|
696
|
-
if (value % divisor !== 0) {
|
|
697
|
-
return err([{ path, message }]);
|
|
698
|
-
}
|
|
699
|
-
return ok(value);
|
|
700
|
-
};
|
|
701
|
-
}
|
|
702
|
-
function precision(maxDecimalPlaces, message = `Must have at most ${maxDecimalPlaces} decimal places`) {
|
|
703
|
-
return (value, path = []) => {
|
|
704
|
-
const str = value.toString();
|
|
705
|
-
const decimalPlaces = (str.split(".")[1] || "").length;
|
|
706
|
-
if (decimalPlaces > maxDecimalPlaces) {
|
|
707
|
-
return err([{ path, message }]);
|
|
708
|
-
}
|
|
709
|
-
return ok(value);
|
|
710
|
-
};
|
|
711
|
-
}
|
|
712
|
-
function finite(message = "Must be a finite number") {
|
|
713
|
-
return (value, path = []) => {
|
|
714
|
-
if (!Number.isFinite(value)) {
|
|
715
|
-
return err([{ path, message }]);
|
|
716
|
-
}
|
|
717
|
-
return ok(value);
|
|
718
|
-
};
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
// src/schema/parsers.ts
|
|
722
|
-
function parseNumber(message = "Must be a valid number") {
|
|
723
|
-
return (value, path = []) => {
|
|
724
|
-
if (typeof value === "number") {
|
|
725
|
-
return ok(value);
|
|
726
|
-
}
|
|
727
|
-
if (typeof value === "string") {
|
|
728
|
-
const trimmed = value.trim();
|
|
729
|
-
if (trimmed === "") {
|
|
730
|
-
return err([{ path, message }]);
|
|
731
|
-
}
|
|
732
|
-
const num = Number(trimmed);
|
|
733
|
-
if (!Number.isNaN(num)) {
|
|
734
|
-
return ok(num);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
return err([{ path, message }]);
|
|
738
|
-
};
|
|
739
|
-
}
|
|
740
|
-
function parseBigInt(message = "Must be a valid BigInt") {
|
|
741
|
-
return (value, path = []) => {
|
|
742
|
-
if (typeof value === "bigint") {
|
|
743
|
-
return ok(value);
|
|
744
|
-
}
|
|
745
|
-
if (typeof value === "string" || typeof value === "number") {
|
|
746
|
-
try {
|
|
747
|
-
if (typeof value === "number" && !Number.isInteger(value)) {
|
|
748
|
-
return err([
|
|
749
|
-
{
|
|
750
|
-
path,
|
|
751
|
-
message: "Cannot convert non-integer number to BigInt"
|
|
752
|
-
}
|
|
753
|
-
]);
|
|
754
|
-
}
|
|
755
|
-
const bigIntValue = BigInt(value);
|
|
756
|
-
return ok(bigIntValue);
|
|
757
|
-
} catch {
|
|
758
|
-
return err([{ path, message }]);
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
return err([{ path, message }]);
|
|
762
|
-
};
|
|
763
|
-
}
|
|
764
|
-
function parseEnum(enumObject, message = `Must be a valid enum value`) {
|
|
765
|
-
return (value, path = []) => {
|
|
766
|
-
const enumEntries = Object.entries(enumObject).filter(
|
|
767
|
-
([key]) => Number.isNaN(Number(key)) || !Number.isInteger(Number(key))
|
|
768
|
-
);
|
|
769
|
-
const enumValues = enumEntries.map(([, val]) => val);
|
|
770
|
-
if (enumValues.includes(value)) {
|
|
771
|
-
return ok(value);
|
|
772
|
-
}
|
|
773
|
-
if (typeof value === "string") {
|
|
774
|
-
const parsed = Number(value);
|
|
775
|
-
if (!Number.isNaN(parsed) && enumValues.includes(parsed)) {
|
|
776
|
-
return ok(parsed);
|
|
777
|
-
}
|
|
778
|
-
const exactEntry = enumEntries.find(([key]) => key === value);
|
|
779
|
-
if (exactEntry) {
|
|
780
|
-
return ok(exactEntry[1]);
|
|
781
|
-
}
|
|
782
|
-
const lowerValue = value.toLowerCase();
|
|
783
|
-
const caseInsensitiveEntry = enumEntries.find(([key]) => key.toLowerCase() === lowerValue);
|
|
784
|
-
if (caseInsensitiveEntry) {
|
|
785
|
-
return ok(caseInsensitiveEntry[1]);
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
return err([
|
|
789
|
-
{
|
|
790
|
-
path,
|
|
791
|
-
message
|
|
792
|
-
}
|
|
793
|
-
]);
|
|
794
|
-
};
|
|
795
|
-
}
|
|
796
|
-
function parseDate(message = "Must be a valid date") {
|
|
797
|
-
return (value, path = []) => {
|
|
798
|
-
if (value instanceof Date) {
|
|
799
|
-
return ok(value);
|
|
800
|
-
}
|
|
801
|
-
let dateValue;
|
|
802
|
-
if (typeof value === "string") {
|
|
803
|
-
dateValue = new Date(value);
|
|
804
|
-
} else if (typeof value === "number") {
|
|
805
|
-
dateValue = new Date(value);
|
|
806
|
-
} else {
|
|
807
|
-
return err([{ path, message }]);
|
|
808
|
-
}
|
|
809
|
-
if (Number.isNaN(dateValue.getTime())) {
|
|
810
|
-
return err([{ path, message }]);
|
|
811
|
-
}
|
|
812
|
-
return ok(dateValue);
|
|
813
|
-
};
|
|
814
|
-
}
|
|
815
|
-
function parseBool(message = "Must be a valid boolean value") {
|
|
816
|
-
return (value, path = []) => {
|
|
817
|
-
if (typeof value === "boolean") {
|
|
818
|
-
return ok(value);
|
|
819
|
-
}
|
|
820
|
-
if (typeof value === "string") {
|
|
821
|
-
const normalized = value.toLowerCase().trim();
|
|
822
|
-
if (normalized === "true" || normalized === "1" || normalized === "yes") {
|
|
823
|
-
return ok(true);
|
|
824
|
-
}
|
|
825
|
-
if (normalized === "false" || normalized === "0" || normalized === "no") {
|
|
826
|
-
return ok(false);
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
if (typeof value === "number") {
|
|
830
|
-
if (value === 1) return ok(true);
|
|
831
|
-
if (value === 0) return ok(false);
|
|
832
|
-
}
|
|
833
|
-
return err([{ path, message }]);
|
|
834
|
-
};
|
|
835
|
-
}
|
|
836
|
-
function parseString(message = "Must be convertible to string") {
|
|
837
|
-
return (value, path = []) => {
|
|
838
|
-
if (typeof value === "string") {
|
|
839
|
-
return ok(value);
|
|
840
|
-
}
|
|
841
|
-
if (value === null || value === void 0) {
|
|
842
|
-
return err([{ path, message }]);
|
|
843
|
-
}
|
|
844
|
-
return ok(String(value));
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
function parseJSON(message = "Must be valid JSON") {
|
|
848
|
-
return (value, path = []) => {
|
|
849
|
-
if (typeof value === "string") {
|
|
850
|
-
return mapErr(
|
|
851
|
-
fromTry(() => JSON.parse(value)),
|
|
852
|
-
() => [{ path, message }]
|
|
853
|
-
);
|
|
854
|
-
}
|
|
855
|
-
if (typeof value === "object" && value !== null) {
|
|
856
|
-
return ok(value);
|
|
857
|
-
}
|
|
858
|
-
return err([{ path, message }]);
|
|
859
|
-
};
|
|
860
|
-
}
|
|
861
|
-
function parseISODate(message = "Must be a valid ISO date string") {
|
|
862
|
-
return (value, path = []) => {
|
|
863
|
-
if (typeof value !== "string") {
|
|
864
|
-
return err([{ path, message }]);
|
|
865
|
-
}
|
|
866
|
-
if (!/^\d{4}-\d{2}-\d{2}/.test(value)) {
|
|
867
|
-
return err([{ path, message }]);
|
|
868
|
-
}
|
|
869
|
-
const date2 = new Date(value);
|
|
870
|
-
if (Number.isNaN(date2.getTime())) {
|
|
871
|
-
return err([{ path, message }]);
|
|
872
|
-
}
|
|
873
|
-
const originalMonth = value.slice(5, 7);
|
|
874
|
-
const originalDay = value.slice(8, 10);
|
|
875
|
-
const parsedMonth = String(date2.getUTCMonth() + 1).padStart(2, "0");
|
|
876
|
-
const parsedDay = String(date2.getUTCDate()).padStart(2, "0");
|
|
877
|
-
if (originalMonth !== parsedMonth || originalDay !== parsedDay) {
|
|
878
|
-
return err([{ path, message }]);
|
|
879
|
-
}
|
|
880
|
-
return ok(date2);
|
|
881
|
-
};
|
|
882
|
-
}
|
|
883
|
-
function parseURL(message = "Must be a valid URL") {
|
|
884
|
-
return (value, path = []) => {
|
|
885
|
-
if (typeof value !== "string") {
|
|
886
|
-
return err([{ path, message }]);
|
|
887
|
-
}
|
|
888
|
-
try {
|
|
889
|
-
return ok(new URL(value));
|
|
890
|
-
} catch {
|
|
891
|
-
return err([{ path, message }]);
|
|
892
|
-
}
|
|
893
|
-
};
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
// src/schema/date.ts
|
|
897
|
-
function date(message = "Must be a Date object") {
|
|
898
|
-
return (value, path = []) => {
|
|
899
|
-
if (!(value instanceof Date)) {
|
|
900
|
-
return err([{ path, message }]);
|
|
901
|
-
}
|
|
902
|
-
if (Number.isNaN(value.getTime())) {
|
|
903
|
-
return err([
|
|
904
|
-
{
|
|
905
|
-
path,
|
|
906
|
-
message: "Invalid Date"
|
|
907
|
-
}
|
|
908
|
-
]);
|
|
909
|
-
}
|
|
910
|
-
return ok(value);
|
|
911
|
-
};
|
|
912
|
-
}
|
|
913
|
-
function dateRange(min2, max2, message = `Must be between ${min2.toISOString().split("T")[0]} and ${max2.toISOString().split("T")[0]}`) {
|
|
914
|
-
return (value, path = []) => {
|
|
915
|
-
if (value < min2 || value > max2) {
|
|
916
|
-
return err([{ path, message }]);
|
|
917
|
-
}
|
|
918
|
-
return ok(value);
|
|
919
|
-
};
|
|
920
|
-
}
|
|
921
|
-
function pastDate(message = "Must be a date in the past") {
|
|
922
|
-
return (value, path = []) => {
|
|
923
|
-
if (value >= /* @__PURE__ */ new Date()) {
|
|
924
|
-
return err([{ path, message }]);
|
|
925
|
-
}
|
|
926
|
-
return ok(value);
|
|
927
|
-
};
|
|
928
|
-
}
|
|
929
|
-
function futureDate(message = "Must be a date in the future") {
|
|
930
|
-
return (value, path = []) => {
|
|
931
|
-
if (value <= /* @__PURE__ */ new Date()) {
|
|
932
|
-
return err([{ path, message }]);
|
|
933
|
-
}
|
|
934
|
-
return ok(value);
|
|
935
|
-
};
|
|
936
|
-
}
|
|
937
|
-
function todayOrFuture(message = "Must be today or a future date") {
|
|
938
|
-
const today = /* @__PURE__ */ new Date();
|
|
939
|
-
today.setHours(0, 0, 0, 0);
|
|
940
|
-
return (value, path = []) => {
|
|
941
|
-
if (value < today) {
|
|
942
|
-
return err([{ path, message }]);
|
|
943
|
-
}
|
|
944
|
-
return ok(value);
|
|
945
|
-
};
|
|
946
|
-
}
|
|
947
|
-
|
|
948
|
-
// src/schema/boolean.ts
|
|
949
|
-
function boolean(message = "Must be a boolean") {
|
|
950
|
-
return (value, path = []) => {
|
|
951
|
-
if (typeof value !== "boolean") {
|
|
952
|
-
return err([{ path, message }]);
|
|
953
|
-
}
|
|
954
|
-
return ok(value);
|
|
955
|
-
};
|
|
956
|
-
}
|
|
957
|
-
function matches(expected, message = `Value must be ${expected}`) {
|
|
958
|
-
return (value, path = []) => {
|
|
959
|
-
if (value !== expected) {
|
|
960
|
-
return err([{ path, message }]);
|
|
961
|
-
}
|
|
962
|
-
return ok(value);
|
|
963
|
-
};
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
// src/schema/union.ts
|
|
967
|
-
function union(validators, options) {
|
|
968
|
-
const { collectAllErrors = true, errorPrefix } = options || {};
|
|
969
|
-
const buildError = (allErrors) => err(
|
|
970
|
-
allErrors.flat().map((error) => ({
|
|
971
|
-
path: error.path,
|
|
972
|
-
message: errorPrefix ? `${errorPrefix}: ${error.message}` : error.message
|
|
973
|
-
}))
|
|
974
|
-
);
|
|
975
|
-
return (value, parentPath = []) => {
|
|
976
|
-
if (validators.length === 0) {
|
|
977
|
-
return err([{ path: parentPath, message: "No validators provided to union" }]);
|
|
978
|
-
}
|
|
979
|
-
const allErrors = [];
|
|
980
|
-
for (let i = 0; i < validators.length; i++) {
|
|
981
|
-
const result = validators[i](value, parentPath);
|
|
982
|
-
if (result instanceof Promise) {
|
|
983
|
-
return (async () => {
|
|
984
|
-
const asyncResult = await result;
|
|
985
|
-
if (isOk(asyncResult)) return asyncResult;
|
|
986
|
-
allErrors.push(asyncResult.error);
|
|
987
|
-
if (!collectAllErrors) return buildError(allErrors);
|
|
988
|
-
for (let j = i + 1; j < validators.length; j++) {
|
|
989
|
-
const r = await validators[j](value, parentPath);
|
|
990
|
-
if (isOk(r)) return r;
|
|
991
|
-
allErrors.push(r.error);
|
|
992
|
-
if (!collectAllErrors) break;
|
|
993
|
-
}
|
|
994
|
-
return buildError(allErrors);
|
|
995
|
-
})();
|
|
996
|
-
}
|
|
997
|
-
if (isOk(result)) {
|
|
998
|
-
return result;
|
|
999
|
-
}
|
|
1000
|
-
allErrors.push(result.error);
|
|
1001
|
-
if (!collectAllErrors) {
|
|
1002
|
-
break;
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
return buildError(allErrors);
|
|
1006
|
-
};
|
|
1007
|
-
}
|
|
1008
|
-
function discriminatedUnion(discriminantField, validatorMap, fallbackMessage = `Invalid discriminant value for '${discriminantField}'`) {
|
|
1009
|
-
return (value, parentPath = []) => {
|
|
1010
|
-
if (value === null || typeof value !== "object") {
|
|
1011
|
-
return err([{ path: parentPath, message: "Expected an object" }]);
|
|
1012
|
-
}
|
|
1013
|
-
const discriminantValue = value[discriminantField];
|
|
1014
|
-
if (typeof discriminantValue !== "string") {
|
|
1015
|
-
return err([
|
|
1016
|
-
{
|
|
1017
|
-
path: [...parentPath, discriminantField],
|
|
1018
|
-
message: `Missing or invalid discriminant field '${discriminantField}'`
|
|
1019
|
-
}
|
|
1020
|
-
]);
|
|
1021
|
-
}
|
|
1022
|
-
const validator = validatorMap[discriminantValue];
|
|
1023
|
-
if (!validator) {
|
|
1024
|
-
return err([
|
|
1025
|
-
{
|
|
1026
|
-
path: [...parentPath, discriminantField],
|
|
1027
|
-
message: `${fallbackMessage}: '${discriminantValue}'`
|
|
1028
|
-
}
|
|
1029
|
-
]);
|
|
1030
|
-
}
|
|
1031
|
-
const result = validator(value, parentPath);
|
|
1032
|
-
if (result instanceof Promise) {
|
|
1033
|
-
return result.then((r) => r);
|
|
1034
|
-
}
|
|
1035
|
-
return result;
|
|
1036
|
-
};
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
// src/schema/enum.ts
|
|
1040
|
-
function enumValue(enumObject, message, excludedValues) {
|
|
1041
|
-
return (value, path = []) => {
|
|
1042
|
-
const enumValues = Object.values(enumObject);
|
|
1043
|
-
if (!enumValues.includes(value)) {
|
|
1044
|
-
const defaultMessage = `Value must be one of: ${enumValues.join(", ")}`;
|
|
1045
|
-
return err([{ path, message: message || defaultMessage }]);
|
|
1046
|
-
}
|
|
1047
|
-
if (excludedValues && excludedValues.includes(value)) {
|
|
1048
|
-
const defaultMessage = `Selected value is excluded from valid enum values`;
|
|
1049
|
-
return err([{ path, message: `${message || defaultMessage}` }]);
|
|
1050
|
-
}
|
|
1051
|
-
return ok(value);
|
|
1052
|
-
};
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
// src/schema/bigint.ts
|
|
1056
|
-
function bigint(message = "Must be a bigint") {
|
|
1057
|
-
return (value, path = []) => {
|
|
1058
|
-
if (typeof value !== "bigint") {
|
|
1059
|
-
return err([{ path, message }]);
|
|
1060
|
-
}
|
|
1061
|
-
return ok(value);
|
|
1062
|
-
};
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
// src/schema/standard.ts
|
|
1066
|
-
function mapErrors(errors) {
|
|
1067
|
-
return errors.map((e) => ({ message: e.message, path: e.path }));
|
|
1068
|
-
}
|
|
1069
|
-
function toStandardSchema(validator) {
|
|
1070
|
-
return {
|
|
1071
|
-
"~standard": {
|
|
1072
|
-
version: 1,
|
|
1073
|
-
vendor: "railway-ts",
|
|
1074
|
-
validate(value) {
|
|
1075
|
-
try {
|
|
1076
|
-
const result = validator(value);
|
|
1077
|
-
if (result instanceof Promise) {
|
|
1078
|
-
return result.then((r) => isOk(r) ? { value: r.value } : { issues: mapErrors(r.error) }).catch((error) => ({
|
|
1079
|
-
issues: [{ message: error instanceof Error ? error.message : String(error) }]
|
|
1080
|
-
}));
|
|
1081
|
-
}
|
|
1082
|
-
return isOk(result) ? { value: result.value } : { issues: mapErrors(result.error) };
|
|
1083
|
-
} catch (error) {
|
|
1084
|
-
return { issues: [{ message: error instanceof Error ? error.message : String(error) }] };
|
|
1085
|
-
}
|
|
1086
|
-
},
|
|
1087
|
-
types: void 0
|
|
1088
|
-
}
|
|
1089
|
-
};
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
exports.ROOT_ERROR_KEY = ROOT_ERROR_KEY;
|
|
1093
|
-
exports._getAbortEarly = _getAbortEarly;
|
|
1094
|
-
exports._withAbortEarly = _withAbortEarly;
|
|
1095
|
-
exports.array = array;
|
|
1096
|
-
exports.between = between;
|
|
1097
|
-
exports.bigint = bigint;
|
|
1098
|
-
exports.boolean = boolean;
|
|
1099
|
-
exports.chain = chain;
|
|
1100
|
-
exports.chainAsync = chainAsync;
|
|
1101
|
-
exports.date = date;
|
|
1102
|
-
exports.dateRange = dateRange;
|
|
1103
|
-
exports.discriminatedUnion = discriminatedUnion;
|
|
1104
|
-
exports.divisibleBy = divisibleBy;
|
|
1105
|
-
exports.email = email;
|
|
1106
|
-
exports.emptyAsOptional = emptyAsOptional;
|
|
1107
|
-
exports.enumValue = enumValue;
|
|
1108
|
-
exports.finite = finite;
|
|
1109
|
-
exports.formatErrors = formatErrors;
|
|
1110
|
-
exports.futureDate = futureDate;
|
|
1111
|
-
exports.integer = integer;
|
|
1112
|
-
exports.is = is;
|
|
1113
|
-
exports.isoDate = isoDate;
|
|
1114
|
-
exports.isoDateTime = isoDateTime;
|
|
1115
|
-
exports.isoDuration = isoDuration;
|
|
1116
|
-
exports.isoTime = isoTime;
|
|
1117
|
-
exports.literal = literal;
|
|
1118
|
-
exports.matches = matches;
|
|
1119
|
-
exports.max = max;
|
|
1120
|
-
exports.maxItems = maxItems;
|
|
1121
|
-
exports.maxLength = maxLength;
|
|
1122
|
-
exports.min = min;
|
|
1123
|
-
exports.minItems = minItems;
|
|
1124
|
-
exports.minLength = minLength;
|
|
1125
|
-
exports.negative = negative;
|
|
1126
|
-
exports.nonEmpty = nonEmpty;
|
|
1127
|
-
exports.nonZero = nonZero;
|
|
1128
|
-
exports.notEmpty = notEmpty;
|
|
1129
|
-
exports.nullable = nullable;
|
|
1130
|
-
exports.number = number;
|
|
1131
|
-
exports.object = object;
|
|
1132
|
-
exports.oneOf = oneOf;
|
|
1133
|
-
exports.optional = optional;
|
|
1134
|
-
exports.parseBigInt = parseBigInt;
|
|
1135
|
-
exports.parseBool = parseBool;
|
|
1136
|
-
exports.parseDate = parseDate;
|
|
1137
|
-
exports.parseEnum = parseEnum;
|
|
1138
|
-
exports.parseISODate = parseISODate;
|
|
1139
|
-
exports.parseJSON = parseJSON;
|
|
1140
|
-
exports.parseNumber = parseNumber;
|
|
1141
|
-
exports.parseString = parseString;
|
|
1142
|
-
exports.parseURL = parseURL;
|
|
1143
|
-
exports.pastDate = pastDate;
|
|
1144
|
-
exports.pattern = pattern;
|
|
1145
|
-
exports.phoneNumber = phoneNumber;
|
|
1146
|
-
exports.positive = positive;
|
|
1147
|
-
exports.precision = precision;
|
|
1148
|
-
exports.refine = refine;
|
|
1149
|
-
exports.refineAsync = refineAsync;
|
|
1150
|
-
exports.refineAt = refineAt;
|
|
1151
|
-
exports.refineAtAsync = refineAtAsync;
|
|
1152
|
-
exports.required = required;
|
|
1153
|
-
exports.string = string;
|
|
1154
|
-
exports.stringEnum = stringEnum;
|
|
1155
|
-
exports.toStandardSchema = toStandardSchema;
|
|
1156
|
-
exports.todayOrFuture = todayOrFuture;
|
|
1157
|
-
exports.transform = transform;
|
|
1158
|
-
exports.tuple = tuple;
|
|
1159
|
-
exports.tupleOf = tupleOf;
|
|
1160
|
-
exports.union = union;
|
|
1161
|
-
exports.unique = unique;
|
|
1162
|
-
exports.url = url;
|
|
1163
|
-
exports.uuid = uuid;
|
|
1164
|
-
exports.validate = validate;
|
|
1165
|
-
exports.validateAndFormatResult = validateAndFormatResult;
|
|
1166
|
-
exports.when = when;
|
|
1167
|
-
exports.whenAsync = whenAsync;
|
|
1168
|
-
//# sourceMappingURL=index.cjs.map
|
|
1169
|
-
//# sourceMappingURL=index.cjs.map
|