@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 +2 -0
- package/dist/schema/index.cjs +71 -14
- package/dist/schema/index.cjs.map +1 -1
- package/dist/schema/index.d.cts +31 -4
- package/dist/schema/index.d.ts +31 -4
- package/dist/schema/index.mjs +69 -15
- package/dist/schema/index.mjs.map +1 -1
- package/docs/API.md +37 -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
|
|
@@ -878,6 +886,14 @@ function email(message?: string): Validator<string>;
|
|
|
878
886
|
|
|
879
887
|
Ensure a string is a valid email address.
|
|
880
888
|
|
|
889
|
+
#### `url`
|
|
890
|
+
|
|
891
|
+
```typescript
|
|
892
|
+
function url(message?: string): Validator<string>;
|
|
893
|
+
```
|
|
894
|
+
|
|
895
|
+
Ensure a string is a valid URL. Uses the WHATWG URL parser.
|
|
896
|
+
|
|
881
897
|
#### `phoneNumber`
|
|
882
898
|
|
|
883
899
|
```typescript
|
|
@@ -1231,26 +1247,44 @@ const validate = chainAsync(
|
|
|
1231
1247
|
|
|
1232
1248
|
```typescript
|
|
1233
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[]>;
|
|
1251
|
+
function validate<T>(
|
|
1252
|
+
value: unknown,
|
|
1253
|
+
validator: Validator<unknown, T>,
|
|
1254
|
+
options: ValidateOptions,
|
|
1255
|
+
): Result<T, ValidationError[]>;
|
|
1234
1256
|
function validate<T>(
|
|
1235
1257
|
value: unknown,
|
|
1236
|
-
validator:
|
|
1237
|
-
|
|
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>
|
|
1238
1263
|
```
|
|
1239
1264
|
|
|
1240
1265
|
Run a validator against a value. Returns sync when the validator is sync.
|
|
1241
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
|
+
|
|
1242
1269
|
#### `validateAndFormatResult`
|
|
1243
1270
|
|
|
1244
1271
|
```typescript
|
|
1245
|
-
function validateAndFormatResult<T>(
|
|
1272
|
+
function validateAndFormatResult<T>(
|
|
1273
|
+
input: unknown,
|
|
1274
|
+
schema: Validator<unknown, T>,
|
|
1275
|
+
options?: ValidateOptions,
|
|
1276
|
+
): ValidationResult<T>;
|
|
1246
1277
|
function validateAndFormatResult<T>(
|
|
1247
1278
|
input: unknown,
|
|
1248
1279
|
schema: MaybeAsyncValidator<unknown, T>,
|
|
1280
|
+
options?: ValidateOptions,
|
|
1249
1281
|
): ValidationResult<T> | Promise<ValidationResult<T>>;
|
|
1250
1282
|
```
|
|
1251
1283
|
|
|
1252
1284
|
Validate and format in one call. Returns `{ valid: true, data }` or `{ valid: false, errors }`.
|
|
1253
1285
|
|
|
1286
|
+
Pass `{ abortEarly: true }` as the third argument to stop on the first error.
|
|
1287
|
+
|
|
1254
1288
|
#### `formatErrors`
|
|
1255
1289
|
|
|
1256
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": {
|