@railway-ts/pipelines 0.1.21 → 0.1.23

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 CHANGED
@@ -186,6 +186,8 @@ const result = validate(input, userSchema);
186
186
  // Result<User, ValidationError[]>
187
187
  ```
188
188
 
189
+ Pass `{ abortEarly: true }` to `validate()` or `validateAndFormatResult()` to stop on the first error.
190
+
189
191
  `object` · `array` · `tuple` · `required` · `optional` · `chain` · `string` · `parseNumber` · `email` · `min` · `max` · `union` · `discriminatedUnion` · `refine` · `refineAt` · `transform` · `validate` · `toStandardSchema` -- [full API](docs/API.md#schema)
190
192
 
191
193
  ### Composition
@@ -39,16 +39,33 @@ function fromTry(f) {
39
39
  }
40
40
 
41
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
+ }
42
55
  function object(schema, { strict = true } = {}) {
43
56
  return (obj, parentPath = []) => {
44
57
  if (obj === null || typeof obj !== "object") {
45
58
  return err([{ path: parentPath, message: "Expected an object" }]);
46
59
  }
60
+ const abortEarly = _getAbortEarly();
47
61
  const allErrors = [];
48
62
  const validatedObj = {};
49
63
  if (strict !== false) {
50
64
  const extraKeys = Object.keys(obj).filter((key) => !Object.prototype.hasOwnProperty.call(schema, key));
51
65
  if (extraKeys.length > 0) {
66
+ if (abortEarly) {
67
+ return err([{ path: [...parentPath, extraKeys[0]], message: `Unexpected field: '${extraKeys[0]}'` }]);
68
+ }
52
69
  for (const key of extraKeys) {
53
70
  allErrors.push({
54
71
  path: [...parentPath, key],
@@ -57,15 +74,18 @@ function object(schema, { strict = true } = {}) {
57
74
  }
58
75
  }
59
76
  }
77
+ if (abortEarly && allErrors.length > 0) return err(allErrors);
60
78
  const pendingResults = [];
61
79
  for (const key in schema) {
62
80
  if (!Object.prototype.hasOwnProperty.call(schema, key)) continue;
63
81
  const validator = schema[key];
64
82
  if (!validator) continue;
83
+ if (abortEarly && allErrors.length > 0) break;
65
84
  const value = obj[key];
66
85
  const fieldPath = [...parentPath, key];
67
86
  const result = validator(value, fieldPath);
68
87
  if (result instanceof Promise) {
88
+ if (abortEarly && allErrors.length > 0) continue;
69
89
  pendingResults.push(
70
90
  result.then((r) => {
71
91
  if (isOk(r)) {
@@ -79,6 +99,7 @@ function object(schema, { strict = true } = {}) {
79
99
  validatedObj[key] = result.value;
80
100
  } else {
81
101
  allErrors.push(...result.error);
102
+ if (abortEarly) break;
82
103
  }
83
104
  }
84
105
  if (pendingResults.length > 0) {
@@ -156,9 +177,21 @@ function chainAsync(...validators) {
156
177
  return ok(current);
157
178
  };
158
179
  }
159
- function validate(value, validator, path = []) {
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
+ }
160
186
  return validator(value, path);
161
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
+ }
162
195
  function formatErrors(errors) {
163
196
  return errors.reduce(
164
197
  (acc, error) => {
@@ -175,20 +208,26 @@ function formatErrors(errors) {
175
208
  {}
176
209
  );
177
210
  }
178
- function validateAndFormatResult(input, schema) {
179
- const result = schema(input);
180
- if (result instanceof Promise) {
181
- return result.then(
182
- (r) => match(r, {
183
- ok: (data) => ({ valid: true, data }),
184
- err: (errors) => ({ valid: false, errors: formatErrors(errors) })
185
- })
186
- );
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);
187
229
  }
188
- return match(result, {
189
- ok: (data) => ({ valid: true, data }),
190
- err: (errors) => ({ valid: false, errors: formatErrors(errors) })
191
- });
230
+ return run();
192
231
  }
193
232
  function transform(transformer) {
194
233
  return (value) => ok(transformer(value));
@@ -300,13 +339,16 @@ function array(itemValidator) {
300
339
  if (!Array.isArray(value)) {
301
340
  return err([{ path: parentPath, message: "Expected an array" }]);
302
341
  }
342
+ const abortEarly = _getAbortEarly();
303
343
  const allErrors = [];
304
344
  const validatedItems = Array.from({ length: value.length });
305
345
  const pendingResults = [];
306
346
  for (const [i, item] of value.entries()) {
347
+ if (abortEarly && allErrors.length > 0) break;
307
348
  const itemPath = [...parentPath, i.toString()];
308
349
  const result = itemValidator(item, itemPath);
309
350
  if (result instanceof Promise) {
351
+ if (abortEarly && allErrors.length > 0) continue;
310
352
  const index = i;
311
353
  pendingResults.push(
312
354
  result.then((r) => {
@@ -321,6 +363,7 @@ function array(itemValidator) {
321
363
  validatedItems[i] = result.value;
322
364
  } else {
323
365
  allErrors.push(...result.error);
366
+ if (abortEarly) break;
324
367
  }
325
368
  }
326
369
  if (pendingResults.length > 0) {
@@ -401,14 +444,17 @@ function tuple(validators) {
401
444
  }
402
445
  ]);
403
446
  }
447
+ const abortEarly = _getAbortEarly();
404
448
  const allErrors = [];
405
449
  const validatedItems = Array.from({ length: validators.length });
406
450
  const pendingResults = [];
407
451
  for (const [i, validator] of validators.entries()) {
452
+ if (abortEarly && allErrors.length > 0) break;
408
453
  const itemPath = [...parentPath, i.toString()];
409
454
  const item = value.at(i);
410
455
  const result = validator(item, itemPath);
411
456
  if (result instanceof Promise) {
457
+ if (abortEarly && allErrors.length > 0) continue;
412
458
  const index = i;
413
459
  pendingResults.push(
414
460
  result.then((r) => {
@@ -423,6 +469,7 @@ function tuple(validators) {
423
469
  validatedItems[i] = result.value;
424
470
  } else {
425
471
  allErrors.push(...result.error);
472
+ if (abortEarly) break;
426
473
  }
427
474
  }
428
475
  if (pendingResults.length > 0) {
@@ -448,13 +495,16 @@ function tupleOf(elementValidator, length) {
448
495
  }
449
496
  ]);
450
497
  }
498
+ const abortEarly = _getAbortEarly();
451
499
  const allErrors = [];
452
500
  const validatedItems = Array.from({ length });
453
501
  const pendingResults = [];
454
502
  for (const [i, item] of value.entries()) {
503
+ if (abortEarly && allErrors.length > 0) break;
455
504
  const itemPath = [...parentPath, i.toString()];
456
505
  const result = elementValidator(item, itemPath);
457
506
  if (result instanceof Promise) {
507
+ if (abortEarly && allErrors.length > 0) continue;
458
508
  const index = i;
459
509
  pendingResults.push(
460
510
  result.then((r) => {
@@ -469,6 +519,7 @@ function tupleOf(elementValidator, length) {
469
519
  validatedItems[i] = result.value;
470
520
  } else {
471
521
  allErrors.push(...result.error);
522
+ if (abortEarly) break;
472
523
  }
473
524
  }
474
525
  if (pendingResults.length > 0) {
@@ -946,6 +997,8 @@ function toStandardSchema(validator) {
946
997
  }
947
998
 
948
999
  exports.ROOT_ERROR_KEY = ROOT_ERROR_KEY;
1000
+ exports._getAbortEarly = _getAbortEarly;
1001
+ exports._withAbortEarly = _withAbortEarly;
949
1002
  exports.array = array;
950
1003
  exports.between = between;
951
1004
  exports.bigint = bigint;
@@ -963,6 +1016,7 @@ exports.finite = finite;
963
1016
  exports.formatErrors = formatErrors;
964
1017
  exports.futureDate = futureDate;
965
1018
  exports.integer = integer;
1019
+ exports.is = is;
966
1020
  exports.literal = literal;
967
1021
  exports.matches = matches;
968
1022
  exports.max = max;