@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 +2 -0
- package/dist/schema/index.cjs +60 -14
- package/dist/schema/index.cjs.map +1 -1
- package/dist/schema/index.d.cts +19 -4
- package/dist/schema/index.d.ts +19 -4
- package/dist/schema/index.mjs +59 -15
- package/dist/schema/index.mjs.map +1 -1
- package/docs/API.md +29 -3
- package/docs/GETTING_STARTED.md +9 -0
- package/docs/RECIPES.md +38 -0
- package/package.json +1 -1
package/docs/API.md
CHANGED
|
@@ -585,6 +585,14 @@ type ValidationResult<T> = { valid: true; data: T } | { valid: false; errors: Re
|
|
|
585
585
|
|
|
586
586
|
The result of a validation-and-format operation.
|
|
587
587
|
|
|
588
|
+
#### `ValidateOptions`
|
|
589
|
+
|
|
590
|
+
```typescript
|
|
591
|
+
type ValidateOptions = { abortEarly?: boolean };
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
Options for controlling validation behavior. When `abortEarly` is `true`, validation stops after the first error instead of collecting all errors.
|
|
595
|
+
|
|
588
596
|
#### `InferSchemaType<V>`
|
|
589
597
|
|
|
590
598
|
```typescript
|
|
@@ -1239,26 +1247,44 @@ const validate = chainAsync(
|
|
|
1239
1247
|
|
|
1240
1248
|
```typescript
|
|
1241
1249
|
function validate<T>(value: unknown, validator: Validator<unknown, T>): Result<T, ValidationError[]>;
|
|
1250
|
+
function validate<T>(value: unknown, validator: Validator<unknown, T>, path: string[]): Result<T, ValidationError[]>;
|
|
1242
1251
|
function validate<T>(
|
|
1243
1252
|
value: unknown,
|
|
1244
|
-
validator:
|
|
1245
|
-
|
|
1253
|
+
validator: Validator<unknown, T>,
|
|
1254
|
+
options: ValidateOptions,
|
|
1255
|
+
): Result<T, ValidationError[]>;
|
|
1256
|
+
function validate<T>(
|
|
1257
|
+
value: unknown,
|
|
1258
|
+
validator: Validator<unknown, T>,
|
|
1259
|
+
path: string[],
|
|
1260
|
+
options: ValidateOptions,
|
|
1261
|
+
): Result<T, ValidationError[]>;
|
|
1262
|
+
// MaybeAsync overloads mirror the above, returning Result | Promise<Result>
|
|
1246
1263
|
```
|
|
1247
1264
|
|
|
1248
1265
|
Run a validator against a value. Returns sync when the validator is sync.
|
|
1249
1266
|
|
|
1267
|
+
Pass `{ abortEarly: true }` to stop on the first validation error instead of collecting all errors. The third argument can be either a base `path` (string array) for error reporting, or an options object. When both are needed, pass `path` third and `options` fourth.
|
|
1268
|
+
|
|
1250
1269
|
#### `validateAndFormatResult`
|
|
1251
1270
|
|
|
1252
1271
|
```typescript
|
|
1253
|
-
function validateAndFormatResult<T>(
|
|
1272
|
+
function validateAndFormatResult<T>(
|
|
1273
|
+
input: unknown,
|
|
1274
|
+
schema: Validator<unknown, T>,
|
|
1275
|
+
options?: ValidateOptions,
|
|
1276
|
+
): ValidationResult<T>;
|
|
1254
1277
|
function validateAndFormatResult<T>(
|
|
1255
1278
|
input: unknown,
|
|
1256
1279
|
schema: MaybeAsyncValidator<unknown, T>,
|
|
1280
|
+
options?: ValidateOptions,
|
|
1257
1281
|
): ValidationResult<T> | Promise<ValidationResult<T>>;
|
|
1258
1282
|
```
|
|
1259
1283
|
|
|
1260
1284
|
Validate and format in one call. Returns `{ valid: true, data }` or `{ valid: false, errors }`.
|
|
1261
1285
|
|
|
1286
|
+
Pass `{ abortEarly: true }` as the third argument to stop on the first error.
|
|
1287
|
+
|
|
1262
1288
|
#### `formatErrors`
|
|
1263
1289
|
|
|
1264
1290
|
```typescript
|
package/docs/GETTING_STARTED.md
CHANGED
|
@@ -236,6 +236,15 @@ match(result, {
|
|
|
236
236
|
});
|
|
237
237
|
```
|
|
238
238
|
|
|
239
|
+
> **Tip:** If you only need the first error (for performance or UX), pass `{ abortEarly: true }`:
|
|
240
|
+
>
|
|
241
|
+
> ```typescript
|
|
242
|
+
> const result = validate(input, userSchema, { abortEarly: true });
|
|
243
|
+
> // stops at the first failing field
|
|
244
|
+
> ```
|
|
245
|
+
>
|
|
246
|
+
> See [Recipes: Abort Early](RECIPES.md#abort-early) for details.
|
|
247
|
+
|
|
239
248
|
---
|
|
240
249
|
|
|
241
250
|
## Step 7: Putting It All Together
|
package/docs/RECIPES.md
CHANGED
|
@@ -247,6 +247,44 @@ match(result, {
|
|
|
247
247
|
});
|
|
248
248
|
```
|
|
249
249
|
|
|
250
|
+
### Abort Early
|
|
251
|
+
|
|
252
|
+
By default, validation collects every error. Pass `{ abortEarly: true }` to stop on the first failure — useful for large forms or performance-sensitive paths:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
import {
|
|
256
|
+
validate,
|
|
257
|
+
validateAndFormatResult,
|
|
258
|
+
object,
|
|
259
|
+
required,
|
|
260
|
+
chain,
|
|
261
|
+
string,
|
|
262
|
+
minLength,
|
|
263
|
+
email,
|
|
264
|
+
} from '@railway-ts/pipelines/schema';
|
|
265
|
+
|
|
266
|
+
const userSchema = object({
|
|
267
|
+
name: required(chain(string(), minLength(3))),
|
|
268
|
+
email: required(chain(string(), email())),
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const input = { name: '', email: 'bad' };
|
|
272
|
+
|
|
273
|
+
// Default: collects all errors
|
|
274
|
+
const all = validate(input, userSchema);
|
|
275
|
+
// Err([{ path: ['name'], ... }, { path: ['email'], ... }])
|
|
276
|
+
|
|
277
|
+
// Abort early: stops at first error
|
|
278
|
+
const first = validate(input, userSchema, { abortEarly: true });
|
|
279
|
+
// Err([{ path: ['name'], message: 'Must be at least 3 characters' }])
|
|
280
|
+
|
|
281
|
+
// Works with validateAndFormatResult too
|
|
282
|
+
const result = validateAndFormatResult(input, userSchema, { abortEarly: true });
|
|
283
|
+
// { valid: false, errors: { name: 'Must be at least 3 characters' } }
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
`abortEarly` propagates through nested `object()`, `array()`, `tuple()`, and `tupleOf()` validators — the entire tree stops as soon as the first error is found.
|
|
287
|
+
|
|
250
288
|
### Conditional Validation
|
|
251
289
|
|
|
252
290
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@railway-ts/pipelines",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"description": "Railway-oriented pipelines for TypeScript: a lightweight fp-style Result/Option and schema layer for handling errors and validating boundaries.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|