@railway-ts/pipelines 0.1.21 → 0.1.22

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,7 +177,12 @@ 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
  }
162
188
  function formatErrors(errors) {
@@ -175,20 +201,26 @@ function formatErrors(errors) {
175
201
  {}
176
202
  );
177
203
  }
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
- );
204
+ function validateAndFormatResult(input, schema, options) {
205
+ const run = () => {
206
+ const result = schema(input);
207
+ if (result instanceof Promise) {
208
+ return result.then(
209
+ (r) => match(r, {
210
+ ok: (data) => ({ valid: true, data }),
211
+ err: (errors) => ({ valid: false, errors: formatErrors(errors) })
212
+ })
213
+ );
214
+ }
215
+ return match(result, {
216
+ ok: (data) => ({ valid: true, data }),
217
+ err: (errors) => ({ valid: false, errors: formatErrors(errors) })
218
+ });
219
+ };
220
+ if (options?.abortEarly) {
221
+ return _withAbortEarly(true, run);
187
222
  }
188
- return match(result, {
189
- ok: (data) => ({ valid: true, data }),
190
- err: (errors) => ({ valid: false, errors: formatErrors(errors) })
191
- });
223
+ return run();
192
224
  }
193
225
  function transform(transformer) {
194
226
  return (value) => ok(transformer(value));
@@ -300,13 +332,16 @@ function array(itemValidator) {
300
332
  if (!Array.isArray(value)) {
301
333
  return err([{ path: parentPath, message: "Expected an array" }]);
302
334
  }
335
+ const abortEarly = _getAbortEarly();
303
336
  const allErrors = [];
304
337
  const validatedItems = Array.from({ length: value.length });
305
338
  const pendingResults = [];
306
339
  for (const [i, item] of value.entries()) {
340
+ if (abortEarly && allErrors.length > 0) break;
307
341
  const itemPath = [...parentPath, i.toString()];
308
342
  const result = itemValidator(item, itemPath);
309
343
  if (result instanceof Promise) {
344
+ if (abortEarly && allErrors.length > 0) continue;
310
345
  const index = i;
311
346
  pendingResults.push(
312
347
  result.then((r) => {
@@ -321,6 +356,7 @@ function array(itemValidator) {
321
356
  validatedItems[i] = result.value;
322
357
  } else {
323
358
  allErrors.push(...result.error);
359
+ if (abortEarly) break;
324
360
  }
325
361
  }
326
362
  if (pendingResults.length > 0) {
@@ -401,14 +437,17 @@ function tuple(validators) {
401
437
  }
402
438
  ]);
403
439
  }
440
+ const abortEarly = _getAbortEarly();
404
441
  const allErrors = [];
405
442
  const validatedItems = Array.from({ length: validators.length });
406
443
  const pendingResults = [];
407
444
  for (const [i, validator] of validators.entries()) {
445
+ if (abortEarly && allErrors.length > 0) break;
408
446
  const itemPath = [...parentPath, i.toString()];
409
447
  const item = value.at(i);
410
448
  const result = validator(item, itemPath);
411
449
  if (result instanceof Promise) {
450
+ if (abortEarly && allErrors.length > 0) continue;
412
451
  const index = i;
413
452
  pendingResults.push(
414
453
  result.then((r) => {
@@ -423,6 +462,7 @@ function tuple(validators) {
423
462
  validatedItems[i] = result.value;
424
463
  } else {
425
464
  allErrors.push(...result.error);
465
+ if (abortEarly) break;
426
466
  }
427
467
  }
428
468
  if (pendingResults.length > 0) {
@@ -448,13 +488,16 @@ function tupleOf(elementValidator, length) {
448
488
  }
449
489
  ]);
450
490
  }
491
+ const abortEarly = _getAbortEarly();
451
492
  const allErrors = [];
452
493
  const validatedItems = Array.from({ length });
453
494
  const pendingResults = [];
454
495
  for (const [i, item] of value.entries()) {
496
+ if (abortEarly && allErrors.length > 0) break;
455
497
  const itemPath = [...parentPath, i.toString()];
456
498
  const result = elementValidator(item, itemPath);
457
499
  if (result instanceof Promise) {
500
+ if (abortEarly && allErrors.length > 0) continue;
458
501
  const index = i;
459
502
  pendingResults.push(
460
503
  result.then((r) => {
@@ -469,6 +512,7 @@ function tupleOf(elementValidator, length) {
469
512
  validatedItems[i] = result.value;
470
513
  } else {
471
514
  allErrors.push(...result.error);
515
+ if (abortEarly) break;
472
516
  }
473
517
  }
474
518
  if (pendingResults.length > 0) {
@@ -946,6 +990,8 @@ function toStandardSchema(validator) {
946
990
  }
947
991
 
948
992
  exports.ROOT_ERROR_KEY = ROOT_ERROR_KEY;
993
+ exports._getAbortEarly = _getAbortEarly;
994
+ exports._withAbortEarly = _withAbortEarly;
949
995
  exports.array = array;
950
996
  exports.between = between;
951
997
  exports.bigint = bigint;