@bolttech/form-engine-core 1.0.10 → 1.1.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/CHANGELOG.md +16 -0
- package/credit-card.d.ts +743 -0
- package/credit-card.esm.js +1 -0
- package/currency.d.ts +131 -0
- package/currency.esm.js +1 -0
- package/date.d.ts +582 -0
- package/date.esm.js +1 -0
- package/document.d.ts +527 -0
- package/document.esm.js +1 -0
- package/index.d.ts +52 -5
- package/index.esm.js +1 -4570
- package/lite.d.ts +2393 -0
- package/lite.esm.js +1 -0
- package/package.json +34 -2
- package/src/constants/constants.d.ts +11 -0
- package/src/formatters/creditCard.d.ts +23 -0
- package/src/formatters/custom.d.ts +29 -0
- package/src/formatters/handler.d.ts +2 -0
- package/src/formatters/regex.d.ts +47 -0
- package/src/formatters/splitter.d.ts +17 -0
- package/src/formatters/string.d.ts +88 -0
- package/src/helpers/SafeSubject.d.ts +21 -0
- package/src/helpers/creditCard.d.ts +95 -0
- package/src/helpers/helpers.d.ts +66 -0
- package/src/helpers/lodash-replacements.d.ts +41 -0
- package/src/helpers/validation.d.ts +28 -0
- package/src/index.d.ts +15 -0
- package/src/interfaces/schema.d.ts +161 -0
- package/src/interfaces/state.d.ts +22 -0
- package/src/lite.d.ts +30 -0
- package/src/managers/field.d.ts +339 -0
- package/src/managers/form.d.ts +357 -0
- package/src/managers/formGroup.d.ts +110 -0
- package/src/managers/index.d.ts +3 -0
- package/src/masks/creditCard.d.ts +60 -0
- package/src/masks/currency.d.ts +29 -0
- package/src/masks/generic.d.ts +39 -0
- package/src/masks/handler.d.ts +2 -0
- package/src/masks/string.d.ts +37 -0
- package/src/plugins/credit-card.d.ts +4 -0
- package/src/plugins/currency.d.ts +2 -0
- package/src/plugins/date.d.ts +2 -0
- package/src/plugins/document.d.ts +2 -0
- package/src/registry.d.ts +20 -0
- package/src/types/event.d.ts +175 -0
- package/src/types/form.d.ts +55 -0
- package/src/types/mapper.d.ts +87 -0
- package/src/types/schema.d.ts +1001 -0
- package/src/types/template.d.ts +65 -0
- package/src/types/utility.d.ts +12 -0
- package/src/validations/creditCard.d.ts +52 -0
- package/src/validations/custom.d.ts +27 -0
- package/src/validations/date.d.ts +79 -0
- package/src/validations/document.d.ts +25 -0
- package/src/validations/handler.d.ts +2 -0
- package/src/validations/length.d.ts +39 -0
- package/src/validations/list.d.ts +32 -0
- package/src/validations/logical.d.ts +75 -0
- package/src/validations/multiple.d.ts +31 -0
- package/src/validations/namedRule.d.ts +22 -0
- package/src/validations/number.d.ts +145 -0
- package/src/validations/object.d.ts +44 -0
- package/src/validations/regex.d.ts +217 -0
- package/src/validations/string.d.ts +53 -0
package/date.d.ts
ADDED
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
import { TCurrencyLocalCode } from '@gaignoux/currency';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @type TFormValues<T>
|
|
5
|
+
* Represents the values and state of a form. It has a generic type that allows the importer to determine which type values key will return.
|
|
6
|
+
*
|
|
7
|
+
* @property {Generic Type} values - The current values of the form fields.
|
|
8
|
+
* @property {string[]} erroredFields - A list of field names that have errors.
|
|
9
|
+
* @property {boolean} isValid - Indicates whether the form is valid.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const formValues: TFormValues = {
|
|
14
|
+
* values: { name: 'John', age: 30 },
|
|
15
|
+
* erroredFields: ['email'],
|
|
16
|
+
* isValid: false
|
|
17
|
+
* };
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
type TFormValues<T> = {
|
|
21
|
+
values: T;
|
|
22
|
+
metadata: unknown;
|
|
23
|
+
erroredFields: string[];
|
|
24
|
+
isValid: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** @virtual */
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @type TLengthValidation
|
|
31
|
+
* Represents the validation rules based on the length of the input.
|
|
32
|
+
*
|
|
33
|
+
* @property {'equal' | 'notEqual' | 'less' | 'lessOrEqual' | 'greater' | 'greaterOrEqual'} rule - The rule to apply.
|
|
34
|
+
* @property {number} target - The target value to compare against.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const lengthValidation: TLengthValidation = { rule: 'greaterOrEqual', target: 8 };
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
type TLengthValidation = {
|
|
42
|
+
rule: 'equal' | 'notEqual' | 'less' | 'lessOrEqual' | 'greater' | 'greaterOrEqual';
|
|
43
|
+
target: number;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* @type TCallbackValidation
|
|
47
|
+
* A custom validation function.
|
|
48
|
+
*
|
|
49
|
+
* @param {unknown} value - The value to validate.
|
|
50
|
+
* @returns {boolean} - The result of the validation.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const callbackValidation: TCallbackValidation = (value) => typeof value === 'string';
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
type TCallbackValidation = (value: unknown, formValues: Pick<TFormValues<unknown>, 'values' | 'metadata'>) => boolean;
|
|
58
|
+
/**
|
|
59
|
+
* @type TBetweenValidation
|
|
60
|
+
* Represents validation rules that check if a value is between a range.
|
|
61
|
+
*
|
|
62
|
+
* @property {number} start - The start of the range.
|
|
63
|
+
* @property {number} end - The end of the range.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const betweenValidation: TBetweenValidation = { start: 1, end: 10 };
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
type TBetweenValidation = {
|
|
71
|
+
start: number;
|
|
72
|
+
end: number;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* @type TCreditCardMatch
|
|
76
|
+
* Represents the options for credit card matching.
|
|
77
|
+
*
|
|
78
|
+
* @property {string} numberCard - The credit card number.
|
|
79
|
+
* @property {string[]} availableOptions - The available options for matching.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const creditCardMatch: TCreditCardMatch = { numberCard: '1234', availableOptions: ['Visa', 'MasterCard'] };
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
type TCreditCardMatch = {
|
|
87
|
+
numberCard: string;
|
|
88
|
+
availableOptions: string[];
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* @type TDocumentValidation
|
|
92
|
+
* Represents validation for specific document types.
|
|
93
|
+
*
|
|
94
|
+
* @property {'NIF' | 'NIE' | 'CIF' | 'IBAN'} type - The type of document.
|
|
95
|
+
* @property {TCurrencyLocalCode} [locale] - The locale code for validation.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const documentValidation: TDocumentValidation = { type: 'NIF', locale: 'ES' };
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
type TDocumentValidation = {
|
|
103
|
+
type: 'NIF' | 'NIE' | 'CIF' | 'IBAN';
|
|
104
|
+
locale?: TCurrencyLocalCode;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* @type TDateOperatorsValidation
|
|
108
|
+
* Represents the operators used for date validation conditions.
|
|
109
|
+
*
|
|
110
|
+
* @property {'<' | '>' | '===' | '>=' | '<=' | '!==' | '!!origin'}
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const operator: TDateOperatorsValidation = '<';
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
type TDateOperatorsValidation = '<' | '>' | '===' | '>=' | '<=' | '!==' | '!!origin';
|
|
118
|
+
/**
|
|
119
|
+
* @type TConditionsValidationSet
|
|
120
|
+
* Represents a single validation condition set.
|
|
121
|
+
*
|
|
122
|
+
* @property {boolean} [forceDefinedOrigin] - Flag to force the origin value to be defined.
|
|
123
|
+
* @property {boolean} [forceDefinedTarget] - Flag to force the target value to be defined.
|
|
124
|
+
* @property {string | number | boolean} [origin] - The origin value for the condition.
|
|
125
|
+
* @property {TDateOperatorsValidation} condition - The validation condition operator.
|
|
126
|
+
* @property {string | number | boolean} [target] - The target value for the condition.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const conditionSet: TConditionsValidationSet = {
|
|
131
|
+
* forceDefinedOrigin: true,
|
|
132
|
+
* forceDefinedTarget: true,
|
|
133
|
+
* origin: '2023-01-01',
|
|
134
|
+
* condition: '>',
|
|
135
|
+
* target: '2022-01-01'
|
|
136
|
+
* };
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
type TConditionsValidationSet = {
|
|
140
|
+
forceDefinedOrigin?: boolean;
|
|
141
|
+
forceDefinedTarget?: boolean;
|
|
142
|
+
origin?: string | number | boolean | null;
|
|
143
|
+
condition: TDateOperatorsValidation;
|
|
144
|
+
target?: string | number | boolean;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* @type TConditionsValidation
|
|
148
|
+
* Represents a set of validation conditions.
|
|
149
|
+
*
|
|
150
|
+
* @property {'and' | 'or'} rule - The logical rule to combine the conditions (AND/OR).
|
|
151
|
+
* @property {TConditionsValidationSet[]} set - The array of validation condition sets.
|
|
152
|
+
* @property {TConditionsValidation} [conditions] - Nested conditions for more complex validation logic.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const conditionsValidation: TConditionsValidation = {
|
|
157
|
+
* rule: 'and',
|
|
158
|
+
* set: [
|
|
159
|
+
* {
|
|
160
|
+
* forceDefinedOrigin: true,
|
|
161
|
+
* forceDefinedTarget: true,
|
|
162
|
+
* origin: '2023-01-01',
|
|
163
|
+
* condition: '>',
|
|
164
|
+
* target: '2022-01-01'
|
|
165
|
+
* }
|
|
166
|
+
* ],
|
|
167
|
+
* conditions: {
|
|
168
|
+
* rule: 'or',
|
|
169
|
+
* set: [
|
|
170
|
+
* {
|
|
171
|
+
* origin: true,
|
|
172
|
+
* condition: '!!origin',
|
|
173
|
+
* target: false
|
|
174
|
+
* }
|
|
175
|
+
* ]
|
|
176
|
+
* }
|
|
177
|
+
* };
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
type TConditionsValidation = {
|
|
181
|
+
rule: 'and' | 'or';
|
|
182
|
+
set: TConditionsValidationSet[];
|
|
183
|
+
conditions?: TConditionsValidation;
|
|
184
|
+
};
|
|
185
|
+
type TAvailableValidations = Omit<TValidationMethods, 'multipleValidations'> | TGenericValidationRule;
|
|
186
|
+
/**
|
|
187
|
+
* @type TMultipleValidation
|
|
188
|
+
* Represents a set of multiple validation methods combined with a logical rule.
|
|
189
|
+
*
|
|
190
|
+
* @property {'AND' | 'OR' | 'NOT'} rule - The logical rule to combine the validation methods (AND, OR, NOT).
|
|
191
|
+
* @property {TAvailableValidations} validations - The validation methods to be combined.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const multipleValidation: TMultipleValidation = {
|
|
196
|
+
* rule: 'AND',
|
|
197
|
+
* validations: {
|
|
198
|
+
* required: true,
|
|
199
|
+
* min: 5,
|
|
200
|
+
* max: 10
|
|
201
|
+
* }
|
|
202
|
+
* };
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
type TMultipleValidation = {
|
|
206
|
+
rule: 'AND' | 'OR' | 'NOT';
|
|
207
|
+
validations: TAvailableValidations;
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* @type TBetweenDatesValidation
|
|
211
|
+
* Represents a validation method to check if a value falls between specified dates.
|
|
212
|
+
*
|
|
213
|
+
* @property {TDateValidation[]} [TBetweenDatesValidation] - An array of date validation methods.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* const betweenDatesValidation: TBetweenDatesValidation = [
|
|
218
|
+
* {
|
|
219
|
+
* onlyValidDate: true,
|
|
220
|
+
* operator: '>=',
|
|
221
|
+
* origin: {
|
|
222
|
+
* format: 'MMDDYYYY',
|
|
223
|
+
* intervals: {
|
|
224
|
+
* years: 1
|
|
225
|
+
* }
|
|
226
|
+
* },
|
|
227
|
+
* target: {
|
|
228
|
+
* value: Date.now(),
|
|
229
|
+
* format: 'timestamp',
|
|
230
|
+
* value: '01/01/2023',
|
|
231
|
+
* }
|
|
232
|
+
* },
|
|
233
|
+
* {
|
|
234
|
+
* onlyValidDate: true,
|
|
235
|
+
* operator: '<=',
|
|
236
|
+
* origin: {
|
|
237
|
+
* format: 'MMDDYYYY',
|
|
238
|
+
* intervals: {
|
|
239
|
+
* years: 1
|
|
240
|
+
* }
|
|
241
|
+
* },
|
|
242
|
+
* target: {
|
|
243
|
+
* format: 'timestamp',
|
|
244
|
+
* value: '31/12/2023',
|
|
245
|
+
* }
|
|
246
|
+
* }
|
|
247
|
+
* ];
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
type TBetweenDatesValidation = TDateValidation[];
|
|
251
|
+
/**
|
|
252
|
+
* @type TDateFormatsValidation
|
|
253
|
+
* Represents the supported date formats for validation.
|
|
254
|
+
*
|
|
255
|
+
* @type {'MMDDYYYY' | 'DDMMYYYY' | 'YYYYMMDD' | 'YYYYDDMM' | 'timestamp'}
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```typescript
|
|
259
|
+
* const dateFormat: TDateFormatsValidation = 'MMDDYYYY';
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
type TDateFormatsValidation = 'MMDDYYYY' | 'DDMMYYYY' | 'YYYYMMDD' | 'YYYYDDMM' | 'timestamp';
|
|
263
|
+
type TDateInterval = {
|
|
264
|
+
years?: number;
|
|
265
|
+
months?: number;
|
|
266
|
+
days?: number;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* @type TDateValidation
|
|
270
|
+
* Represents a validation method for date values.
|
|
271
|
+
*
|
|
272
|
+
* @property {boolean} [onlyValidDate] - Flag to validate only if the date is valid.
|
|
273
|
+
* @property {TDateOperatorsValidation} operator - The validation operator to be used.
|
|
274
|
+
* @property {Object} origin - The origin date configuration.
|
|
275
|
+
* @property {string | number} [origin.value] - The origin date value.
|
|
276
|
+
* @property {TDateFormatsValidation} origin.format - The format of the origin date.
|
|
277
|
+
* @property {Object} [origin.intervals] - The intervals to add to the origin date for comparison.
|
|
278
|
+
* @property {number} [origin.intervals.years] - The number of years to add.
|
|
279
|
+
* @property {number} [origin.intervals.months] - The number of months to add.
|
|
280
|
+
* @property {number} [origin.intervals.days] - The number of days to add.
|
|
281
|
+
* @property {Object} [target] - The target date configuration.
|
|
282
|
+
* @property {string | number} target.value - The target date value.
|
|
283
|
+
* @property {TDateFormatsValidation} target.format - The format of the target date.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```typescript
|
|
287
|
+
* const dateValidation: TDateValidation = {
|
|
288
|
+
* onlyValidDate: true,
|
|
289
|
+
* operator: '===',
|
|
290
|
+
* origin: {
|
|
291
|
+
* value: '10/10/2022',
|
|
292
|
+
* format: 'MMDDYYYY',
|
|
293
|
+
* intervals: {
|
|
294
|
+
* years: 1
|
|
295
|
+
* }
|
|
296
|
+
* },
|
|
297
|
+
* target: {
|
|
298
|
+
* value: Date.now(),
|
|
299
|
+
* format: 'timestamp'
|
|
300
|
+
* }
|
|
301
|
+
* };
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
type TDateValidation = {
|
|
305
|
+
onlyValidDate?: boolean;
|
|
306
|
+
operator: TDateOperatorsValidation;
|
|
307
|
+
origin: {
|
|
308
|
+
value?: string | number;
|
|
309
|
+
format: TDateFormatsValidation;
|
|
310
|
+
/**
|
|
311
|
+
* Intervals to compare with the original date.
|
|
312
|
+
*
|
|
313
|
+
* It will use today's date for comparison.
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* origin date = 10/10/2022
|
|
318
|
+
* interval.year = 1
|
|
319
|
+
* operator = '==='
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* It will compare (10/10/2022 + 1) with the current date and check if they are the same
|
|
323
|
+
*/
|
|
324
|
+
intervals?: TDateInterval;
|
|
325
|
+
};
|
|
326
|
+
target?: {
|
|
327
|
+
value: string | number;
|
|
328
|
+
format: TDateFormatsValidation;
|
|
329
|
+
};
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* @type TValidationMethods
|
|
333
|
+
* Represents the various validation methods that can be applied to form fields.
|
|
334
|
+
*
|
|
335
|
+
* @property {number} [max] - Maximum value or length.
|
|
336
|
+
* @property {number} [min] - Minimum value or length.
|
|
337
|
+
* @property {number} [lessThan] - Minimum value or length.
|
|
338
|
+
* @property {number} [greaterThan] - Minimum value or length.
|
|
339
|
+
* @property {TLengthValidation} [length] - Length validation rule.
|
|
340
|
+
* @property {boolean} [required] - Indicates if the field is required.
|
|
341
|
+
* @property {unknown} [value] - Specific value to match.
|
|
342
|
+
* @property {string} [regex] - Regular expression for validation.
|
|
343
|
+
* @property {boolean} [email] - Indicates if the value should be a valid email.
|
|
344
|
+
* @property {boolean} [url] - Indicates if the value should be a valid URL.
|
|
345
|
+
* @property {boolean} [onlyLetters] - Indicates if the value should contain only letters.
|
|
346
|
+
* @property {boolean} [notAllowSpaces] - Indicates if spaces are not allowed.
|
|
347
|
+
* @property {TCallbackValidation} [callback] - Custom validation callback function.
|
|
348
|
+
* @property {boolean} [isNumber] - Indicates if the value should be a number.
|
|
349
|
+
* @property {boolean} [bool] - Indicates if the value should be a boolean or a string that can be converted to a boolean.
|
|
350
|
+
* @property {boolean | string} [exists] - Indicates if the value should exist or match a specific existence condition.
|
|
351
|
+
* @property {boolean} [hasNoExtraSpaces] - Indicates if there should be no extra spaces.
|
|
352
|
+
* @property {boolean} [notEmpty] - Indicates if the field should not be empty.
|
|
353
|
+
* @property {TBetweenValidation} [between] - Validation rule for range between two values.
|
|
354
|
+
* @property {boolean} [sequential] - Indicates if the value should be sequential.
|
|
355
|
+
* @property {boolean} [repeated] - Indicates if the value should not be repeated.
|
|
356
|
+
* @property {string[] | number[]} [includes] - Array of values that should be included.
|
|
357
|
+
* @property {string[]} [isCreditCard] - Array of valid credit card numbers.
|
|
358
|
+
* @property {TCreditCardMatch} [isCreditCodeMatch] - Validation rule for credit card matching.
|
|
359
|
+
* @property {string[]} [isCreditCardAndLength] - Array of valid credit card numbers with length check.
|
|
360
|
+
* @property {TDocumentValidation} [document] - Document validation rule.
|
|
361
|
+
* @property {TConditionsValidation} [conditions] - Conditional validation rules.
|
|
362
|
+
* @property {TMultipleValidation} [multipleValidations] - Multiple validation rules combined with a logical rule.
|
|
363
|
+
* @property {TBetweenDatesValidation} [betweenDates] - Validation rule for checking if a date falls between two dates.
|
|
364
|
+
* @property {TDateValidation} [date] - Validation rule for date values.
|
|
365
|
+
* @property {TDateFormatsValidation} [validDate] - Validation rule for valid date formats.
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```typescript
|
|
369
|
+
* const validationMethods: TValidationMethods = {
|
|
370
|
+
* max: 100,
|
|
371
|
+
* min: 1,
|
|
372
|
+
* lessThan: 1995,
|
|
373
|
+
* greaterThan: 82000,
|
|
374
|
+
* length: { rule: 'greaterOrEqual', target: 8 },
|
|
375
|
+
* required: true,
|
|
376
|
+
* regex: '^[a-zA-Z0-9]+$',
|
|
377
|
+
* email: true,
|
|
378
|
+
* conditions: {
|
|
379
|
+
* rule: 'and',
|
|
380
|
+
* set: [
|
|
381
|
+
* {
|
|
382
|
+
* forceDefinedOrigin: true,
|
|
383
|
+
* forceDefinedTarget: true,
|
|
384
|
+
* origin: '2023-01-01',
|
|
385
|
+
* condition: '>',
|
|
386
|
+
* target: '2022-01-01'
|
|
387
|
+
* }
|
|
388
|
+
* ]
|
|
389
|
+
* },
|
|
390
|
+
* multipleValidations: {
|
|
391
|
+
* rule: 'AND',
|
|
392
|
+
* validations: {
|
|
393
|
+
* required: true,
|
|
394
|
+
* min: 5,
|
|
395
|
+
* max: 10
|
|
396
|
+
* }
|
|
397
|
+
* },
|
|
398
|
+
* betweenDates: {
|
|
399
|
+
* start: Date.parse('2023-01-01'),
|
|
400
|
+
* end: Date.parse('2023-12-31'),
|
|
401
|
+
* isIncludedBoundaries: true
|
|
402
|
+
* },
|
|
403
|
+
* date: {
|
|
404
|
+
* operator: '===',
|
|
405
|
+
* origin: {
|
|
406
|
+
* value: '10/10/2022',
|
|
407
|
+
* format: 'MMDDYYYY',
|
|
408
|
+
* intervals: {
|
|
409
|
+
* years: 1
|
|
410
|
+
* }
|
|
411
|
+
* },
|
|
412
|
+
* target: {
|
|
413
|
+
* value: Date.now(),
|
|
414
|
+
* format: 'timestamp'
|
|
415
|
+
* }
|
|
416
|
+
* },
|
|
417
|
+
* validDate: 'MMDDYYYY'
|
|
418
|
+
* };
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
421
|
+
type TValidationMethods = {
|
|
422
|
+
/** Maximum value or length. */
|
|
423
|
+
max?: number;
|
|
424
|
+
/** Minimum value or length. */
|
|
425
|
+
min?: number;
|
|
426
|
+
/** Minimum value or length. */
|
|
427
|
+
lessThan?: number;
|
|
428
|
+
/** Minimum value or length. */
|
|
429
|
+
greaterThan?: number;
|
|
430
|
+
/** Length validation rule. */
|
|
431
|
+
length?: TLengthValidation;
|
|
432
|
+
/** Indicates if the field is required. */
|
|
433
|
+
required?: boolean;
|
|
434
|
+
/** Specific value to match. */
|
|
435
|
+
value?: unknown;
|
|
436
|
+
/** Regular expression for validation. */
|
|
437
|
+
regex?: string;
|
|
438
|
+
/** Indicates if the value should be a valid email. */
|
|
439
|
+
email?: boolean;
|
|
440
|
+
/** Indicates if the value should be a valid URL. */
|
|
441
|
+
url?: boolean;
|
|
442
|
+
/** Indicates if the value should contain only letters. */
|
|
443
|
+
onlyLetters?: boolean;
|
|
444
|
+
/** Indicates if spaces are not allowed. */
|
|
445
|
+
notAllowSpaces?: boolean;
|
|
446
|
+
/** Custom validation callback function. */
|
|
447
|
+
callback?: TCallbackValidation;
|
|
448
|
+
/** Indicates if the value should be a number. */
|
|
449
|
+
isNumber?: boolean;
|
|
450
|
+
/** Indicates if the value should be a boolean or a string that can be converted to a boolean. */
|
|
451
|
+
bool?: boolean | string;
|
|
452
|
+
/** Indicates if the value should exist or match a specific existence condition. */
|
|
453
|
+
exists?: boolean | string;
|
|
454
|
+
/** Indicates if there should be no extra spaces. */
|
|
455
|
+
hasNoExtraSpaces?: boolean;
|
|
456
|
+
/** Indicates if the field should not be empty. */
|
|
457
|
+
notEmpty?: boolean;
|
|
458
|
+
/** Validation rule for range between two values. */
|
|
459
|
+
between?: TBetweenValidation;
|
|
460
|
+
/** Indicates if the value should be sequential. */
|
|
461
|
+
sequential?: boolean;
|
|
462
|
+
/** Indicates if the value should not be repeated. */
|
|
463
|
+
repeated?: boolean;
|
|
464
|
+
/** Array of values that should be included. */
|
|
465
|
+
includes?: (string | number)[];
|
|
466
|
+
/** Array of valid credit card numbers. */
|
|
467
|
+
isCreditCard?: string[];
|
|
468
|
+
/** Validation rule for credit card matching. */
|
|
469
|
+
isCreditCodeMatch?: TCreditCardMatch;
|
|
470
|
+
/** Array of valid credit card numbers with length check. */
|
|
471
|
+
isCreditCardAndLength?: string[];
|
|
472
|
+
/** Document validation rule. */
|
|
473
|
+
document?: TDocumentValidation;
|
|
474
|
+
/** Conditional validation rules. */
|
|
475
|
+
conditions?: TConditionsValidation;
|
|
476
|
+
/** Multiple validation rules combined with a logical rule. */
|
|
477
|
+
multipleValidations?: TMultipleValidation;
|
|
478
|
+
/** Validation rule for checking if a date falls between two dates. */
|
|
479
|
+
betweenDates?: TBetweenDatesValidation;
|
|
480
|
+
/** Validation rule for date values. */
|
|
481
|
+
date?: TDateValidation;
|
|
482
|
+
/** Validation rule for valid date formats. */
|
|
483
|
+
validDate?: TDateFormatsValidation;
|
|
484
|
+
};
|
|
485
|
+
/**
|
|
486
|
+
* @type {Object.<string, TValidationMethods>} TGenericValidationRule
|
|
487
|
+
* Represents a generic validation rule where each key is associated with a set of validation methods.
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* const genericValidationRule = {
|
|
491
|
+
* email: {
|
|
492
|
+
* required: true,
|
|
493
|
+
* email: true,
|
|
494
|
+
* },
|
|
495
|
+
* password: {
|
|
496
|
+
* required: true,
|
|
497
|
+
* minLength: 8,
|
|
498
|
+
* },
|
|
499
|
+
* };
|
|
500
|
+
*/
|
|
501
|
+
type TGenericValidationRule = Record<string, TValidationMethods>;
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* @function betweenDates
|
|
505
|
+
* Validates if a date value falls between two specified dates.
|
|
506
|
+
*
|
|
507
|
+
* @param {string} value - The date value to be validated in string format.
|
|
508
|
+
* @param {TValidationMethods} validations - The validation methods object containing the betweenDates validation rules.
|
|
509
|
+
* @returns {boolean} - Returns `true` if the date value fails the betweenDates validation, otherwise `false`.
|
|
510
|
+
*
|
|
511
|
+
* @example
|
|
512
|
+
* ```typescript
|
|
513
|
+
* const validations = {
|
|
514
|
+
* betweenDates: [
|
|
515
|
+
* { origin: { value: '2023-01-01', format: 'yyyy-MM-dd' }, operator: '>=' },
|
|
516
|
+
* { origin: { value: '2023-12-31', format: 'yyyy-MM-dd' }, operator: '<=' }
|
|
517
|
+
* ]
|
|
518
|
+
* };
|
|
519
|
+
*
|
|
520
|
+
* const result1 = betweenDates('2023-06-01', validations);
|
|
521
|
+
* console.log(result1); // false (date is within the range)
|
|
522
|
+
*
|
|
523
|
+
* const result2 = betweenDates('2024-01-01', validations);
|
|
524
|
+
* console.log(result2); // true (date is outside the range)
|
|
525
|
+
* ```
|
|
526
|
+
*/
|
|
527
|
+
declare const betweenDates: (value: string, validations: TValidationMethods) => boolean;
|
|
528
|
+
/**
|
|
529
|
+
* @function date
|
|
530
|
+
* Validates a date value based on various date conditions and intervals.
|
|
531
|
+
*
|
|
532
|
+
* @param {string} value - The date value to be validated in string format.
|
|
533
|
+
* @param {TValidationMethods} validations - The validation methods object containing the date validation rules.
|
|
534
|
+
* @returns {boolean} - Returns `true` if the date validation fails, otherwise `false`.
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* ```typescript
|
|
538
|
+
* const validations = {
|
|
539
|
+
* date: {
|
|
540
|
+
* origin: {
|
|
541
|
+
* value: '2023-01-01',
|
|
542
|
+
* format: 'YYYY-MM-DD'
|
|
543
|
+
* },
|
|
544
|
+
* target: {
|
|
545
|
+
* value: '2023-12-31',
|
|
546
|
+
* format: 'YYYY-MM-DD'
|
|
547
|
+
* },
|
|
548
|
+
* operator: '<=',
|
|
549
|
+
* onlyValidDate: true
|
|
550
|
+
* }
|
|
551
|
+
* };
|
|
552
|
+
*
|
|
553
|
+
* const result1 = date('2023-06-01', validations);
|
|
554
|
+
* console.log(result1); // false (date is within the range)
|
|
555
|
+
*
|
|
556
|
+
* const result2 = date('2024-01-01', validations);
|
|
557
|
+
* console.log(result2); // true (date is outside the range)
|
|
558
|
+
* ```
|
|
559
|
+
*/
|
|
560
|
+
declare const date: (value: string | null, validations: TValidationMethods) => boolean;
|
|
561
|
+
/**
|
|
562
|
+
* @function validDate
|
|
563
|
+
* Validates that a date string is a valid date according to the given format.
|
|
564
|
+
*
|
|
565
|
+
* @param {string} value - The date value to be validated in string format.
|
|
566
|
+
* @param {TValidationMethods} validations - The validation methods object containing the validDate rule.
|
|
567
|
+
* @returns {boolean} - Returns `true` if the date is not valid, otherwise `false`.
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* ```typescript
|
|
571
|
+
* const validations = { validDate: 'MM-dd-yyyy' };
|
|
572
|
+
*
|
|
573
|
+
* const result1 = validDate('12-31-2023', validations);
|
|
574
|
+
* console.log(result1); // false (date is valid)
|
|
575
|
+
*
|
|
576
|
+
* const result2 = validDate('02-30-2023', validations);
|
|
577
|
+
* console.log(result2); // true (date is invalid)
|
|
578
|
+
* ```
|
|
579
|
+
*/
|
|
580
|
+
declare const validDate: (value: string, validations: TValidationMethods) => boolean;
|
|
581
|
+
|
|
582
|
+
export { betweenDates, date, validDate };
|
package/date.esm.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e={DDMMYYYY:e=>{if(!e)return"";const t=e.split(e.includes("/")?"/":"-");return`${t[1]}/${t[0]}/${t[2]}`},YYYYMMDD:e=>{if(!e)return"";const t=e.split(e.includes("/")?"/":"-");return`${t[1]}/${t[2]}/${t[0]}`},YYYYDDMM:e=>{if(!e)return"";const t=e.split(e.includes("/")?"/":"-");return`${t[2]}/${t[1]}/${t[0]}`},MMDDYYYY:e=>e,timestamp:e=>new Date(e).toString()},t=(e,t)=>{var a;let i=!1;if(2!=(null===(a=t.betweenDates)||void 0===a?void 0:a.length))return!1;for(const a of t.betweenDates)if(n(e,{date:a})){i=!0;break}return i},n=(t,n)=>{var a,i,r,d,l,o,Y,s,u,D,v,g;if(!(null===(i=null===(a=n.date)||void 0===a?void 0:a.target)||void 0===i?void 0:i.value)&&!(null===(d=null===(r=n.date)||void 0===r?void 0:r.origin)||void 0===d?void 0:d.intervals))return!1;const M=n.date.origin.value||t,c=e[null===(l=n.date)||void 0===l?void 0:l.origin.format](M).toString();let f=new Date(c),$=new Date,w=new Date;if((null===(Y=null===(o=n.date)||void 0===o?void 0:o.target)||void 0===Y?void 0:Y.format)&&(w=new Date(e[null===(u=null===(s=n.date)||void 0===s?void 0:s.target)||void 0===u?void 0:u.format](n.date.target.value).toString()),$=w),n.date.origin.intervals){$=((e,t)=>{const n={years:(e,t)=>new Date(e.setUTCFullYear(e.getUTCFullYear()+t)),months:(e,t)=>new Date(e.setUTCMonth(e.getUTCMonth()+t)),days:(e,t)=>new Date(e.setDate(e.getUTCDate()+t))};return Object.keys(t).reduce((e,a)=>n[a](e,t[a]),new Date(e))})(f,n.date.origin.intervals);const e=new Date;(null===(D=n.date.target)||void 0===D?void 0:D.value)&&w&&(e.setDate(w.getDate()),e.setMonth(w.getMonth())),f=new Date(`${e.getUTCMonth()+1}/${e.getUTCDate()}/${e.getUTCFullYear()}`)}if(n.date.onlyValidDate&&(!($ instanceof Date&&isFinite($))||!($ instanceof Date&&isFinite(f))||M.length<8))return!0;if(((e,t)=>{if(!e.includes("/")&&!e.includes("-")||!t)return!0;const n=e.replace(/[-/]/g,"");return!{DDMMYYYY:/^(\d{2})(\d{2})(\d{4})$/,YYYYMMDD:/^(\d{4})(\d{2})(\d{2})$/,YYYYDDMM:/^(\d{4})(\d{2})(\d{2})$/,MMDDYYYY:/^(\d{2})(\d{2})(\d{4})$/,DMYYYY:/^(\d{1,2})(\d{1,2})(\d{4})$/,YYYYMD:/^(\d{4})(\d{1,2})(\d{1,2})$/,YYYYDM:/^(\d{4})(\d{1,2})(\d{1,2})$/,MDYYYY:/^(\d{1,2})(\d{1,2})(\d{4})$/}[t].test(n)})(c,null===(v=n.date)||void 0===v?void 0:v.origin.format))return!1;const p=f.getTime(),F=$.getTime();return{">":p>F,">=":p>=F,"<":p<F,"<=":p<=F,"===":p===F,"!==":p!==F}[null===(g=n.date)||void 0===g?void 0:g.operator]},a=(t,n)=>{if(!n.validDate||!t)return!1;if(/[^a-zA-Z0-9\s-]/.test(t))return!0;const a=e[n.validDate](t).toString().split(/[/-]/),i=parseInt(a[2],10),r=parseInt(a[0],10)-1,d=parseInt(a[1],10),l=new Date(i,r,d),o=new Date;return o.setFullYear(o.getFullYear()-150),l.getFullYear()<o.getFullYear()||!(l.getFullYear()===i&&l.getMonth()===r&&l.getDate()===d)};Object.assign({},{date:n,betweenDates:t,validDate:a});export{t as betweenDates,n as date,a as validDate};
|