@railway-ts/pipelines 0.1.20 → 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));
@@ -273,6 +305,16 @@ function email(message = "Invalid email format") {
273
305
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
274
306
  return pattern(emailRegex, message);
275
307
  }
308
+ function url(message = "Invalid URL format") {
309
+ return (value, path = []) => {
310
+ try {
311
+ new URL(value);
312
+ return ok(value);
313
+ } catch {
314
+ return err([{ path, message }]);
315
+ }
316
+ };
317
+ }
276
318
  function phoneNumber(message = "Invalid phone number format") {
277
319
  const phoneRegex = /^\+?[\d\s\-()]{8,20}$/;
278
320
  return (value, path = []) => {
@@ -290,13 +332,16 @@ function array(itemValidator) {
290
332
  if (!Array.isArray(value)) {
291
333
  return err([{ path: parentPath, message: "Expected an array" }]);
292
334
  }
335
+ const abortEarly = _getAbortEarly();
293
336
  const allErrors = [];
294
337
  const validatedItems = Array.from({ length: value.length });
295
338
  const pendingResults = [];
296
339
  for (const [i, item] of value.entries()) {
340
+ if (abortEarly && allErrors.length > 0) break;
297
341
  const itemPath = [...parentPath, i.toString()];
298
342
  const result = itemValidator(item, itemPath);
299
343
  if (result instanceof Promise) {
344
+ if (abortEarly && allErrors.length > 0) continue;
300
345
  const index = i;
301
346
  pendingResults.push(
302
347
  result.then((r) => {
@@ -311,6 +356,7 @@ function array(itemValidator) {
311
356
  validatedItems[i] = result.value;
312
357
  } else {
313
358
  allErrors.push(...result.error);
359
+ if (abortEarly) break;
314
360
  }
315
361
  }
316
362
  if (pendingResults.length > 0) {
@@ -391,14 +437,17 @@ function tuple(validators) {
391
437
  }
392
438
  ]);
393
439
  }
440
+ const abortEarly = _getAbortEarly();
394
441
  const allErrors = [];
395
442
  const validatedItems = Array.from({ length: validators.length });
396
443
  const pendingResults = [];
397
444
  for (const [i, validator] of validators.entries()) {
445
+ if (abortEarly && allErrors.length > 0) break;
398
446
  const itemPath = [...parentPath, i.toString()];
399
447
  const item = value.at(i);
400
448
  const result = validator(item, itemPath);
401
449
  if (result instanceof Promise) {
450
+ if (abortEarly && allErrors.length > 0) continue;
402
451
  const index = i;
403
452
  pendingResults.push(
404
453
  result.then((r) => {
@@ -413,6 +462,7 @@ function tuple(validators) {
413
462
  validatedItems[i] = result.value;
414
463
  } else {
415
464
  allErrors.push(...result.error);
465
+ if (abortEarly) break;
416
466
  }
417
467
  }
418
468
  if (pendingResults.length > 0) {
@@ -438,13 +488,16 @@ function tupleOf(elementValidator, length) {
438
488
  }
439
489
  ]);
440
490
  }
491
+ const abortEarly = _getAbortEarly();
441
492
  const allErrors = [];
442
493
  const validatedItems = Array.from({ length });
443
494
  const pendingResults = [];
444
495
  for (const [i, item] of value.entries()) {
496
+ if (abortEarly && allErrors.length > 0) break;
445
497
  const itemPath = [...parentPath, i.toString()];
446
498
  const result = elementValidator(item, itemPath);
447
499
  if (result instanceof Promise) {
500
+ if (abortEarly && allErrors.length > 0) continue;
448
501
  const index = i;
449
502
  pendingResults.push(
450
503
  result.then((r) => {
@@ -459,6 +512,7 @@ function tupleOf(elementValidator, length) {
459
512
  validatedItems[i] = result.value;
460
513
  } else {
461
514
  allErrors.push(...result.error);
515
+ if (abortEarly) break;
462
516
  }
463
517
  }
464
518
  if (pendingResults.length > 0) {
@@ -936,6 +990,8 @@ function toStandardSchema(validator) {
936
990
  }
937
991
 
938
992
  exports.ROOT_ERROR_KEY = ROOT_ERROR_KEY;
993
+ exports._getAbortEarly = _getAbortEarly;
994
+ exports._withAbortEarly = _withAbortEarly;
939
995
  exports.array = array;
940
996
  exports.between = between;
941
997
  exports.bigint = bigint;
@@ -998,6 +1054,7 @@ exports.tuple = tuple;
998
1054
  exports.tupleOf = tupleOf;
999
1055
  exports.union = union;
1000
1056
  exports.unique = unique;
1057
+ exports.url = url;
1001
1058
  exports.validate = validate;
1002
1059
  exports.validateAndFormatResult = validateAndFormatResult;
1003
1060
  //# sourceMappingURL=index.cjs.map