@h3ravel/validation 0.1.0-alpha.1

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.
@@ -0,0 +1,340 @@
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 };