@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 +2 -0
- package/dist/schema/index.cjs +68 -14
- package/dist/schema/index.cjs.map +1 -1
- package/dist/schema/index.d.cts +34 -4
- package/dist/schema/index.d.ts +34 -4
- package/dist/schema/index.mjs +66 -15
- package/dist/schema/index.mjs.map +1 -1
- package/docs/API.md +46 -3
- package/docs/GETTING_STARTED.md +21 -0
- package/docs/RECIPES.md +65 -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,61 @@ 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
|
+
|
|
1269
|
+
#### `is`
|
|
1270
|
+
|
|
1271
|
+
```typescript
|
|
1272
|
+
function is(value: unknown, validator: Validator<unknown, unknown>): boolean;
|
|
1273
|
+
function is(value: unknown, validator: MaybeAsyncValidator<unknown, unknown>): boolean | Promise<boolean>;
|
|
1274
|
+
```
|
|
1275
|
+
|
|
1276
|
+
Check if a value passes validation. Returns `true` if valid, `false` if not. Shorthand for `isOk(validate(value, validator))`.
|
|
1277
|
+
|
|
1278
|
+
Uses `abortEarly` internally for efficiency — stops on the first error. Returns a `Promise<boolean>` when given an async validator.
|
|
1279
|
+
|
|
1280
|
+
```typescript
|
|
1281
|
+
is('hello', string()); // true
|
|
1282
|
+
is(42, string()); // false
|
|
1283
|
+
await is('dave', asyncValidator); // true (async)
|
|
1284
|
+
```
|
|
1285
|
+
|
|
1250
1286
|
#### `validateAndFormatResult`
|
|
1251
1287
|
|
|
1252
1288
|
```typescript
|
|
1253
|
-
function validateAndFormatResult<T>(
|
|
1289
|
+
function validateAndFormatResult<T>(
|
|
1290
|
+
input: unknown,
|
|
1291
|
+
schema: Validator<unknown, T>,
|
|
1292
|
+
options?: ValidateOptions,
|
|
1293
|
+
): ValidationResult<T>;
|
|
1254
1294
|
function validateAndFormatResult<T>(
|
|
1255
1295
|
input: unknown,
|
|
1256
1296
|
schema: MaybeAsyncValidator<unknown, T>,
|
|
1297
|
+
options?: ValidateOptions,
|
|
1257
1298
|
): ValidationResult<T> | Promise<ValidationResult<T>>;
|
|
1258
1299
|
```
|
|
1259
1300
|
|
|
1260
1301
|
Validate and format in one call. Returns `{ valid: true, data }` or `{ valid: false, errors }`.
|
|
1261
1302
|
|
|
1303
|
+
Pass `{ abortEarly: true }` as the third argument to stop on the first error.
|
|
1304
|
+
|
|
1262
1305
|
#### `formatErrors`
|
|
1263
1306
|
|
|
1264
1307
|
```typescript
|
package/docs/GETTING_STARTED.md
CHANGED
|
@@ -236,6 +236,27 @@ 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
|
+
|
|
248
|
+
> **Tip:** If you only need a boolean (pass/fail), use `is()`:
|
|
249
|
+
>
|
|
250
|
+
> ```typescript
|
|
251
|
+
> import { is } from '@railway-ts/pipelines/schema';
|
|
252
|
+
>
|
|
253
|
+
> if (is(input, userSchema)) {
|
|
254
|
+
> // input is valid
|
|
255
|
+
> }
|
|
256
|
+
> ```
|
|
257
|
+
>
|
|
258
|
+
> See [API Reference: `is`](API.md#is) for details.
|
|
259
|
+
|
|
239
260
|
---
|
|
240
261
|
|
|
241
262
|
## Step 7: Putting It All Together
|
package/docs/RECIPES.md
CHANGED
|
@@ -247,6 +247,71 @@ 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
|
+
|
|
288
|
+
### Quick Validity Check
|
|
289
|
+
|
|
290
|
+
Use `is()` when you only need a boolean — no error details, no `Result` unwrapping:
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
import { is, string, chain, minLength, object, required, email } from '@railway-ts/pipelines/schema';
|
|
294
|
+
|
|
295
|
+
// Primitives
|
|
296
|
+
is('hello', string()); // true
|
|
297
|
+
is(42, string()); // false
|
|
298
|
+
|
|
299
|
+
// Composed validators
|
|
300
|
+
const shortString = chain(string(), minLength(5));
|
|
301
|
+
is('hello', shortString); // true
|
|
302
|
+
is('hi', shortString); // false
|
|
303
|
+
|
|
304
|
+
// Objects
|
|
305
|
+
const userSchema = object({
|
|
306
|
+
name: required(chain(string(), minLength(2))),
|
|
307
|
+
email: required(chain(string(), email())),
|
|
308
|
+
});
|
|
309
|
+
is({ name: 'Alice', email: 'alice@example.com' }, userSchema); // true
|
|
310
|
+
is({ name: 'Alice', email: 'nope' }, userSchema); // false
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
`is()` uses `abortEarly` internally — it short-circuits on the first error since you only need pass/fail.
|
|
314
|
+
|
|
250
315
|
### Conditional Validation
|
|
251
316
|
|
|
252
317
|
```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.23",
|
|
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": {
|