reslib 1.0.3 → 2.0.0

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.
Files changed (62) hide show
  1. package/README.md +9 -5
  2. package/build/auth/index.js +2 -2
  3. package/build/countries/index.js +2 -2
  4. package/build/currency/index.js +2 -2
  5. package/build/currency/session.js +2 -2
  6. package/build/exception/index.d.ts +1901 -0
  7. package/build/exception/index.js +5 -0
  8. package/build/i18n/index.d.ts +12 -4
  9. package/build/i18n/index.js +2 -2
  10. package/build/inputFormatter/index.js +2 -2
  11. package/build/logger/index.js +2 -2
  12. package/build/resources/ResourcePaginationHelper.js +1 -1
  13. package/build/resources/decorators/index.js +1 -1
  14. package/build/resources/fields/index.d.ts +5 -5
  15. package/build/resources/fields/index.js +1 -1
  16. package/build/resources/index.d.ts +1 -1
  17. package/build/resources/index.js +3 -3
  18. package/build/translations/index.d.ts +40 -4
  19. package/build/translations/index.js +2 -2
  20. package/build/translations/validator.en.d.ts +73 -4
  21. package/build/translations/validator.en.js +2 -2
  22. package/build/types/index.d.ts +21 -0
  23. package/build/utils/date/dateHelper.js +2 -2
  24. package/build/utils/date/index.js +2 -2
  25. package/build/utils/index.d.ts +2 -2
  26. package/build/utils/index.js +3 -3
  27. package/build/utils/interpolate.js +1 -1
  28. package/build/utils/isTime.js +1 -1
  29. package/build/utils/numbers.js +2 -2
  30. package/build/utils/object.js +1 -1
  31. package/build/validator/errors/index.d.ts +299 -0
  32. package/build/validator/errors/index.js +1 -0
  33. package/build/validator/index.d.ts +1 -0
  34. package/build/validator/index.js +3 -3
  35. package/build/validator/rules/array.js +2 -2
  36. package/build/validator/rules/boolean.js +2 -2
  37. package/build/validator/rules/date.js +2 -2
  38. package/build/validator/rules/default.js +2 -2
  39. package/build/validator/rules/enum.js +2 -2
  40. package/build/validator/rules/file.js +2 -2
  41. package/build/validator/rules/format.d.ts +182 -13
  42. package/build/validator/rules/format.js +3 -3
  43. package/build/validator/rules/index.d.ts +1 -0
  44. package/build/validator/rules/index.js +3 -3
  45. package/build/validator/rules/multiRules.d.ts +10 -10
  46. package/build/validator/rules/multiRules.js +2 -2
  47. package/build/validator/rules/numeric.d.ts +8 -8
  48. package/build/validator/rules/numeric.js +2 -2
  49. package/build/validator/rules/object.d.ts +71 -0
  50. package/build/validator/rules/object.js +5 -0
  51. package/build/validator/rules/string.d.ts +6 -6
  52. package/build/validator/rules/string.js +2 -2
  53. package/build/validator/rules/target.d.ts +8 -8
  54. package/build/validator/rules/target.js +2 -2
  55. package/build/validator/rules.types.d.ts +167 -0
  56. package/build/validator/rules.types.js +1 -0
  57. package/build/validator/types.d.ts +832 -1286
  58. package/build/validator/validator.d.ts +554 -867
  59. package/build/validator/validator.js +2 -2
  60. package/lib/cjs/exception.js +1 -0
  61. package/lib/esm/exception.mjs +2 -0
  62. package/package.json +254 -244
