@orieljs/validation 0.1.0-alpha.15 → 0.1.0-alpha.16
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/dist/index.d.ts +19 -7
- package/dist/index.js +45 -21
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { OrielError, ValidationError, Rules, InferValidatedType, MessageBag, ValidationRule, TypedValidationRule, RuleTyping, InValue, UploadedFile,
|
|
1
|
+
import { OrielError, ValidationError, Rules, Validator as Validator$1, InferValidatedType, MessageBag, ValidationRule, TypedValidationRule, RuleTyping, InValue, UploadedFile, ValidationFactory, RuleCallback } from '@orieljs/contracts';
|
|
2
2
|
export { InferRuleType, InferValidatedType } from '@orieljs/contracts';
|
|
3
3
|
import * as _orieljs_support from '@orieljs/support';
|
|
4
|
-
import { ServiceProvider } from '@orieljs/support';
|
|
4
|
+
import { ValidatedInput, ServiceProvider } from '@orieljs/support';
|
|
5
5
|
|
|
6
6
|
declare class ValidationErrorImpl extends Error implements ValidationError {
|
|
7
7
|
readonly errors: Record<string, string[]>;
|
|
@@ -50,18 +50,25 @@ type ValidatedData<F extends FormRequest> = InferValidatedType<Awaited<ReturnTyp
|
|
|
50
50
|
* A subclass only has to implement rules(); authorize()/messages()/attributes() default to Laravel's own defaults.
|
|
51
51
|
*/
|
|
52
52
|
declare abstract class FormRequest {
|
|
53
|
+
/** Laravel's `$stopOnFirstFailure`: once a field has failed, the others aren't validated. */
|
|
54
|
+
protected stopOnFirstFailure: boolean;
|
|
53
55
|
/** May be async, to await something first: `async rules() { const user = await request().user<User>() … }`. */
|
|
54
56
|
abstract rules(): Rules | Promise<Rules>;
|
|
57
|
+
/** Runs first, before authorize() and the rules: change the input here, with `request().merge()`. */
|
|
58
|
+
protected prepareForValidation(): void | Promise<void>;
|
|
59
|
+
/** Callbacks that run once the rules have (whether they passed or not); each may add failures with `fail()`. */
|
|
60
|
+
protected after(): Array<(validator: Validator$1) => void | Promise<void>>;
|
|
55
61
|
authorize(): boolean | Promise<boolean>;
|
|
56
62
|
messages(): Record<string, string>;
|
|
57
63
|
attributes(): Record<string, string>;
|
|
58
64
|
/**
|
|
59
65
|
* `const data = await StorePostRequest.validate()` (spec §4.3). Resolves the current request and the
|
|
60
|
-
* ValidationFactory from the active container, runs
|
|
61
|
-
* returns false), then validates request.all() against rules()/messages()/attributes()
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
66
|
+
* ValidationFactory from the active container, runs prepareForValidation(), then authorize() (throwing
|
|
67
|
+
* AuthorizationErrorImpl if it returns false), then validates request.all() against rules()/messages()/attributes(),
|
|
68
|
+
* with stopOnFirstFailure and the after() callbacks, and returns the validated data (or throws the 422
|
|
69
|
+
* ValidationError — see ValidatorImpl.validated()); `request().safe()` reads it afterwards. The data is typed from
|
|
70
|
+
* rules(): write it without a return type and with `as const` (`return { title: 'required|string' } as const`), so
|
|
71
|
+
* its rule strings keep their text; a rules() typed `Rules` gives `Record<string, unknown>`.
|
|
65
72
|
*/
|
|
66
73
|
static validate<F extends FormRequest>(this: new () => F): Promise<ValidatedData<F>>;
|
|
67
74
|
}
|
|
@@ -407,14 +414,19 @@ declare class ValidatorImpl<R extends Rules = Rules> implements Validator$1<R> {
|
|
|
407
414
|
private readonly excludedRuns;
|
|
408
415
|
/** What the rules keep while one pass runs (RuleContext.cache); a new pass starts with a new one. */
|
|
409
416
|
private cache;
|
|
417
|
+
private stopsOnFirstFailure;
|
|
410
418
|
constructor(data: Record<string, unknown>, rules: R, customMessages: Record<string, string>, customAttributes: Record<string, string>, registry: Record<string, BuiltinRule>, errorBagName?: string);
|
|
411
419
|
after(callback: (validator: Validator$1) => void | Promise<void>): this;
|
|
420
|
+
/** Laravel's stopOnFirstFailure(): once a field has failed, no other field is validated; after() callbacks still run. */
|
|
421
|
+
stopOnFirstFailure(stop?: boolean): this;
|
|
412
422
|
/** Lets an after() hook add a failure. */
|
|
413
423
|
fail(field: string, message: string): void;
|
|
414
424
|
passes(): Promise<boolean>;
|
|
415
425
|
fails(): Promise<boolean>;
|
|
416
426
|
errors(): MessageBagImpl;
|
|
417
427
|
validated<T = InferValidatedType<R>>(): Promise<T>;
|
|
428
|
+
/** validated() as a ValidatedInput, for `only()`, `except()` and `merge()`. */
|
|
429
|
+
safe(): Promise<ValidatedInput<InferValidatedType<R>>>;
|
|
418
430
|
/** Takes the field (with its children) out of validation and of the validated data. */
|
|
419
431
|
private exclude;
|
|
420
432
|
/**
|
package/dist/index.js
CHANGED
|
@@ -56,9 +56,18 @@ var InvalidRuleParameterError = class extends Error {
|
|
|
56
56
|
};
|
|
57
57
|
|
|
58
58
|
// FormRequest.ts
|
|
59
|
-
import { HttpRequest, ValidationFactory } from "@orieljs/contracts";
|
|
59
|
+
import { HttpRequest, VALIDATED_INPUT, ValidationFactory } from "@orieljs/contracts";
|
|
60
60
|
import { inject } from "@orieljs/container";
|
|
61
61
|
var FormRequest = class {
|
|
62
|
+
/** Laravel's `$stopOnFirstFailure`: once a field has failed, the others aren't validated. */
|
|
63
|
+
stopOnFirstFailure = false;
|
|
64
|
+
/** Runs first, before authorize() and the rules: change the input here, with `request().merge()`. */
|
|
65
|
+
prepareForValidation() {
|
|
66
|
+
}
|
|
67
|
+
/** Callbacks that run once the rules have (whether they passed or not); each may add failures with `fail()`. */
|
|
68
|
+
after() {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
62
71
|
authorize() {
|
|
63
72
|
return true;
|
|
64
73
|
}
|
|
@@ -70,19 +79,25 @@ var FormRequest = class {
|
|
|
70
79
|
}
|
|
71
80
|
/**
|
|
72
81
|
* `const data = await StorePostRequest.validate()` (spec §4.3). Resolves the current request and the
|
|
73
|
-
* ValidationFactory from the active container, runs
|
|
74
|
-
* returns false), then validates request.all() against rules()/messages()/attributes()
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
82
|
+
* ValidationFactory from the active container, runs prepareForValidation(), then authorize() (throwing
|
|
83
|
+
* AuthorizationErrorImpl if it returns false), then validates request.all() against rules()/messages()/attributes(),
|
|
84
|
+
* with stopOnFirstFailure and the after() callbacks, and returns the validated data (or throws the 422
|
|
85
|
+
* ValidationError — see ValidatorImpl.validated()); `request().safe()` reads it afterwards. The data is typed from
|
|
86
|
+
* rules(): write it without a return type and with `as const` (`return { title: 'required|string' } as const`), so
|
|
87
|
+
* its rule strings keep their text; a rules() typed `Rules` gives `Record<string, unknown>`.
|
|
78
88
|
*/
|
|
79
89
|
static async validate() {
|
|
80
90
|
const instance = new this();
|
|
91
|
+
await instance.prepareForValidation();
|
|
81
92
|
if (!await instance.authorize()) throw new AuthorizationErrorImpl();
|
|
82
93
|
const request = inject(HttpRequest);
|
|
83
94
|
const factory = inject(ValidationFactory);
|
|
84
95
|
const validator = factory.make(request.all(), await instance.rules(), instance.messages(), instance.attributes());
|
|
85
|
-
|
|
96
|
+
validator.stopOnFirstFailure(instance.stopOnFirstFailure);
|
|
97
|
+
for (const callback of instance.after()) validator.after(callback);
|
|
98
|
+
const data = await validator.validated();
|
|
99
|
+
request[VALIDATED_INPUT] = data;
|
|
100
|
+
return data;
|
|
86
101
|
}
|
|
87
102
|
};
|
|
88
103
|
|
|
@@ -1279,9 +1294,13 @@ var fileRules = {
|
|
|
1279
1294
|
import { Arr as Arr7, Str as Str2 } from "@orieljs/support";
|
|
1280
1295
|
var ASCII_RE = /^[\x09\x10\x13\x0A\x0D\x20-\x7E]*$/;
|
|
1281
1296
|
var HEX_COLOR_RE = /^#(?:(?:[0-9a-f]{3}){1,2}|(?:[0-9a-f]{4}){1,2})$/i;
|
|
1282
|
-
var ALPHA_RE = /^[
|
|
1283
|
-
var ALPHA_NUM_RE = /^[
|
|
1284
|
-
var ALPHA_DASH_RE = /^[
|
|
1297
|
+
var ALPHA_RE = /^[\p{L}\p{M}]+$/u;
|
|
1298
|
+
var ALPHA_NUM_RE = /^[\p{L}\p{M}\p{N}]+$/u;
|
|
1299
|
+
var ALPHA_DASH_RE = /^[\p{L}\p{M}\p{N}_-]+$/u;
|
|
1300
|
+
var ASCII_ALPHA_RE = /^[A-Za-z]+$/;
|
|
1301
|
+
var ASCII_ALPHA_NUM_RE = /^[A-Za-z0-9]+$/;
|
|
1302
|
+
var ASCII_ALPHA_DASH_RE = /^[A-Za-z0-9_-]+$/;
|
|
1303
|
+
var alphaRule = (unicode, ascii) => (ctx) => typeof ctx.value === "string" && (ctx.parameters[0] === "ascii" ? ascii : unicode).test(ctx.value);
|
|
1285
1304
|
function isEmail(value) {
|
|
1286
1305
|
const at = value.indexOf("@");
|
|
1287
1306
|
if (at < 1 || value.includes("@", at + 1) || /\s/.test(value)) return false;
|
|
@@ -1386,15 +1405,9 @@ var formatRules = {
|
|
|
1386
1405
|
messageKey: () => "doesnt_end_with",
|
|
1387
1406
|
replacements: listed3
|
|
1388
1407
|
},
|
|
1389
|
-
alpha: { validate: (
|
|
1390
|
-
alpha_num: {
|
|
1391
|
-
|
|
1392
|
-
messageKey: () => "alpha_num"
|
|
1393
|
-
},
|
|
1394
|
-
alpha_dash: {
|
|
1395
|
-
validate: (ctx) => typeof ctx.value === "string" && ALPHA_DASH_RE.test(ctx.value),
|
|
1396
|
-
messageKey: () => "alpha_dash"
|
|
1397
|
-
},
|
|
1408
|
+
alpha: { validate: alphaRule(ALPHA_RE, ASCII_ALPHA_RE), messageKey: () => "alpha" },
|
|
1409
|
+
alpha_num: { validate: alphaRule(ALPHA_NUM_RE, ASCII_ALPHA_NUM_RE), messageKey: () => "alpha_num" },
|
|
1410
|
+
alpha_dash: { validate: alphaRule(ALPHA_DASH_RE, ASCII_ALPHA_DASH_RE), messageKey: () => "alpha_dash" },
|
|
1398
1411
|
lowercase: {
|
|
1399
1412
|
validate: (ctx) => (typeof ctx.value === "string" || typeof ctx.value === "number") && String(ctx.value).toLowerCase() === String(ctx.value),
|
|
1400
1413
|
messageKey: () => "lowercase"
|
|
@@ -2187,7 +2200,7 @@ var Rule = {
|
|
|
2187
2200
|
};
|
|
2188
2201
|
|
|
2189
2202
|
// Validator.ts
|
|
2190
|
-
import { Arr as Arr11 } from "@orieljs/support";
|
|
2203
|
+
import { Arr as Arr11, ValidatedInput } from "@orieljs/support";
|
|
2191
2204
|
var RULE_FLAGS = ["bail", "nullable", "sometimes"];
|
|
2192
2205
|
var SKIPPED_MODIFIERS = new Set(RULE_FLAGS);
|
|
2193
2206
|
var DEPENDENT_RULES = /* @__PURE__ */ new Set([
|
|
@@ -2357,10 +2370,16 @@ var ValidatorImpl = class {
|
|
|
2357
2370
|
excludedRuns = { run: "", next: /* @__PURE__ */ new Map(), excluded: false };
|
|
2358
2371
|
/** What the rules keep while one pass runs (RuleContext.cache); a new pass starts with a new one. */
|
|
2359
2372
|
cache = /* @__PURE__ */ new Map();
|
|
2373
|
+
stopsOnFirstFailure = false;
|
|
2360
2374
|
after(callback) {
|
|
2361
2375
|
this.afterCallbacks.push(callback);
|
|
2362
2376
|
return this;
|
|
2363
2377
|
}
|
|
2378
|
+
/** Laravel's stopOnFirstFailure(): once a field has failed, no other field is validated; after() callbacks still run. */
|
|
2379
|
+
stopOnFirstFailure(stop = true) {
|
|
2380
|
+
this.stopsOnFirstFailure = stop;
|
|
2381
|
+
return this;
|
|
2382
|
+
}
|
|
2364
2383
|
/** Lets an after() hook add a failure. */
|
|
2365
2384
|
fail(field, message) {
|
|
2366
2385
|
this.bag ??= new MessageBagImpl();
|
|
@@ -2394,6 +2413,10 @@ var ValidatorImpl = class {
|
|
|
2394
2413
|
if (this.excluded.size > 0) dropExcluded(nested);
|
|
2395
2414
|
return nested;
|
|
2396
2415
|
}
|
|
2416
|
+
/** validated() as a ValidatedInput, for `only()`, `except()` and `merge()`. */
|
|
2417
|
+
async safe() {
|
|
2418
|
+
return new ValidatedInput(await this.validated());
|
|
2419
|
+
}
|
|
2397
2420
|
/** Takes the field (with its children) out of validation and of the validated data. */
|
|
2398
2421
|
exclude(field) {
|
|
2399
2422
|
this.excluded.add(field);
|
|
@@ -2487,13 +2510,14 @@ var ValidatorImpl = class {
|
|
|
2487
2510
|
async run() {
|
|
2488
2511
|
this.bag = new MessageBagImpl();
|
|
2489
2512
|
this.cache = /* @__PURE__ */ new Map();
|
|
2490
|
-
for (const [pattern, parsed] of this.parsed()) {
|
|
2513
|
+
fields: for (const [pattern, parsed] of this.parsed()) {
|
|
2491
2514
|
const names = parsed.filter((r) => r.kind === "named").map((r) => r.name);
|
|
2492
2515
|
const bail = names.includes("bail");
|
|
2493
2516
|
const nullable = names.includes("nullable");
|
|
2494
2517
|
const sometimes = names.includes("sometimes");
|
|
2495
2518
|
for (const field of this.fieldsFor(pattern)) {
|
|
2496
2519
|
if (this.isExcluded(field)) continue;
|
|
2520
|
+
if (this.stopsOnFirstFailure && !this.bag.isEmpty()) break fields;
|
|
2497
2521
|
const present = Arr11.has(this.data, field);
|
|
2498
2522
|
if (sometimes && !present) continue;
|
|
2499
2523
|
const value = Arr11.get(this.data, field);
|