reslib 1.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.
- package/README.md +298 -0
- package/build/auth/index.d.ts +2034 -0
- package/build/auth/index.js +5 -0
- package/build/auth/types.d.ts +465 -0
- package/build/auth/types.js +1 -0
- package/build/countries/countries.d.ts +1454 -0
- package/build/countries/countries.js +1 -0
- package/build/countries/index.d.ts +159 -0
- package/build/countries/index.js +5 -0
- package/build/countries/types.d.ts +65 -0
- package/build/countries/types.js +1 -0
- package/build/currency/currencies.d.ts +8 -0
- package/build/currency/currencies.js +1 -0
- package/build/currency/index.d.ts +51 -0
- package/build/currency/index.js +5 -0
- package/build/currency/session.d.ts +23 -0
- package/build/currency/session.js +5 -0
- package/build/currency/types.d.ts +1039 -0
- package/build/currency/types.js +1 -0
- package/build/currency/utils.d.ts +25 -0
- package/build/currency/utils.js +1 -0
- package/build/i18n/index.d.ts +640 -0
- package/build/i18n/index.js +5 -0
- package/build/inputFormatter/index.d.ts +396 -0
- package/build/inputFormatter/index.js +5 -0
- package/build/inputFormatter/types.d.ts +544 -0
- package/build/inputFormatter/types.js +1 -0
- package/build/logger/index.d.ts +235 -0
- package/build/logger/index.js +5 -0
- package/build/observable/index.d.ts +329 -0
- package/build/observable/index.js +1 -0
- package/build/platform/index.d.ts +32 -0
- package/build/platform/index.js +1 -0
- package/build/resources/ResourcePaginationHelper.d.ts +537 -0
- package/build/resources/ResourcePaginationHelper.js +2 -0
- package/build/resources/decorators/create.decorator.d.ts +20 -0
- package/build/resources/decorators/create.decorator.js +1 -0
- package/build/resources/decorators/index.d.ts +41 -0
- package/build/resources/decorators/index.js +1 -0
- package/build/resources/fields/index.d.ts +33 -0
- package/build/resources/fields/index.js +1 -0
- package/build/resources/filters.d.ts +62 -0
- package/build/resources/filters.js +1 -0
- package/build/resources/index.d.ts +854 -0
- package/build/resources/index.js +6 -0
- package/build/resources/types/filters.d.ts +508 -0
- package/build/resources/types/filters.js +1 -0
- package/build/resources/types/index.d.ts +4138 -0
- package/build/resources/types/index.js +1 -0
- package/build/session/index.d.ts +1474 -0
- package/build/session/index.js +1 -0
- package/build/translations/auth.en.d.ts +3 -0
- package/build/translations/auth.en.js +1 -0
- package/build/translations/countries.en.d.ts +6 -0
- package/build/translations/countries.en.js +1 -0
- package/build/translations/currencies.en.d.ts +5 -0
- package/build/translations/currencies.en.js +1 -0
- package/build/translations/date.en.d.ts +19 -0
- package/build/translations/date.en.js +1 -0
- package/build/translations/index.d.ts +1583 -0
- package/build/translations/index.js +5 -0
- package/build/translations/resources.en.d.ts +6 -0
- package/build/translations/resources.en.js +1 -0
- package/build/translations/validator.en.d.ts +104 -0
- package/build/translations/validator.en.js +5 -0
- package/build/types/date.d.ts +44 -0
- package/build/types/date.js +1 -0
- package/build/types/dictionary.d.ts +29 -0
- package/build/types/dictionary.js +1 -0
- package/build/types/i18n.d.ts +121 -0
- package/build/types/i18n.js +1 -0
- package/build/types/index.d.ts +145 -0
- package/build/types/index.js +1 -0
- package/build/utils/areEquals.d.ts +19 -0
- package/build/utils/areEquals.js +1 -0
- package/build/utils/date/dateHelper.d.ts +371 -0
- package/build/utils/date/dateHelper.js +5 -0
- package/build/utils/date/index.d.ts +212 -0
- package/build/utils/date/index.js +5 -0
- package/build/utils/date/isDateObj.d.ts +14 -0
- package/build/utils/date/isDateObj.js +1 -0
- package/build/utils/debounce.d.ts +52 -0
- package/build/utils/debounce.js +1 -0
- package/build/utils/defaultArray.d.ts +18 -0
- package/build/utils/defaultArray.js +1 -0
- package/build/utils/defaultBool.d.ts +14 -0
- package/build/utils/defaultBool.js +1 -0
- package/build/utils/defaultStr.d.ts +17 -0
- package/build/utils/defaultStr.js +1 -0
- package/build/utils/defaultVal.d.ts +18 -0
- package/build/utils/defaultVal.js +1 -0
- package/build/utils/dom/index.d.ts +65 -0
- package/build/utils/dom/index.js +1 -0
- package/build/utils/dom/isDOMElement.d.ts +11 -0
- package/build/utils/dom/isDOMElement.js +1 -0
- package/build/utils/file/index.d.ts +26 -0
- package/build/utils/file/index.js +1 -0
- package/build/utils/global.d.ts +53 -0
- package/build/utils/global.js +1 -0
- package/build/utils/image.d.ts +56 -0
- package/build/utils/image.js +1 -0
- package/build/utils/index.d.ts +39 -0
- package/build/utils/index.js +6 -0
- package/build/utils/interpolate.d.ts +105 -0
- package/build/utils/interpolate.js +1 -0
- package/build/utils/isEmail.d.ts +57 -0
- package/build/utils/isEmail.js +1 -0
- package/build/utils/isEmpty.d.ts +18 -0
- package/build/utils/isEmpty.js +1 -0
- package/build/utils/isNonNullString.d.ts +17 -0
- package/build/utils/isNonNullString.js +1 -0
- package/build/utils/isNullable.d.ts +7 -0
- package/build/utils/isNullable.js +1 -0
- package/build/utils/isNumber.d.ts +36 -0
- package/build/utils/isNumber.js +1 -0
- package/build/utils/isPrimitive.d.ts +16 -0
- package/build/utils/isPrimitive.js +1 -0
- package/build/utils/isPromise.d.ts +14 -0
- package/build/utils/isPromise.js +1 -0
- package/build/utils/isRegex.d.ts +15 -0
- package/build/utils/isRegex.js +1 -0
- package/build/utils/isTime.d.ts +18 -0
- package/build/utils/isTime.js +1 -0
- package/build/utils/json.d.ts +224 -0
- package/build/utils/json.js +1 -0
- package/build/utils/numbers.d.ts +148 -0
- package/build/utils/numbers.js +5 -0
- package/build/utils/object.d.ts +567 -0
- package/build/utils/object.js +1 -0
- package/build/utils/sort.d.ts +67 -0
- package/build/utils/sort.js +1 -0
- package/build/utils/string.d.ts +165 -0
- package/build/utils/string.js +1 -0
- package/build/utils/stringify.d.ts +23 -0
- package/build/utils/stringify.js +1 -0
- package/build/utils/uniqid.d.ts +18 -0
- package/build/utils/uniqid.js +1 -0
- package/build/utils/uri/index.d.ts +333 -0
- package/build/utils/uri/index.js +2 -0
- package/build/validator/index.d.ts +4 -0
- package/build/validator/index.js +6 -0
- package/build/validator/rules/array.d.ts +848 -0
- package/build/validator/rules/array.js +5 -0
- package/build/validator/rules/boolean.d.ts +87 -0
- package/build/validator/rules/boolean.js +5 -0
- package/build/validator/rules/date.d.ts +551 -0
- package/build/validator/rules/date.js +5 -0
- package/build/validator/rules/default.d.ts +367 -0
- package/build/validator/rules/default.js +5 -0
- package/build/validator/rules/enum.d.ts +155 -0
- package/build/validator/rules/enum.js +5 -0
- package/build/validator/rules/file.d.ts +356 -0
- package/build/validator/rules/file.js +5 -0
- package/build/validator/rules/format.d.ts +2825 -0
- package/build/validator/rules/format.js +6 -0
- package/build/validator/rules/index.d.ts +16 -0
- package/build/validator/rules/index.js +6 -0
- package/build/validator/rules/multiRules.d.ts +475 -0
- package/build/validator/rules/multiRules.js +5 -0
- package/build/validator/rules/numeric.d.ts +1135 -0
- package/build/validator/rules/numeric.js +5 -0
- package/build/validator/rules/string.d.ts +504 -0
- package/build/validator/rules/string.js +5 -0
- package/build/validator/rules/target.d.ts +137 -0
- package/build/validator/rules/target.js +5 -0
- package/build/validator/rules/utils.d.ts +1 -0
- package/build/validator/rules/utils.js +1 -0
- package/build/validator/rulesMarkers.d.ts +11 -0
- package/build/validator/rulesMarkers.js +1 -0
- package/build/validator/types.d.ts +2906 -0
- package/build/validator/types.js +1 -0
- package/build/validator/validator.d.ts +3692 -0
- package/build/validator/validator.js +5 -0
- package/lib/cjs/auth.js +1 -0
- package/lib/cjs/countries.js +1 -0
- package/lib/cjs/currency.js +1 -0
- package/lib/cjs/i18n.js +1 -0
- package/lib/cjs/inputFormatter.js +1 -0
- package/lib/cjs/logger.js +1 -0
- package/lib/cjs/observable.js +1 -0
- package/lib/cjs/platform.js +1 -0
- package/lib/cjs/resources.js +1 -0
- package/lib/cjs/session.js +1 -0
- package/lib/cjs/types.js +1 -0
- package/lib/cjs/utils.js +1 -0
- package/lib/cjs/validator.js +1 -0
- package/lib/esm/auth.mjs +1 -0
- package/lib/esm/countries.mjs +1 -0
- package/lib/esm/currency.mjs +1 -0
- package/lib/esm/i18n.mjs +1 -0
- package/lib/esm/inputFormatter.mjs +1 -0
- package/lib/esm/logger.mjs +1 -0
- package/lib/esm/observable.mjs +1 -0
- package/lib/esm/platform.mjs +1 -0
- package/lib/esm/resources.mjs +1 -0
- package/lib/esm/session.mjs +1 -0
- package/lib/esm/types.mjs +1 -0
- package/lib/esm/utils.mjs +1 -0
- package/lib/esm/validator.mjs +1 -0
- package/package.json +244 -0
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ## OneOf Validation Decorator
|
|
3
|
+
*
|
|
4
|
+
* A powerful validation decorator that implements "OneOf" logic, where validation succeeds
|
|
5
|
+
* if at least one of the provided sub-rules validates successfully. This decorator enables
|
|
6
|
+
* flexible validation scenarios where multiple validation paths are acceptable.
|
|
7
|
+
*
|
|
8
|
+
* ### OneOf Validation Concept
|
|
9
|
+
* Unlike traditional AND validation (where all rules must pass), OneOf validation uses
|
|
10
|
+
* OR logic - validation succeeds when any single sub-rule passes. This is ideal for
|
|
11
|
+
* scenarios where data can be valid in multiple different formats or meet different criteria.
|
|
12
|
+
*
|
|
13
|
+
* ### Key Features
|
|
14
|
+
* - **Flexible Validation**: Accept values that satisfy any one of several validation criteria
|
|
15
|
+
* - **Parallel Execution**: Sub-rules are validated concurrently for optimal performance
|
|
16
|
+
* - **Short-Circuit Success**: Returns immediately when the first sub-rule passes
|
|
17
|
+
* - **Error Aggregation**: Combines error messages from all failed sub-rules when all fail
|
|
18
|
+
* - **Type Safe**: Full TypeScript support with generic context typing
|
|
19
|
+
* - **Decorator Pattern**: Easy to apply to class properties using the `@OneOf()` syntax
|
|
20
|
+
*
|
|
21
|
+
* ### Common Use Cases
|
|
22
|
+
* - **Alternative Contact Methods**: Accept either email OR phone number
|
|
23
|
+
* - **Multiple ID Formats**: Allow UUID, custom ID format, or database-generated ID
|
|
24
|
+
* - **Flexible Input Types**: Accept string OR number for certain fields
|
|
25
|
+
* - **Conditional Business Rules**: Different validation rules based on context
|
|
26
|
+
* - **Format Alternatives**: Accept data in JSON, XML, or custom format
|
|
27
|
+
*
|
|
28
|
+
* ### Validation Behavior
|
|
29
|
+
* - **Success Condition**: At least one sub-rule must return a successful validation result
|
|
30
|
+
* - **Failure Condition**: All sub-rules fail, aggregated errors are returned
|
|
31
|
+
* - **Empty Rules**: If no sub-rules are provided, validation fails with "invalidRule" error
|
|
32
|
+
* - **Rule Processing**: Each sub-rule is validated using the standard `Validator.validate` method
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { OneOf } from 'reslib/validator';
|
|
37
|
+
*
|
|
38
|
+
* class User {
|
|
39
|
+
* // Accept either a valid email OR a valid phone number
|
|
40
|
+
* @OneOf("Email", "PhoneNumber")
|
|
41
|
+
* contact: string;
|
|
42
|
+
*
|
|
43
|
+
* // Accept either a UUID OR a custom ID format
|
|
44
|
+
* @OneOf(
|
|
45
|
+
* "UUID",
|
|
46
|
+
* ({ value }) => value.startsWith('CUSTOM-') || 'Must start with CUSTOM-'
|
|
47
|
+
* )
|
|
48
|
+
* identifier: string;
|
|
49
|
+
*
|
|
50
|
+
* // Accept either a string name OR a number ID
|
|
51
|
+
* @OneOf(
|
|
52
|
+
* "IsNonNullString",
|
|
53
|
+
* "IsNumber"
|
|
54
|
+
* )
|
|
55
|
+
* flexibleId: string | number;
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* // Validation examples
|
|
59
|
+
* const user1 = new User();
|
|
60
|
+
* user1.contact = "user@example.com"; // ✅ Passes (Email rule)
|
|
61
|
+
*
|
|
62
|
+
* const user2 = new User();
|
|
63
|
+
* user2.contact = "+1234567890"; // ✅ Passes (PhoneNumber rule)
|
|
64
|
+
*
|
|
65
|
+
* const user3 = new User();
|
|
66
|
+
* user3.contact = "invalid-input"; // ❌ Fails (both Email and PhoneNumber fail)
|
|
67
|
+
* // Error: "Invalid email format; Invalid phone number format"
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* ### Advanced Usage with Context
|
|
71
|
+
* ```typescript
|
|
72
|
+
* interface ValidationContext {
|
|
73
|
+
* userType: 'admin' | 'user';
|
|
74
|
+
* permissions: string[];
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* class Entity {
|
|
78
|
+
* @OneOf(
|
|
79
|
+
* "Email",
|
|
80
|
+
* "UUID",
|
|
81
|
+
* ({ value, context }) => {
|
|
82
|
+
* // Custom rule that depends on context
|
|
83
|
+
* const ctx = context as ValidationContext;
|
|
84
|
+
* if (ctx?.userType === 'admin') {
|
|
85
|
+
* return value.startsWith('ADMIN-') || 'Admin IDs must start with ADMIN-';
|
|
86
|
+
* }
|
|
87
|
+
* return false; // Skip this rule for non-admins
|
|
88
|
+
* }
|
|
89
|
+
* )
|
|
90
|
+
* identifier: string;
|
|
91
|
+
* }
|
|
92
|
+
*
|
|
93
|
+
* // Context-aware validation
|
|
94
|
+
* const adminEntity = new Entity();
|
|
95
|
+
* adminEntity.identifier = "ADMIN-123"; // ✅ Passes (custom rule for admin)
|
|
96
|
+
*
|
|
97
|
+
* const userEntity = new Entity();
|
|
98
|
+
* userEntity.identifier = "user@example.com"; // ✅ Passes (Email rule)
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* ### Complex Rule Combinations
|
|
102
|
+
* ```typescript
|
|
103
|
+
* class Product {
|
|
104
|
+
* // Accept either a valid URL OR a relative path starting with '/'
|
|
105
|
+
* @OneOf(
|
|
106
|
+
* "IsUrl",
|
|
107
|
+
* ({ value }) => value.startsWith('/') || 'Path must start with /'
|
|
108
|
+
* )
|
|
109
|
+
* imagePath: string;
|
|
110
|
+
*
|
|
111
|
+
* // Accept either a standard email OR an internal company email
|
|
112
|
+
* @OneOf(
|
|
113
|
+
* "Email",
|
|
114
|
+
* ({ value }) => value.endsWith('@company.com') || 'Must be company email'
|
|
115
|
+
* )
|
|
116
|
+
* contactEmail: string;
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* ### Error Handling
|
|
121
|
+
* ```typescript
|
|
122
|
+
* class FlexibleForm {
|
|
123
|
+
* @OneOf("Email", "PhoneNumber", "UUID")
|
|
124
|
+
* identifier: string;
|
|
125
|
+
* }
|
|
126
|
+
*
|
|
127
|
+
* const form = new FlexibleForm();
|
|
128
|
+
* form.identifier = "invalid"; // ❌ All rules fail
|
|
129
|
+
*
|
|
130
|
+
* // When validation fails, you get aggregated error messages:
|
|
131
|
+
* // "Invalid email format; Invalid phone number format; Invalid UUID format"
|
|
132
|
+
*
|
|
133
|
+
* // Use with validateTarget for comprehensive error reporting
|
|
134
|
+
* const result = await Validator.validateTarget(FlexibleForm, {
|
|
135
|
+
* identifier: "invalid"
|
|
136
|
+
* });
|
|
137
|
+
*
|
|
138
|
+
* if (!result.success) {
|
|
139
|
+
* console.log(result.errors[0].message);
|
|
140
|
+
* // Output: "Invalid email format; Invalid phone number format; Invalid UUID format"
|
|
141
|
+
* }
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* ### Integration with Other Decorators
|
|
145
|
+
* ```typescript
|
|
146
|
+
* class ComprehensiveUser {
|
|
147
|
+
* @IsRequired() // Must be present
|
|
148
|
+
* @OneOf( // And must satisfy at least one of these
|
|
149
|
+
* "Email",
|
|
150
|
+
* "PhoneNumber"
|
|
151
|
+
* )
|
|
152
|
+
* contact: string;
|
|
153
|
+
*
|
|
154
|
+
* @IsOptional() // Can be omitted
|
|
155
|
+
* @OneOf( // But if present, must satisfy one of these
|
|
156
|
+
* "IsUrl",
|
|
157
|
+
* ({ value }) => value.startsWith('file://') || 'Must be file:// URL'
|
|
158
|
+
* )
|
|
159
|
+
* avatarUrl?: string;
|
|
160
|
+
* }
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @template Context - Optional type for the validation context object
|
|
164
|
+
*
|
|
165
|
+
* @param rules - Array of validation rules where at least one must pass for validation to succeed
|
|
166
|
+
* @param rules - Each rule can be a string (rule name), object (rule with parameters), or function (custom validation)
|
|
167
|
+
*
|
|
168
|
+
* @returns Property decorator that applies OneOf validation logic to class properties
|
|
169
|
+
*
|
|
170
|
+
* @throws {string} When all sub-rules fail, throws aggregated error messages joined with semicolons
|
|
171
|
+
* @throws {string} When no sub-rules are provided, throws "invalidRule" error
|
|
172
|
+
*
|
|
173
|
+
*
|
|
174
|
+
* @see {@link Validator.validateOneOfRule} - The underlying validation method
|
|
175
|
+
* @see {@link Validator.buildMultiRuleDecorator} - Factory method that creates this decorator
|
|
176
|
+
* @see {@link Validator.validateTarget} - For class-based validation using decorators
|
|
177
|
+
* @see {@link ValidatorValidateMultiRuleOptions} - Type definition for validation options
|
|
178
|
+
*
|
|
179
|
+
* @public
|
|
180
|
+
* @decorator
|
|
181
|
+
*/
|
|
182
|
+
export declare const OneOf: (ruleParameters: import("..").ValidatorDefaultMultiRule<unknown>) => PropertyDecorator;
|
|
183
|
+
/**
|
|
184
|
+
* ## AllOf Validation Decorator
|
|
185
|
+
*
|
|
186
|
+
* A powerful validation decorator that implements "AllOf" logic, where validation succeeds
|
|
187
|
+
* only if ALL of the provided sub-rules validate successfully. This decorator enables
|
|
188
|
+
* strict validation scenarios where multiple validation conditions must all be met simultaneously.
|
|
189
|
+
*
|
|
190
|
+
* ### AllOf Validation Concept
|
|
191
|
+
* Unlike OneOf which uses OR logic (any rule can pass), AllOf uses AND logic - every single
|
|
192
|
+
* sub-rule must pass for validation to succeed. This is perfect for scenarios requiring
|
|
193
|
+
* multiple validation criteria to be satisfied at the same time.
|
|
194
|
+
*
|
|
195
|
+
* ### Key Features
|
|
196
|
+
* - **Strict Validation**: Require values to satisfy ALL specified validation criteria
|
|
197
|
+
* - **Sequential Execution**: Sub-rules are validated in order until one fails
|
|
198
|
+
* - **Early Failure**: Returns immediately when the first sub-rule fails
|
|
199
|
+
* - **Error Aggregation**: Combines error messages from all failed sub-rules
|
|
200
|
+
* - **Type Safe**: Full TypeScript support with generic context typing
|
|
201
|
+
* - **Decorator Pattern**: Easy to apply to class properties using the `@AllOf()` syntax
|
|
202
|
+
*
|
|
203
|
+
* ### Common Use Cases
|
|
204
|
+
* - **Password Requirements**: Must be long enough AND contain numbers AND contain uppercase
|
|
205
|
+
* - **Product Codes**: Must match format AND be correct length AND start with prefix
|
|
206
|
+
* - **Address Validation**: Must be non-empty AND valid characters AND within length limits
|
|
207
|
+
* - **Complex Business Rules**: Multiple conditions that must all be satisfied
|
|
208
|
+
* - **Security Constraints**: Multiple security requirements that must all pass
|
|
209
|
+
*
|
|
210
|
+
* ### Validation Behavior
|
|
211
|
+
* - **Success Condition**: ALL sub-rules must return successful validation results
|
|
212
|
+
* - **Failure Condition**: Any single sub-rule fails, validation stops and fails
|
|
213
|
+
* - **Empty Rules**: If no sub-rules are provided, validation fails with "invalidRule" error
|
|
214
|
+
* - **Rule Processing**: Each sub-rule is validated using the standard `Validator.validate` method
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* // Password must be string, min 8 chars, contain number and uppercase
|
|
219
|
+
* // AllOf ensures ALL these conditions are met simultaneously
|
|
220
|
+
* ['AllOf', 'IsString', [{'MinLength': [8]}], [{Matches: [/.*\\d.*\/]}], [{'Matches': [/.*[A-Z].*\/]}]
|
|
221
|
+
* ```
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```typescript
|
|
225
|
+
* // Product code must start with PROD, be 10 chars, alphanumeric only
|
|
226
|
+
* // AllOf requires ALL criteria to pass
|
|
227
|
+
* ['AllOf', 'IsString', [{'Matches': [/^PROD/]}], [{'Length': [10]}], [{'Matches': [/^[A-Z0-9]+$/]}]]
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* // Address must be required, 5-100 chars, valid characters
|
|
233
|
+
* // AllOf validates that ALL rules succeed
|
|
234
|
+
* ['AllOf', 'IsString', 'IsNotEmpty', ['MinLength', 5], ['MaxLength', 100]]
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* ### Advanced Usage with Context
|
|
238
|
+
* ```typescript
|
|
239
|
+
* interface ValidationContext {
|
|
240
|
+
* environment: 'production' | 'staging' | 'development';
|
|
241
|
+
* strictMode: boolean;
|
|
242
|
+
* }
|
|
243
|
+
*
|
|
244
|
+
* class SecureEntity {
|
|
245
|
+
* @AllOf(
|
|
246
|
+
* "IsString",
|
|
247
|
+
* [{"MinLength": [12]}],
|
|
248
|
+
* ({ value, context }) => {
|
|
249
|
+
* // Additional security rules based on context
|
|
250
|
+
* const ctx = context as ValidationContext;
|
|
251
|
+
* if (ctx?.environment === 'production' && ctx?.strictMode) {
|
|
252
|
+
* return /.*[!@#$%^&*].*\/.test(value) || 'Production requires special characters';
|
|
253
|
+
* }
|
|
254
|
+
* return true; // Skip this rule in non-production or non-strict mode
|
|
255
|
+
* }
|
|
256
|
+
* )
|
|
257
|
+
* secureToken: string;
|
|
258
|
+
* }
|
|
259
|
+
* ```
|
|
260
|
+
*
|
|
261
|
+
* ### Error Handling
|
|
262
|
+
* ```typescript
|
|
263
|
+
* class StrictForm {
|
|
264
|
+
* @AllOf("IsString", [{"MinLength": [5]}], [{"MaxLength": [10]}])
|
|
265
|
+
* code: string;
|
|
266
|
+
* }
|
|
267
|
+
*
|
|
268
|
+
* const form = new StrictForm();
|
|
269
|
+
* form.code = "hi"; // ❌ Fails (too short)
|
|
270
|
+
*
|
|
271
|
+
* // When validation fails, you get the first error encountered:
|
|
272
|
+
* // "Value must be at least 5 characters long"
|
|
273
|
+
*
|
|
274
|
+
* // Use with validateTarget for comprehensive error reporting
|
|
275
|
+
* const result = await Validator.validateTarget(StrictForm, {
|
|
276
|
+
* code: "hi"
|
|
277
|
+
* });
|
|
278
|
+
*
|
|
279
|
+
* if (!result.success) {
|
|
280
|
+
* console.log(result.errors[0].message);
|
|
281
|
+
* // Output: "Value must be at least 5 characters long"
|
|
282
|
+
* }
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* ### Integration with Other Decorators
|
|
286
|
+
* ```typescript
|
|
287
|
+
* class ComprehensiveUser {
|
|
288
|
+
* @IsRequired() // Must be present
|
|
289
|
+
* @AllOf( // And must satisfy ALL of these
|
|
290
|
+
* "IsString",
|
|
291
|
+
* [{"MinLength": [8]}],
|
|
292
|
+
* [{"Matches": [/.*\\d.*\/, { message: 'Must contain a number' }]}]
|
|
293
|
+
* )
|
|
294
|
+
* password: string;
|
|
295
|
+
*
|
|
296
|
+
* @IsOptional() // Can be omitted
|
|
297
|
+
* @AllOf( // But if present, must satisfy ALL of these
|
|
298
|
+
* "IsString",
|
|
299
|
+
* [{"MinLength": [10]}],
|
|
300
|
+
* [{"MaxLength": [50]}]
|
|
301
|
+
* )
|
|
302
|
+
* description?: string;
|
|
303
|
+
* }
|
|
304
|
+
* ```
|
|
305
|
+
*
|
|
306
|
+
* @template Context - Optional type for the validation context object
|
|
307
|
+
*
|
|
308
|
+
* @param rules - Array of validation rules where ALL must pass for validation to succeed
|
|
309
|
+
* @param rules - Each rule can be a string (rule name), object (rule with parameters), or function (custom validation)
|
|
310
|
+
*
|
|
311
|
+
* @returns Property decorator that applies AllOf validation logic to class properties
|
|
312
|
+
*
|
|
313
|
+
* @throws {string} When any sub-rule fails, throws the error message from the first failing rule
|
|
314
|
+
* @throws {string} When no sub-rules are provided, throws "invalidRule" error
|
|
315
|
+
*
|
|
316
|
+
* @see {@link Validator.validateAllOfRule} - The underlying validation method
|
|
317
|
+
* @see {@link Validator.buildMultiRuleDecorator} - Factory method that creates this decorator
|
|
318
|
+
* @see {@link Validator.validateTarget} - For class-based validation using decorators
|
|
319
|
+
* @see {@link ValidatorValidateMultiRuleOptions} - Type definition for validation options
|
|
320
|
+
*
|
|
321
|
+
* @public
|
|
322
|
+
* @decorator
|
|
323
|
+
*/
|
|
324
|
+
export declare const AllOf: (ruleParameters: import("..").ValidatorDefaultMultiRule<unknown>) => PropertyDecorator;
|
|
325
|
+
/**
|
|
326
|
+
* ## ArrayOf Validation Decorator
|
|
327
|
+
*
|
|
328
|
+
* A powerful validation decorator that validates arrays where each element must satisfy
|
|
329
|
+
* ALL of the provided sub-rules. This decorator combines array validation with AllOf logic,
|
|
330
|
+
* ensuring that every item in an array meets multiple validation criteria simultaneously.
|
|
331
|
+
*
|
|
332
|
+
* ### ArrayOf Validation Concept
|
|
333
|
+
* ArrayOf first validates that the value is an array, then applies AllOf validation to each
|
|
334
|
+
* element. This means every single item in the array must pass ALL specified validation rules.
|
|
335
|
+
* It's perfect for validating collections where each item has multiple requirements.
|
|
336
|
+
*
|
|
337
|
+
* ### Key Features
|
|
338
|
+
* - **Array Validation**: Ensures the value is an array before validating elements
|
|
339
|
+
* - **Element-wise AllOf**: Each array element must satisfy ALL sub-rules
|
|
340
|
+
* - **Comprehensive Coverage**: Validates all elements against all rules
|
|
341
|
+
* - **Detailed Error Reporting**: Provides specific error messages for failed elements
|
|
342
|
+
* - **Type Safe**: Full TypeScript support with generic context typing
|
|
343
|
+
* - **Decorator Pattern**: Easy to apply to array properties using the `@ArrayOf()` syntax
|
|
344
|
+
*
|
|
345
|
+
* ### Common Use Cases
|
|
346
|
+
* - **Email Lists**: Array of strings where each must be valid email AND not empty
|
|
347
|
+
* - **Product IDs**: Array of codes where each must match format AND be correct length
|
|
348
|
+
* - **User Permissions**: Array of permission strings with specific format requirements
|
|
349
|
+
* - **Numeric Ranges**: Array of numbers within bounds AND meeting precision requirements
|
|
350
|
+
* - **File Lists**: Array of file paths that must exist AND have valid extensions
|
|
351
|
+
*
|
|
352
|
+
* ### Validation Behavior
|
|
353
|
+
* - **Array Check**: First validates that the value is actually an array
|
|
354
|
+
* - **Element Validation**: Each element is validated against ALL sub-rules
|
|
355
|
+
* - **All-or-Nothing**: Every element must pass all rules for overall validation to succeed
|
|
356
|
+
* - **Empty Arrays**: Empty arrays are considered valid (no elements to validate)
|
|
357
|
+
* - **Rule Processing**: Each sub-rule is applied to each element using `Validator.validate`
|
|
358
|
+
*
|
|
359
|
+
* ### Common Use Cases
|
|
360
|
+
* - **Email Lists**: Each email must be valid format AND not empty
|
|
361
|
+
* - **Product Codes**: Each code must match pattern AND be correct length
|
|
362
|
+
* - **User IDs**: Each ID must be valid format AND within length limits
|
|
363
|
+
* - **Numeric Arrays**: Each number must be within range AND meet precision rules
|
|
364
|
+
* - **Permission Lists**: Each permission must be valid format AND recognized
|
|
365
|
+
*
|
|
366
|
+
* ### Validation Behavior
|
|
367
|
+
* - **Array Validation**: First ensures the value is an array
|
|
368
|
+
* - **Element-wise Validation**: Each array element must pass ALL sub-rules
|
|
369
|
+
* - **Comprehensive Checking**: All elements are validated against all rules
|
|
370
|
+
* - **Empty Arrays**: Valid (no elements means no validation failures)
|
|
371
|
+
* - **Error Details**: Specific error messages indicate which element failed which rule
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```typescript
|
|
375
|
+
* import { ArrayOf } from 'reslib/validator';
|
|
376
|
+
*
|
|
377
|
+
* class User {
|
|
378
|
+
* // Each email in the list must be a valid email AND not empty
|
|
379
|
+
* @ArrayOf("Email", "IsNonNullString")
|
|
380
|
+
* emailAddresses: string[];
|
|
381
|
+
*
|
|
382
|
+
* // Each permission must be a string AND match the permission format
|
|
383
|
+
* @ArrayOf(
|
|
384
|
+
* "IsString",
|
|
385
|
+
* [{Matches: [/^perm\./, {message: 'Permission must start with perm.'}]}]
|
|
386
|
+
* )
|
|
387
|
+
* permissions: string[];
|
|
388
|
+
* }
|
|
389
|
+
*
|
|
390
|
+
* // Validation examples
|
|
391
|
+
* const user1 = new User();
|
|
392
|
+
* user1.emailAddresses = ["user@example.com", "admin@test.com"]; // ✅ Passes
|
|
393
|
+
*
|
|
394
|
+
* const user2 = new User();
|
|
395
|
+
* user2.emailAddresses = ["invalid-email", "user@example.com"]; // ❌ Fails
|
|
396
|
+
* // Error: "Invalid email format (at index 0)"
|
|
397
|
+
* ```
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```typescript
|
|
401
|
+
* class Product {
|
|
402
|
+
* // Each product code must be string, 10 chars, start with PROD, alphanumeric
|
|
403
|
+
* @ArrayOf(
|
|
404
|
+
* "IsString",
|
|
405
|
+
* ["Length", 10, { message: 'Each product code must be exactly 10 characters' }],
|
|
406
|
+
* [{Matches: [/^PROD/, { message: 'Each product code must start with PROD' }]}],
|
|
407
|
+
* [{Matches: [/^[A-Z0-9]+$/, { message: 'Each product code must be alphanumeric' }]}]
|
|
408
|
+
* )
|
|
409
|
+
* productCodes: string[];
|
|
410
|
+
*
|
|
411
|
+
* // Each price must be a positive number within reasonable range
|
|
412
|
+
* @ArrayOf(
|
|
413
|
+
* "IsNumber",
|
|
414
|
+
* [{Min: [0.01, { message: 'Each price must be greater than 0' }]}],
|
|
415
|
+
* [{Max: [999999.99, { message: 'Each price cannot exceed $999,999.99' }]}]
|
|
416
|
+
* )
|
|
417
|
+
* prices: number[];
|
|
418
|
+
* }
|
|
419
|
+
*
|
|
420
|
+
* // Validation examples
|
|
421
|
+
* const product1 = new Product();
|
|
422
|
+
* product1.productCodes = ["PROD123456", "PROD789012"]; // ✅ Passes
|
|
423
|
+
*
|
|
424
|
+
* const product2 = new Product();
|
|
425
|
+
* product2.productCodes = ["PROD123456", "INVALID123"]; // ❌ Fails
|
|
426
|
+
* // Error: "Each product code must start with PROD (at index 1)"
|
|
427
|
+
* ```
|
|
428
|
+
*
|
|
429
|
+
* @example
|
|
430
|
+
* ```typescript
|
|
431
|
+
* class Configuration {
|
|
432
|
+
* // Each tag must be non-empty string AND within length limits AND valid format
|
|
433
|
+
* @ArrayOf(
|
|
434
|
+
* "IsString",
|
|
435
|
+
* ["IsNotEmpty"],
|
|
436
|
+
* [{MinLength: [2]}],
|
|
437
|
+
* [{MaxLength: [50]}],
|
|
438
|
+
* [{Matches: [/^[a-zA-Z0-9_-]+$/, { message: 'Tags can only contain letters, numbers, hyphens, and underscores' }]}]
|
|
439
|
+
* )
|
|
440
|
+
* tags: string[];
|
|
441
|
+
* }
|
|
442
|
+
*
|
|
443
|
+
* // Validation examples
|
|
444
|
+
* const config1 = new Configuration();
|
|
445
|
+
* config1.tags = ["important", "urgent", "review"]; // ✅ Passes
|
|
446
|
+
*
|
|
447
|
+
* const config2 = new Configuration();
|
|
448
|
+
* config2.tags = ["important", "", "review"]; // ❌ Fails
|
|
449
|
+
* // Error: "Tags cannot be empty (at index 1)"
|
|
450
|
+
*
|
|
451
|
+
* const config3 = new Configuration();
|
|
452
|
+
* config3.tags = ["important", "tag with spaces", "review"]; // ❌ Fails
|
|
453
|
+
* // Error: "Tags can only contain letters, numbers, hyphens, and underscores (at index 1)"
|
|
454
|
+
* ```
|
|
455
|
+
*
|
|
456
|
+
* @template Context - Optional type for the validation context object
|
|
457
|
+
*
|
|
458
|
+
* @param rules - Array of validation rules where ALL must pass for each array element
|
|
459
|
+
* @param rules - Each rule can be a string (rule name), object (rule with parameters), or function (custom validation)
|
|
460
|
+
*
|
|
461
|
+
* @returns Property decorator that applies ArrayOf validation logic to array properties
|
|
462
|
+
*
|
|
463
|
+
* @throws {string} When value is not an array, throws "Value must be an array" error
|
|
464
|
+
* @throws {string} When any array element fails any sub-rule, throws detailed error message
|
|
465
|
+
* @throws {string} When no sub-rules are provided, throws "invalidRule" error
|
|
466
|
+
*
|
|
467
|
+
* @see {@link Validator.validateArrayOfRule} - The underlying validation method
|
|
468
|
+
* @see {@link Validator.buildMultiRuleDecorator} - Factory method that creates this decorator
|
|
469
|
+
* @see {@link Validator.validateTarget} - For class-based validation using decorators
|
|
470
|
+
* @see {@link ValidatorValidateMultiRuleOptions} - Type definition for validation options
|
|
471
|
+
*
|
|
472
|
+
* @public
|
|
473
|
+
* @decorator
|
|
474
|
+
*/
|
|
475
|
+
export declare const ArrayOf: (ruleParameters: import("..").ValidatorDefaultMultiRule<unknown>) => PropertyDecorator;
|