@h3ravel/validation 0.1.0-alpha.8 → 1.29.0-alpha.11

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.cjs CHANGED
@@ -1,18 +1,18 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
1
2
  let simple_body_validator = require("simple-body-validator");
2
- let __h3ravel_support = require("@h3ravel/support");
3
- let __h3ravel_foundation = require("@h3ravel/foundation");
4
-
3
+ let _h3ravel_support = require("@h3ravel/support");
4
+ let _h3ravel_foundation = require("@h3ravel/foundation");
5
5
  //#region src/ImplicitRule.ts
6
6
  var ImplicitRule = class extends simple_body_validator.ImplicitRule {
7
7
  rules = [];
8
8
  };
9
-
10
9
  //#endregion
11
10
  //#region src/Providers/ValidationServiceProvider.ts
12
11
  /**
13
12
  * Service provider for Validation utilities
14
13
  */
15
14
  var ValidationServiceProvider = class {
15
+ app;
16
16
  registeredCommands;
17
17
  static priority = 895;
18
18
  constructor(app) {
@@ -27,7 +27,6 @@ var ValidationServiceProvider = class {
27
27
  */
28
28
  boot() {}
29
29
  };
30
-
31
30
  //#endregion
32
31
  //#region src/ValidationRule.ts
33
32
  var ValidationRule = class extends simple_body_validator.Rule {
@@ -48,7 +47,6 @@ var ValidationRule = class extends simple_body_validator.Rule {
48
47
  return this.passing;
49
48
  }
50
49
  };
51
-
52
50
  //#endregion
53
51
  //#region src/Rules/ExtendedRules.ts
54
52
  var ExtendedRules = class extends ValidationRule {
@@ -91,12 +89,12 @@ var ExtendedRules = class extends ValidationRule {
91
89
  },
92
90
  {
93
91
  name: "datetime",
94
- validator: (value, parameters = [], attr) => {
92
+ validator: (value, parameters = [], _attr) => {
95
93
  if (typeof value !== "string") return false;
96
94
  const [format] = parameters;
97
95
  if (!format) return !isNaN(Date.parse(value));
98
96
  try {
99
- return new __h3ravel_support.DateTime(value, format, true).isValid();
97
+ return new _h3ravel_support.DateTime(value, format, true).isValid();
100
98
  } catch {
101
99
  return !isNaN(Date.parse(value));
102
100
  }
@@ -138,7 +136,6 @@ var ExtendedRules = class extends ValidationRule {
138
136
  ];
139
137
  validate() {}
140
138
  };
141
-
142
139
  //#endregion
143
140
  //#region src/utilities/MessageBag.ts
144
141
  var MessageBag = class {
@@ -241,7 +238,7 @@ var MessageBag = class {
241
238
  * Wildcard key match.
242
239
  */
243
240
  getMessagesForWildcardKey(key, format) {
244
- const regex = /* @__PURE__ */ new RegExp("^" + key.replace(/\*/g, ".*") + "$");
241
+ const regex = new RegExp("^" + key.replace(/\*/g, ".*") + "$");
245
242
  const result = {};
246
243
  for (const [messageKey, messages] of Object.entries(this.messages)) if (regex.test(messageKey)) result[messageKey] = this.transform(messages, this.checkFormat(format), messageKey);
247
244
  return result;
@@ -352,7 +349,6 @@ var MessageBag = class {
352
349
  return this.toJson();
353
350
  }
354
351
  };
355
-
356
352
  //#endregion
357
353
  //#region src/Validator.ts
358
354
  (0, simple_body_validator.register)("telephone", function(value) {
@@ -545,20 +541,19 @@ var Validator = class Validator {
545
541
  return this;
546
542
  }
547
543
  };
548
-
549
544
  //#endregion
550
545
  //#region src/ValidationException.ts
551
- var ValidationException = class ValidationException extends __h3ravel_foundation.UnprocessableEntityHttpException {
546
+ var ValidationException = class ValidationException extends _h3ravel_foundation.UnprocessableEntityHttpException {
552
547
  validator;
553
548
  response;
554
549
  status = 422;
555
550
  errorBag = "default";
556
551
  redirectTo;
557
- constructor(validator, response$1 = null, errorBag = "default") {
552
+ constructor(validator, response = null, errorBag = "default") {
558
553
  super(ValidationException.summarize(validator));
559
554
  this.name = "ValidationException";
560
555
  this.validator = validator;
561
- this.response = response$1;
556
+ this.response = response;
562
557
  this.errorBag = errorBag;
563
558
  Object.setPrototypeOf(this, ValidationException.prototype);
564
559
  }
@@ -600,7 +595,7 @@ var ValidationException = class ValidationException extends __h3ravel_foundation
600
595
  if (!messages.length || typeof messages[0] !== "string") return "The given data was invalid.";
601
596
  let message = messages.shift();
602
597
  const count = messages.length;
603
- if (count > 0) message += ` (and ${count} more ${__h3ravel_support.Str.plural("error", count)})`;
598
+ if (count > 0) message += ` (and ${count} more ${_h3ravel_support.Str.plural("error", count)})`;
604
599
  return message;
605
600
  }
606
601
  /**
@@ -637,7 +632,6 @@ var ValidationException = class ValidationException extends __h3ravel_foundation
637
632
  return this.response;
638
633
  }
639
634
  };
640
-
641
635
  //#endregion
642
636
  exports.ExtendedRules = ExtendedRules;
643
637
  exports.ImplicitRule = ImplicitRule;
@@ -645,4 +639,4 @@ exports.MessageBag = MessageBag;
645
639
  exports.ValidationException = ValidationException;
646
640
  exports.ValidationRule = ValidationRule;
647
641
  exports.ValidationServiceProvider = ValidationServiceProvider;
648
- exports.Validator = Validator;
642
+ exports.Validator = Validator;
package/dist/index.d.ts CHANGED
@@ -1,340 +1 @@
1
1
  /// <reference path="./app.globals.d.ts" />
2
- import * as _h3ravel_contracts0 from "@h3ravel/contracts";
3
- import { BaseValidationRuleClass, DotPaths, IMessageBag, IRequest, IValidationRule, IValidator, MessagesForRules, MessagesForRules as MessagesForRules$1, RulesForData, RulesForData as RulesForData$1, ValidationMessageProvider, ValidationRuleCallable, ValidationRuleSet } from "@h3ravel/contracts";
4
- import { ImplicitRule as ImplicitRule$1, Rule } from "simple-body-validator";
5
- import { UnprocessableEntityHttpException } from "@h3ravel/foundation";
6
-
7
- //#region src/utilities/MessageBag.d.ts
8
- declare class MessageBag implements IMessageBag {
9
- /**
10
- * All of the registered messages.
11
- */
12
- protected messages: Record<string, string[]>;
13
- /**
14
- * Default format for message output.
15
- */
16
- protected format: string;
17
- /**
18
- * Create a new message bag instance.
19
- */
20
- constructor(messages?: Record<string, string[] | string>);
21
- /**
22
- * Get all message keys.
23
- */
24
- keys(): string[];
25
- /**
26
- * Add a message.
27
- */
28
- add(key: string, message: string): this;
29
- /**
30
- * Add a message conditionally.
31
- */
32
- addIf(condition: boolean, key: string, message: string): this;
33
- /**
34
- * Check uniqueness of key/message pair.
35
- */
36
- protected isUnique(key: string, message: string): boolean;
37
- /**
38
- * Merge another message source into this one.
39
- */
40
- merge(messages: Record<string, string[]> | ValidationMessageProvider): this;
41
- /**
42
- * Determine if messages exist for all given keys.
43
- */
44
- has(key: string | string[] | null): boolean;
45
- /**
46
- * Determine if messages exist for any given key.
47
- */
48
- hasAny(keys?: string | string[]): boolean;
49
- /**
50
- * Determine if messages don't exist for given keys.
51
- */
52
- missing(key: string | string[]): boolean;
53
- /**
54
- * Get the first message for a given key.
55
- */
56
- first(key?: string | null, format?: string | null): string;
57
- /**
58
- * Get all messages for a given key.
59
- */
60
- get(key: string, format?: string | null): string[] | Record<string, string[]>;
61
- /**
62
- * Wildcard key match.
63
- */
64
- protected getMessagesForWildcardKey(key: string, format: string | null): Record<string, string[]>;
65
- /**
66
- * Get all messages.
67
- */
68
- all(format?: string | null): string[];
69
- /**
70
- * Get unique messages.
71
- */
72
- unique(format?: string | null): string[];
73
- /**
74
- * Remove messages for a key.
75
- */
76
- forget(key: string): this;
77
- /**
78
- * Format an array of messages.
79
- */
80
- protected transform(messages: string[], format: string, messageKey: string): string[];
81
- /**
82
- * Get proper format string.
83
- */
84
- protected checkFormat(format?: string | null): string;
85
- /**
86
- * Get raw messages.
87
- */
88
- messagesRaw(): Record<string, string[]>;
89
- /**
90
- * Alias for messagesRaw().
91
- */
92
- getMessages(): Record<string, string[]>;
93
- /**
94
- * Return message bag instance.
95
- */
96
- getMessageBag(): MessageBag;
97
- /**
98
- * Get format string.
99
- */
100
- getFormat(): string;
101
- /**
102
- * Set default message format.
103
- */
104
- setFormat(format?: string): this;
105
- /**
106
- * Empty checks.
107
- */
108
- isEmpty(): boolean;
109
- isNotEmpty(): boolean;
110
- any(): boolean;
111
- /**
112
- * Count total messages.
113
- */
114
- count(): number;
115
- /**
116
- * Array & JSON conversions.
117
- */
118
- toArray(): Record<string, string[]>;
119
- jsonSerialize(): any;
120
- toJson(options?: number): string;
121
- toPrettyJson(): string;
122
- /**
123
- * String representation.
124
- */
125
- toString(): string;
126
- }
127
- //#endregion
128
- //#region src/Validator.d.ts
129
- declare class Validator<D extends Record<string, any> = any, R extends RulesForData$1<D> = RulesForData$1<D>> implements IValidator<D, R> {
130
- #private;
131
- private data;
132
- private rules;
133
- private _errors;
134
- private passing;
135
- private executed;
136
- private instance?;
137
- private errorBagName;
138
- private registeredCustomRules;
139
- private shouldStopOnFirstFailure;
140
- constructor(data: D, rules: R, messages?: Partial<Record<MessagesForRules$1<R>, string>>);
141
- /**
142
- * Validate the data and return the instance
143
- */
144
- static make<D extends Record<string, any>, R extends RulesForData$1<D>>(data: D, rules: R, messages?: Partial<Record<MessagesForRules$1<R>, string>>): Validator<D, R>;
145
- /**
146
- * Run the validator and store results.
147
- */
148
- passes(): Promise<boolean>;
149
- /**
150
- * Opposite of passes()
151
- */
152
- fails(): Promise<boolean>;
153
- /**
154
- * Throw if validation fails, else return executed data
155
- *
156
- * @throws ValidationException if validation fails
157
- */
158
- validate(): Promise<Record<string, any>>;
159
- /**
160
- * Run the validator's rules against its data.
161
- * @param bagName
162
- * @returns
163
- */
164
- validateWithBag(bagName: string): Promise<Record<string, any>>;
165
- /**
166
- * Stop validation on first failure.
167
- */
168
- stopOnFirstFailure(): this;
169
- /**
170
- * Get the data that passed validation.
171
- */
172
- validatedData(): Record<string, any>;
173
- /**
174
- * Return all validated input.
175
- */
176
- validated(): Partial<D>;
177
- /**
178
- * Return a portion of validated input
179
- */
180
- safe(): {
181
- only: (keys: string[]) => Partial<D>;
182
- except: (keys: string[]) => Partial<D>;
183
- };
184
- /**
185
- * Get the message container for the validator.
186
- */
187
- messages(): Promise<Partial<Record<MessagesForRules$1<R>, string>>>;
188
- /**
189
- * Add an after validation callback.
190
- *
191
- * @param callback
192
- */
193
- after<C extends ((validator: Validator<D, R>) => void) | BaseValidationRuleClass>(callback: C | C[]): this;
194
- /**
195
- * Get all errors.
196
- */
197
- errors(): MessageBag;
198
- errorBag(): string;
199
- /**
200
- * Reset validator with new data.
201
- */
202
- setData(data: D): this;
203
- /**
204
- * Set validation rules.
205
- */
206
- setRules(rules: R): this;
207
- /**
208
- * Add a single rule to existing rules.
209
- */
210
- addRule(key: DotPaths<D>, rule: ValidationRuleSet): this;
211
- /**
212
- * Merge additional rules.
213
- */
214
- mergeRules(rules: Record<string, string>): this;
215
- /**
216
- * Get current data.
217
- */
218
- getData(): Record<string, any>;
219
- /**
220
- * Get current rules.
221
- */
222
- getRules(): R;
223
- /**
224
- * Bind all required services here.
225
- */
226
- private bindServices;
227
- private execute;
228
- }
229
- //#endregion
230
- //#region src/ImplicitRule.d.ts
231
- declare abstract class ImplicitRule extends ImplicitRule$1 {
232
- rules: ValidationRuleCallable[];
233
- /**
234
- * Run the validation rule.
235
- */
236
- abstract validate(attribute: string, value: any, fail: (msg: string) => any): void;
237
- /**
238
- * Set the current validator.
239
- */
240
- setValidator?(validator: Validator<any, any>): this;
241
- }
242
- //#endregion
243
- //#region src/Providers/ValidationServiceProvider.d.ts
244
- /**
245
- * Service provider for Validation utilities
246
- */
247
- declare class ValidationServiceProvider {
248
- private app;
249
- registeredCommands?: (new (app: any, kernel: any) => any)[];
250
- static priority: number;
251
- constructor(app: any);
252
- /**
253
- * Register URL services in the container
254
- */
255
- register(): void;
256
- /**
257
- * Boot URL services
258
- */
259
- boot(): void;
260
- }
261
- //#endregion
262
- //#region src/ValidationRule.d.ts
263
- declare abstract class ValidationRule<D extends Record<string, any> = any, R extends RulesForData$1<D> = any> extends Rule implements IValidationRule {
264
- rules: ValidationRuleCallable[];
265
- private passing;
266
- /**
267
- * Run the validation rule.
268
- */
269
- abstract validate(attribute: string, value: any, fail: (msg: string) => any): void;
270
- /**
271
- * Set the current validator.
272
- */
273
- setValidator?(validator: Validator<D, R>): this;
274
- /**
275
- * Set the data under validation.
276
- */
277
- setData(_data: Record<string, any>): this;
278
- passes(value: any, attribute: string): boolean | Promise<boolean>;
279
- }
280
- //#endregion
281
- //#region src/Rules/ExtendedRules.d.ts
282
- declare class ExtendedRules extends ValidationRule {
283
- /**
284
- * The validator instance.
285
- */
286
- protected validator: Validator<any, any>;
287
- setValidator(validator: Validator<any, any>): this;
288
- rules: ValidationRuleCallable[];
289
- validate(): void;
290
- }
291
- //#endregion
292
- //#region src/ValidationException.d.ts
293
- declare class ValidationException extends UnprocessableEntityHttpException {
294
- validator: Validator<any, any>;
295
- response?: any;
296
- status: number;
297
- errorBag: string;
298
- redirectTo?: string;
299
- constructor(validator: Validator<any, any>, response?: any, errorBag?: string);
300
- /**
301
- * Send a custom response body for this exception
302
- *
303
- * @param request
304
- * @returns
305
- */
306
- toResponse(request: IRequest): _h3ravel_contracts0.IResponse | {
307
- message: string;
308
- errors: Record<string, string[]>;
309
- };
310
- /**
311
- * Create a new validation exception from a plain array of messages.
312
- */
313
- static withMessages(messages: Record<string, string[] | string>): ValidationException;
314
- /**
315
- * Create a readable summary message from the validation errors.
316
- */
317
- protected static summarize(validator: Validator<any, any>): string;
318
- /**
319
- * Get all of the validation error messages.
320
- */
321
- errors(): Record<string, string[]>;
322
- /**
323
- * Set the HTTP status code to be used for the response.
324
- */
325
- setStatus(status: number): this;
326
- /**
327
- * Set the error bag on the exception.
328
- */
329
- setErrorBag(errorBag: string): this;
330
- /**
331
- * Set the URL to redirect to on a validation error.
332
- */
333
- setRedirectTo(url: string): this;
334
- /**
335
- * Get the underlying response instance.
336
- */
337
- getResponse(): any;
338
- }
339
- //#endregion
340
- export { ExtendedRules, ImplicitRule, MessageBag, type MessagesForRules, type RulesForData, ValidationException, ValidationRule, ValidationServiceProvider, Validator };
package/dist/index.js CHANGED
@@ -1,18 +1,17 @@
1
1
  import { ImplicitRule as ImplicitRule$1, Rule, make, register, setTranslationObject } from "simple-body-validator";
2
2
  import { DateTime, Str } from "@h3ravel/support";
3
3
  import { UnprocessableEntityHttpException } from "@h3ravel/foundation";
4
-
5
4
  //#region src/ImplicitRule.ts
6
5
  var ImplicitRule = class extends ImplicitRule$1 {
7
6
  rules = [];
8
7
  };
9
-
10
8
  //#endregion
11
9
  //#region src/Providers/ValidationServiceProvider.ts
12
10
  /**
13
11
  * Service provider for Validation utilities
14
12
  */
15
13
  var ValidationServiceProvider = class {
14
+ app;
16
15
  registeredCommands;
17
16
  static priority = 895;
18
17
  constructor(app) {
@@ -27,7 +26,6 @@ var ValidationServiceProvider = class {
27
26
  */
28
27
  boot() {}
29
28
  };
30
-
31
29
  //#endregion
32
30
  //#region src/ValidationRule.ts
33
31
  var ValidationRule = class extends Rule {
@@ -48,7 +46,6 @@ var ValidationRule = class extends Rule {
48
46
  return this.passing;
49
47
  }
50
48
  };
51
-
52
49
  //#endregion
53
50
  //#region src/Rules/ExtendedRules.ts
54
51
  var ExtendedRules = class extends ValidationRule {
@@ -91,7 +88,7 @@ var ExtendedRules = class extends ValidationRule {
91
88
  },
92
89
  {
93
90
  name: "datetime",
94
- validator: (value, parameters = [], attr) => {
91
+ validator: (value, parameters = [], _attr) => {
95
92
  if (typeof value !== "string") return false;
96
93
  const [format] = parameters;
97
94
  if (!format) return !isNaN(Date.parse(value));
@@ -138,7 +135,6 @@ var ExtendedRules = class extends ValidationRule {
138
135
  ];
139
136
  validate() {}
140
137
  };
141
-
142
138
  //#endregion
143
139
  //#region src/utilities/MessageBag.ts
144
140
  var MessageBag = class {
@@ -241,7 +237,7 @@ var MessageBag = class {
241
237
  * Wildcard key match.
242
238
  */
243
239
  getMessagesForWildcardKey(key, format) {
244
- const regex = /* @__PURE__ */ new RegExp("^" + key.replace(/\*/g, ".*") + "$");
240
+ const regex = new RegExp("^" + key.replace(/\*/g, ".*") + "$");
245
241
  const result = {};
246
242
  for (const [messageKey, messages] of Object.entries(this.messages)) if (regex.test(messageKey)) result[messageKey] = this.transform(messages, this.checkFormat(format), messageKey);
247
243
  return result;
@@ -352,7 +348,6 @@ var MessageBag = class {
352
348
  return this.toJson();
353
349
  }
354
350
  };
355
-
356
351
  //#endregion
357
352
  //#region src/Validator.ts
358
353
  register("telephone", function(value) {
@@ -545,7 +540,6 @@ var Validator = class Validator {
545
540
  return this;
546
541
  }
547
542
  };
548
-
549
543
  //#endregion
550
544
  //#region src/ValidationException.ts
551
545
  var ValidationException = class ValidationException extends UnprocessableEntityHttpException {
@@ -554,11 +548,11 @@ var ValidationException = class ValidationException extends UnprocessableEntityH
554
548
  status = 422;
555
549
  errorBag = "default";
556
550
  redirectTo;
557
- constructor(validator, response$1 = null, errorBag = "default") {
551
+ constructor(validator, response = null, errorBag = "default") {
558
552
  super(ValidationException.summarize(validator));
559
553
  this.name = "ValidationException";
560
554
  this.validator = validator;
561
- this.response = response$1;
555
+ this.response = response;
562
556
  this.errorBag = errorBag;
563
557
  Object.setPrototypeOf(this, ValidationException.prototype);
564
558
  }
@@ -637,6 +631,5 @@ var ValidationException = class ValidationException extends UnprocessableEntityH
637
631
  return this.response;
638
632
  }
639
633
  };
640
-
641
634
  //#endregion
642
- export { ExtendedRules, ImplicitRule, MessageBag, ValidationException, ValidationRule, ValidationServiceProvider, Validator };
635
+ export { ExtendedRules, ImplicitRule, MessageBag, ValidationException, ValidationRule, ValidationServiceProvider, Validator };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/validation",
3
- "version": "0.1.0-alpha.8",
3
+ "version": "1.29.0-alpha.11",
4
4
  "description": "Lightweight validation library providing expressive rule-based validation for requests, data objects, and custom logic for H3ravel applications.",
5
5
  "h3ravel": {
6
6
  "providers": [
@@ -46,19 +46,19 @@
46
46
  "builder"
47
47
  ],
48
48
  "dependencies": {
49
- "@h3ravel/shared": "^0.29.0-alpha.8",
50
- "@h3ravel/support": "^0.17.0-alpha.8",
49
+ "@h3ravel/shared": "^1.29.0-alpha.11",
50
+ "@h3ravel/support": "^1.29.0-alpha.11",
51
51
  "simple-body-validator": "^1.3.9",
52
- "@h3ravel/foundation": "^0.1.0-alpha.8"
52
+ "@h3ravel/foundation": "^1.29.0-alpha.11"
53
53
  },
54
54
  "peerDependencies": {
55
- "@h3ravel/core": "^1.22.0-alpha.8",
56
- "@h3ravel/config": "^1.5.0-alpha.8",
57
- "@h3ravel/database": "^11.5.0-alpha.8",
58
- "@h3ravel/contracts": "^0.29.0-alpha.8"
55
+ "@h3ravel/core": "^1.29.0-alpha.11",
56
+ "@h3ravel/config": "^1.29.0-alpha.11",
57
+ "@h3ravel/database": "^11.15.0-alpha.11",
58
+ "@h3ravel/contracts": "^1.29.0-alpha.11"
59
59
  },
60
60
  "devDependencies": {
61
- "typescript": "^5.4.0"
61
+ "typescript": "^6.0.0"
62
62
  },
63
63
  "peerDependenciesMeta": {
64
64
  "@h3ravel/core": {
@@ -72,7 +72,7 @@
72
72
  }
73
73
  },
74
74
  "scripts": {
75
- "build": "tsdown --config-loader unconfig",
75
+ "build": "tsdown --config-loader unrun",
76
76
  "dev": "tsx watch src/index.ts",
77
77
  "start": "node dist/index.js",
78
78
  "lint": "eslint . --ext .ts",