@@ -0,0 +1,299 @@
1
+ import { ClassConstructor } from '../../types';
2
+ import { ValidatorClassInput, ValidatorClassKeys, ValidatorRuleName } from '../rules.types';
3
+ /**
4
+ * ## Single Validation Error
5
+ *
6
+ * Represents a specific failure of a single validation rule against a single value.
7
+ * This is the most granular error type in the validation system.
8
+ *
9
+ * It contains detailed context about *why* the validation failed, including
10
+ * the rule name, parameters, and the specific value that was rejected.
11
+ *
12
+ */
13
+ export interface ValidatorError extends ValidatorBaseError {
14
+ name: 'ValidatorError';
15
+ /**
16
+ * The specific rule that triggered the failure.
17
+ * Can be a built-in rule name or a custom rule identifier.
18
+ *
19
+ * @example "Email"
20
+ * @example "MinLength"
21
+ * @example "CustomBusinessLogic"
22
+ */
23
+ ruleName: ValidatorRuleName | string;
24
+ /**
25
+ * The parameters that were passed to the validation rule.
26
+ * These define the constraints that were violated.
27
+ *
28
+ * @example [] // For "Required"
29
+ * @example [5] // For "MinLength" (meaning minimum required was 5)
30
+ * @example [18] // For "MinAge"
31
+ */
32
+ ruleParams: any[];
33
+ /**
34
+ * The actual value that failed validation.
35
+ * Captured at the time of validation for debugging or feedback messages.
36
+ *
37
+ * @example "invalid-email"
38
+ * @example 17 (when failure was MinAge: [18])
39
+ */
40
+ value: any;
41
+ /**
42
+ * The logical identifier of the field being validated.
43
+ * Often corresponds to the form input name or API parameter name.
44
+ *
45
+ * @example "userEmail"
46
+ * @example "password_confirm"
47
+ */
48
+ fieldName?: string;
49
+ /**
50
+ * The specific property key on the target object (if validating an class/object).
51
+ *
52
+ * @example "email"
53
+ * @example "age"
54
+ */
55
+ propertyName?: string;
56
+ /**
57
+ * A localized or human-friendly label for the property.
58
+ * Used when generating user-facing error messages.
59
+ *
60
+ * @example "Email Address"
61
+ * @example "Age of User"
62
+ */
63
+ translatedPropertyName?: string;
64
+ /**
65
+ * A machine-readable sub-code specific to this validation failure.
66
+ * Useful for client-side translation lookups.
67
+ *
68
+ * @example "validation.email.invalid"
69
+ * @example "validation.required"
70
+ */
71
+ code?: string;
72
+ /**
73
+ * The severity of the validation failure.
74
+ * - `error`: Blocks operation (default)
75
+ * - `warning`: Alerts user but allows proceeding
76
+ * - `info`: Informational feedback
77
+ */
78
+ severity?: 'error' | 'warning' | 'info';
79
+ }
80
+ /**
81
+ * @internal
82
+ * Helper type defining the keys that are automatically managed by the BaseError factory
83
+ * and thus should not be required when creating a new error.
84
+ */
85
+ type ValidatorCreateErrorPartialKeys = 'message' | '__validatorBaseName' | 'failedAt' | 'success' | 'name' | 'status' | 'statusCode' | 'errorCode';
86
+ /**
87
+ * ## Create Single Error Payload
88
+ *
89
+ * Helper type used when creating a `ValidatorError` using `Validator.createError`.
90
+ * It excludes properties that are automatically generated (like assertions and timestamps).
91
+ */
92
+ export interface ValidatorCreateErrorPayload extends Omit<ValidatorError, ValidatorCreateErrorPartialKeys> {
93
+ }
94
+ /**
95
+ * ## Create Class Error Payload
96
+ *
97
+ * Helper type used when creating a `ValidatorClassError` using `Validator.createClassError`.
98
+ * It excludes properties that are automatically generated.
99
+ */
100
+ export interface ValidatorCreateClassErrorPayload extends Omit<ValidatorClassError, ValidatorCreateErrorPartialKeys> {
101
+ }
102
+ /**
103
+ * ## Create Bulk Error Payload
104
+ *
105
+ * Helper type used when creating a `ValidatorBulkError` using `Validator.createBulkError`.
106
+ * It excludes properties that are automatically generated.
107
+ */
108
+ export interface ValidatorCreateBulkErrorPayload extends Omit<ValidatorBulkError, ValidatorCreateErrorPartialKeys | 'failureCount'> {
109
+ }
110
+ /**
111
+ * ## Validator Bulk Failure Item
112
+ *
113
+ * Represents the failure details for a single item within a bulk validation operation.
114
+ * It maps the specific item from the original array (by index) to the errors it triggered.
115
+ *
116
+ * @template TClass - The class constructor of the items being validated
117
+ * @public
118
+ */
119
+ export interface ValidatorBulkErrorItem<TClass extends ClassConstructor = ClassConstructor> extends ValidatorClassError<TClass> {
120
+ /**
121
+ * The zero-based index of the item in the original input array.
122
+ * Use this to correlate the error back to the specific row or item in the source data.
123
+ *
124
+ * @example 0 (First item failed)
125
+ * @example 5 (Sixth item failed)
126
+ */
127
+ index: number;
128
+ }
129
+ /**
130
+ * ## Validator Bulk Error Details
131
+ *
132
+ * Specialized error type for reporting failures in bulk operations (e.g., validating an array of 100 users).
133
+ * Instead of failing on the first error, this aggregates ALL failures across ALL items.
134
+ *
135
+ * This is crucial for batch processing where you want to report "Items 3, 5, and 10 failed"
136
+ * rather than just "Validation failed".
137
+ *
138
+ * @template TClass - The class constructor of the items being validated
139
+ * @public
140
+ */
141
+ export interface ValidatorBulkError<TClass extends ClassConstructor = ClassConstructor> extends ValidatorBaseError {
142
+ name: 'ValidatorBulkError';
143
+ /**
144
+ * A list of all items that failed validation, with their specific errors.
145
+ * Items that passed validation are not included in this list.
146
+ */
147
+ failures: ValidatorBulkErrorItem<TClass>[];
148
+ /**
149
+ * The total count of items that were processed in the batch.
150
+ * @example 100
151
+ */
152
+ totalCount: number;
153
+ /**
154
+ * The total count of items that failed validation.
155
+ * `failureCount` <= `totalCount`
156
+ * @example 3
157
+ */
158
+ failureCount: number;
159
+ }
160
+ /**
161
+ * ## Validator Base Error
162
+ *
163
+ * The foundational interface that all validation errors implement.
164
+ * It provides a consistent structure for error reporting, including timing,
165
+ * status codes, and identifiers.
166
+ *
167
+ * This ensures that regardless of whether an error is a simple single-value failure
168
+ * or a complex bulk validation failure, consumers can rely on a common set of properties.
169
+ */
170
+ export interface ValidatorBaseError {
171
+ readonly __validatorBaseName: 'ValidatorBaseError';
172
+ /**
173
+ * The primary human-readable error message.
174
+ * If i18n is enabled, this will be the translated string.
175
+ *
176
+ * @example "Value must be at least 5 characters long."
177
+ * @example "L'adresse email est invalide." (Localized)
178
+ */
179
+ message: string;
180
+ /**
181
+ * The timestamp (in milliseconds) when the validation process initiated.
182
+ * Captured via `Date.now()`.
183
+ */
184
+ startTime: number;
185
+ /**
186
+ * The total duration of the validation process in milliseconds.
187
+ * Useful for performance monitoring and latency tracking.
188
+ *
189
+ * @example 15 // Took 15ms
190
+ */
191
+ duration?: number;
192
+ /**
193
+ * The Date object representing when the failure occurred.
194
+ * Typically `new Date()` at moment of error creation.
195
+ */
196
+ failedAt: Date;
197
+ /**
198
+ * Boolean flag used for type narrowing.
199
+ * Always `false` for error objects.
200
+ */
201
+ success: false;
202
+ /**
203
+ * A string status status "error".
204
+ * Consistent with many API response standards (e.g., JSend).
205
+ */
206
+ status: 'error';
207
+ /**
208
+ * HTTP-compatible status code indicating the nature of the error.
209
+ * Defaults to 422 (Unprocessable Entity) for validation failures.
210
+ */
211
+ statusCode: 422;
212
+ /**
213
+ * A constant string code identifying this as a validation failure.
214
+ * distinct from system errors or auth errors.
215
+ */
216
+ errorCode: 'ERR_VALIDATION_FAILED';
217
+ }
218
+ /**
219
+ * ## Validator Class Error
220
+ *
221
+ * Represents validation failures for a complex class instance or object target.
222
+ * Unlike simple errors, a Class Error aggregates multiple failures across different fields
223
+ * of the same object/instance.
224
+ *
225
+ * It provides both a flat list of errors and a mapped structure (`fieldErrors`)
226
+ * for easy UI integration.
227
+ *
228
+ * @template TClass - The class constructor of the validated target
229
+ */
230
+ export interface ValidatorClassError<TClass extends ClassConstructor = ClassConstructor> extends ValidatorBaseError {
231
+ name: 'ValidatorClassError';
232
+ /**
233
+ * Map of field names to their first encountered error message.
234
+ *
235
+ * This property is optimized for UI form handling, providing direct access
236
+ * to error strings keyed by field name.
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * {
241
+ * username: "Username is required",
242
+ * age: "Must be 18 or older"
243
+ * }
244
+ * ```
245
+ */
246
+ fieldErrors: Partial<Record<ValidatorClassKeys<TClass>, string>>;
247
+ /**
248
+ * A flat list of ALL individual validation errors that occurred on this target.
249
+ * Includes nested details for every rule failure.
250
+ */
251
+ errors: Array<ValidatorClassItemError>;
252
+ /**
253
+ * The total number of validation rules that failed for this target.
254
+ */
255
+ failureCount: number;
256
+ /**
257
+ * The original data object that was validated.
258
+ * Attached for context.
259
+ */
260
+ data: ValidatorClassInput<TClass>;
261
+ }
262
+ /**
263
+ * ## Validator Object Error
264
+ *
265
+ * Represents validation failures for a plain object validation operation.
266
+ * Aggregates all field-level failures into a structure optimized for UI display.
267
+ *
268
+ * This is the error variant of the {@link ValidatorObjectResult} discriminated union.
269
+ *
270
+ * @template TObject - The type of object that was validated
271
+ * @public
272
+ */
273
+ export interface ValidatorObjectError<TObject extends object> extends Omit<ValidatorClassError<ClassConstructor>, 'data' | 'fieldErrors'> {
274
+ /**
275
+ * The original data object that failed validation.
276
+ */
277
+ data: TObject;
278
+ /**
279
+ * Map of object keys to their error messages.
280
+ * Enables easy error lookups for UI binding.
281
+ */
282
+ fieldErrors: Partial<Record<keyof TObject | string, string>>;
283
+ }
284
+ /**
285
+ * ## Class Single Error Item
286
+ *
287
+ * A specialized wrapper for a validation error that occurred within a Class/Object validation context.
288
+ * It links the specific `ValidatorError` (`cause`) to the broader class validation failure.
289
+ *
290
+ * Use this to drill down into the specifics of why a particular field on an instance failed.
291
+ */
292
+ export interface ValidatorClassItemError extends ValidatorError {
293
+ /**
294
+ * The underlying simple `ValidatorError` that was generated by the failing rule.
295
+ * Contains the granular details (ruleName, params, etc.).
296
+ */
297
+ cause: ValidatorError;
298
+ }
299
+ export {};
@@ -0,0 +1 @@
1
+ 'use strict';
@@ -1,4 +1,5 @@
1
1
  import 'reflect-metadata';
2
+ export * from './errors';
2
3
  export * from './rules';
3
4
  export * from './types';
4
5
  export * from './validator';