@bolttech/form-engine-core 0.0.1-beta.17 → 0.0.1-beta.19
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 +40 -36
- package/index.esm.js +102 -17
- package/package.json +1 -1
- package/src/helpers/validation.d.ts +27 -0
- package/src/types/schema.d.ts +50 -11
- package/src/types/utility.d.ts +2 -0
- package/src/validations/handler.d.ts +2 -2
- package/src/validations/multiple.d.ts +2 -2
- package/src/validations/namedRule.d.ts +22 -0
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@ Achieve form logic re-usage with forms expressed in json format.
|
|
|
7
7
|
1. [Basic setup](#markdown-header-basic-setup)
|
|
8
8
|
2. [Step by step](#markdown-header-step-by-step)
|
|
9
9
|
3. [Form Features](#markdown-header-available-features)
|
|
10
|
+
|
|
10
11
|
- 3.1. [Validations - Allow form to run validations in the field](#markdown-header-validations)
|
|
11
12
|
- 3.1.1. [Named Validations](#markdown-header-validations)
|
|
12
13
|
- 3.1.2. [Error Messages](#markdown-header-validations)
|
|
@@ -277,16 +278,18 @@ You can also specify the error messages you want.
|
|
|
277
278
|
```json
|
|
278
279
|
{
|
|
279
280
|
"validations": {
|
|
280
|
-
"
|
|
281
|
-
"require": true
|
|
282
|
-
},
|
|
283
|
-
"ON_FIELD_CHANGE": {
|
|
281
|
+
"methods": {
|
|
282
|
+
"require": true,
|
|
284
283
|
"email": true
|
|
284
|
+
},
|
|
285
|
+
"eventMessages": {
|
|
286
|
+
"ON_FIELD_BLUR": ["required"],
|
|
287
|
+
"ON_FIELD_CHANGE": ["email"]
|
|
288
|
+
},
|
|
289
|
+
"messages": {
|
|
290
|
+
"default": "Default error message",
|
|
291
|
+
"email": "Invalid e-mail"
|
|
285
292
|
}
|
|
286
|
-
},
|
|
287
|
-
"errorMessages": {
|
|
288
|
-
"default": "Default error message",
|
|
289
|
-
"email": "Invalid e-mail"
|
|
290
293
|
}
|
|
291
294
|
}
|
|
292
295
|
```
|
|
@@ -303,28 +306,29 @@ If you have a named validation, you can use its name in the error messages, havi
|
|
|
303
306
|
```json
|
|
304
307
|
{
|
|
305
308
|
"validations": {
|
|
306
|
-
"
|
|
309
|
+
"methods": {
|
|
307
310
|
"blurRequire": {
|
|
308
311
|
"require": true
|
|
309
|
-
}
|
|
310
|
-
},
|
|
311
|
-
"ON_FIELD_CHANGE": {
|
|
312
|
+
},
|
|
312
313
|
"email": true,
|
|
313
314
|
"changeRequire": {
|
|
314
315
|
"require": true
|
|
315
316
|
},
|
|
316
317
|
"changeRestOfValidations": {
|
|
317
318
|
"length": 50
|
|
318
|
-
//...
|
|
319
319
|
}
|
|
320
|
+
},
|
|
321
|
+
"eventMessages": {
|
|
322
|
+
"ON_FIELD_BLUR": ["blurRequire"],
|
|
323
|
+
"ON_FIELD_CHANGE": ["email", "changeRequire", "changeRestOfValidations"]
|
|
324
|
+
},
|
|
325
|
+
"messages": {
|
|
326
|
+
"default": "Default error message",
|
|
327
|
+
"email": "Invalid e-mail",
|
|
328
|
+
"blurRequire": "When you blur, this component is required",
|
|
329
|
+
"changeRequire": "You should not leave the field blank",
|
|
330
|
+
"changeRestOfValidations": "You are changing into an incorrect state"
|
|
320
331
|
}
|
|
321
|
-
},
|
|
322
|
-
"errorMessages": {
|
|
323
|
-
"default": "Default error message",
|
|
324
|
-
"email": "Invalid e-mail",
|
|
325
|
-
"blurRequire": "When you blur, this component is required",
|
|
326
|
-
"changeRequire": "You should not leave the field blank",
|
|
327
|
-
"changeRestOfValidations": "You are changing into an incorrect state"
|
|
328
332
|
}
|
|
329
333
|
}
|
|
330
334
|
```
|
|
@@ -624,10 +628,6 @@ Refer to the `TAvailableValidations` types here:
|
|
|
624
628
|
* Allow to define a maximum length for the input to have no error
|
|
625
629
|
*/
|
|
626
630
|
length?: number;
|
|
627
|
-
/**
|
|
628
|
-
* Will look into the input length and send an error if if not greater than this value
|
|
629
|
-
*/
|
|
630
|
-
greaterThan?: number | string;
|
|
631
631
|
|
|
632
632
|
/**
|
|
633
633
|
* Specifies a regular expression pattern that the value should match.
|
|
@@ -669,7 +669,7 @@ Refer to the `TAvailableValidations` types here:
|
|
|
669
669
|
* @param value - The value to be validated.
|
|
670
670
|
* @returns An object with validation results.
|
|
671
671
|
*/
|
|
672
|
-
callback?(value: string | number):
|
|
672
|
+
callback?(value: string | number): boolean;
|
|
673
673
|
|
|
674
674
|
/**
|
|
675
675
|
* Specifies a numeric range for the value.
|
|
@@ -1371,14 +1371,17 @@ Templates are already a great power of form-engine, but we can go further allowi
|
|
|
1371
1371
|
{
|
|
1372
1372
|
"component": "input",
|
|
1373
1373
|
"name": "password",
|
|
1374
|
-
"errorMessages": {
|
|
1375
|
-
"required": "Password is required",
|
|
1376
|
-
"value": "Error value must be varOps.concatenate(${fields.email.value||0},${fields.email2.value||0})"
|
|
1377
|
-
},
|
|
1378
1374
|
"validations": {
|
|
1379
|
-
"
|
|
1375
|
+
"methods": {
|
|
1380
1376
|
"required": true,
|
|
1381
1377
|
"value": "varOps.concatenate(${fields.email.value||0},${fields.email2.value||0})"
|
|
1378
|
+
},
|
|
1379
|
+
"eventMessages": {
|
|
1380
|
+
"ON_FIELD_CHANGE": ["required", "value" ]
|
|
1381
|
+
},
|
|
1382
|
+
"messages": {
|
|
1383
|
+
"required": "Password is required",
|
|
1384
|
+
"value": "Error value must be varOps.concatenate(${fields.email.value||0},${fields.email2.value||0})"
|
|
1382
1385
|
}
|
|
1383
1386
|
},
|
|
1384
1387
|
"props": {
|
|
@@ -1416,9 +1419,12 @@ Since we are already using [templates](#templates) to run our varOps and subscri
|
|
|
1416
1419
|
|
|
1417
1420
|
```json
|
|
1418
1421
|
{
|
|
1419
|
-
"
|
|
1420
|
-
|
|
1421
|
-
"
|
|
1422
|
+
"validations": {
|
|
1423
|
+
...,
|
|
1424
|
+
"messages": {
|
|
1425
|
+
"required": "Password is required",
|
|
1426
|
+
"value": "Error value must be foo_bar"
|
|
1427
|
+
}
|
|
1422
1428
|
}
|
|
1423
1429
|
}
|
|
1424
1430
|
```
|
|
@@ -1576,9 +1582,7 @@ Or Simply using the form reference in a button outside the form, for example:
|
|
|
1576
1582
|
{
|
|
1577
1583
|
const ref = useRef < TFormRefActions > null;
|
|
1578
1584
|
|
|
1579
|
-
return (
|
|
1580
|
-
<Form id="form" ref={ref} onClick={() => ref.current?.stepForward()} />
|
|
1581
|
-
);
|
|
1585
|
+
return <Form id="form" ref={ref} onClick={() => ref.current?.stepForward()} />;
|
|
1582
1586
|
}
|
|
1583
1587
|
|
|
1584
1588
|
// --------------------- OR --------------------- //
|
package/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Subject, Subscription, combineLatest, startWith, groupBy, mergeMap, debounceTime, filter, map } from 'rxjs';
|
|
2
2
|
import creditCardType from 'credit-card-type';
|
|
3
|
-
import { isNumber as isNumber$1, isEqual, get, isNil, set } from 'lodash';
|
|
3
|
+
import { isNumber as isNumber$1, isFunction, isEqual, get, isNil, set } from 'lodash';
|
|
4
4
|
import { getCurrencySymbol } from '@gaignoux/currency';
|
|
5
5
|
|
|
6
6
|
var TMutationEnum;
|
|
@@ -2117,6 +2117,56 @@ const validDate = (value, validations) => {
|
|
|
2117
2117
|
return !isValidDate;
|
|
2118
2118
|
};
|
|
2119
2119
|
|
|
2120
|
+
/**
|
|
2121
|
+
* @internal
|
|
2122
|
+
* Runs a set of validation handlers against a given value.
|
|
2123
|
+
*
|
|
2124
|
+
* @param {unknown} value - The value to be validated.
|
|
2125
|
+
* @param {TValidationMethods} handlers - An object containing validation methods to be applied.
|
|
2126
|
+
* @param {TValidationHandler} validations - An object containing every validation methods to be executed.
|
|
2127
|
+
* @returns {boolean[]} - An array of boolean results for each validation method.
|
|
2128
|
+
*
|
|
2129
|
+
* @example
|
|
2130
|
+
* const handlers = {
|
|
2131
|
+
* max: { max: 10 },
|
|
2132
|
+
* required: true,
|
|
2133
|
+
* email: true
|
|
2134
|
+
* };
|
|
2135
|
+
* const results = run('test@example.com', handlers);
|
|
2136
|
+
* console.log(results); // [false, false, true] (value fails 'max', passes 'required', passes 'email')
|
|
2137
|
+
*/
|
|
2138
|
+
function run$1(value, handlers, validations) {
|
|
2139
|
+
const runner = [];
|
|
2140
|
+
Object.keys(handlers).forEach(rule => {
|
|
2141
|
+
runner.push(validations[rule](value, {
|
|
2142
|
+
[rule]: handlers[rule]
|
|
2143
|
+
}));
|
|
2144
|
+
});
|
|
2145
|
+
return runner;
|
|
2146
|
+
}
|
|
2147
|
+
/**
|
|
2148
|
+
* Validates a given value based on specified validation methods inside a custom named validation.
|
|
2149
|
+
*
|
|
2150
|
+
* @param {unknown} value - The value to be validated.
|
|
2151
|
+
* @param {TValidationMethods} methods - The validation methods to be applied.
|
|
2152
|
+
* @param {TValidationHandler} validations - An object containing every validation methods to be executed.
|
|
2153
|
+
* @returns {boolean} - Returns true if any of the validation methods pass, otherwise false.
|
|
2154
|
+
*
|
|
2155
|
+
* @example
|
|
2156
|
+
* const value = 'example@example.com';
|
|
2157
|
+
* const methods = {
|
|
2158
|
+
* required: true,
|
|
2159
|
+
* email: true
|
|
2160
|
+
* };
|
|
2161
|
+
*
|
|
2162
|
+
* const isValid = validateValue(value, methods);
|
|
2163
|
+
* console.log(isValid); // Output: true
|
|
2164
|
+
*/
|
|
2165
|
+
var namedRule = ((value, methods, validations) => {
|
|
2166
|
+
if (!methods) return false;
|
|
2167
|
+
return run$1(value, methods, validations).some(validation => validation);
|
|
2168
|
+
});
|
|
2169
|
+
|
|
2120
2170
|
/**
|
|
2121
2171
|
* @internal
|
|
2122
2172
|
* An object mapping validation keys to their respective validation functions.
|
|
@@ -2151,7 +2201,6 @@ const validations$1 = {
|
|
|
2151
2201
|
notEmpty,
|
|
2152
2202
|
bool,
|
|
2153
2203
|
exists,
|
|
2154
|
-
greaterThan: () => true,
|
|
2155
2204
|
isNumber,
|
|
2156
2205
|
conditions,
|
|
2157
2206
|
validDate,
|
|
@@ -2175,20 +2224,26 @@ const validations$1 = {
|
|
|
2175
2224
|
* const results = run('test@example.com', handlers);
|
|
2176
2225
|
* console.log(results); // [false, false, true] (value fails 'max', passes 'required', passes 'email')
|
|
2177
2226
|
*/
|
|
2178
|
-
|
|
2227
|
+
function run(value, handlers) {
|
|
2179
2228
|
const runner = [];
|
|
2180
2229
|
Object.keys(handlers).forEach(rule => {
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2230
|
+
let handler;
|
|
2231
|
+
if (isFunction(validations$1[rule])) {
|
|
2232
|
+
handler = validations$1[rule](value, {
|
|
2233
|
+
[rule]: handlers[rule]
|
|
2234
|
+
});
|
|
2235
|
+
} else {
|
|
2236
|
+
handler = namedRule(value, handlers[rule], validations$1);
|
|
2237
|
+
}
|
|
2238
|
+
runner.push(handler);
|
|
2184
2239
|
});
|
|
2185
2240
|
return runner;
|
|
2186
|
-
}
|
|
2241
|
+
}
|
|
2187
2242
|
/**
|
|
2188
2243
|
* Validates that a value meets multiple validation rules.
|
|
2189
2244
|
*
|
|
2190
2245
|
* @param {number | string | boolean} value - The value to be validated.
|
|
2191
|
-
* @param {TValidationMethods}
|
|
2246
|
+
* @param {TValidationMethods} methods - The validation methods object containing the multipleValidations rule set.
|
|
2192
2247
|
* @returns {boolean} - Returns `true` if the value meets the specified multiple validation rules, otherwise `false`.
|
|
2193
2248
|
*
|
|
2194
2249
|
* @example
|
|
@@ -2213,15 +2268,15 @@ const run = (value, handlers) => {
|
|
|
2213
2268
|
* console.log(result2); // false
|
|
2214
2269
|
* ```
|
|
2215
2270
|
*/
|
|
2216
|
-
const multipleValidations = (value,
|
|
2217
|
-
if (!
|
|
2218
|
-
const runner = run(value,
|
|
2271
|
+
const multipleValidations = (value, methods) => {
|
|
2272
|
+
if (!methods.multipleValidations) return false;
|
|
2273
|
+
const runner = run(value, methods.multipleValidations.validations);
|
|
2219
2274
|
const rulesMapper = {
|
|
2220
2275
|
AND: () => runner.every(validation => validation),
|
|
2221
2276
|
OR: () => runner.some(validation => validation),
|
|
2222
2277
|
NOT: () => !runner.every(validation => validation)
|
|
2223
2278
|
};
|
|
2224
|
-
return rulesMapper[
|
|
2279
|
+
return rulesMapper[methods.multipleValidations.rule]();
|
|
2225
2280
|
};
|
|
2226
2281
|
|
|
2227
2282
|
const validations = {
|
|
@@ -2248,7 +2303,6 @@ const validations = {
|
|
|
2248
2303
|
notEmpty,
|
|
2249
2304
|
bool,
|
|
2250
2305
|
exists,
|
|
2251
|
-
greaterThan: () => true,
|
|
2252
2306
|
isNumber,
|
|
2253
2307
|
conditions,
|
|
2254
2308
|
multipleValidations,
|
|
@@ -2273,6 +2327,37 @@ class SafeSubject extends Subject {
|
|
|
2273
2327
|
}
|
|
2274
2328
|
}
|
|
2275
2329
|
|
|
2330
|
+
/**
|
|
2331
|
+
* @internal
|
|
2332
|
+
* Handles the validation of a given value based on specified validation methods and rules.
|
|
2333
|
+
*
|
|
2334
|
+
* @param {string | number | boolean | unknown} value - The value to be validated.
|
|
2335
|
+
* @param {TSchemaValidation} validations - The schema validations to be applied.
|
|
2336
|
+
* @param {TValidationHandler} methods - The validation handler methods.
|
|
2337
|
+
* @param {keyof TValidationMethods} key - The specific key of the validation method to be used.
|
|
2338
|
+
* @returns {boolean} - Returns true if the value passes the validation, otherwise false.
|
|
2339
|
+
*
|
|
2340
|
+
* @example
|
|
2341
|
+
* const value = 'example@example.com';
|
|
2342
|
+
* const validations = {
|
|
2343
|
+
* required: true,
|
|
2344
|
+
* customName: { email: true }
|
|
2345
|
+
* };
|
|
2346
|
+
* const methods = {
|
|
2347
|
+
* email: (value) => /\S+@\S+\.\S+/.test(value)
|
|
2348
|
+
* };
|
|
2349
|
+
* const key = 'required';
|
|
2350
|
+
*
|
|
2351
|
+
* const isValid = handleValidation(value, validations, methods, key);
|
|
2352
|
+
* console.log(isValid); // Output: true
|
|
2353
|
+
*/
|
|
2354
|
+
function handleValidation(value, validations, methods, key) {
|
|
2355
|
+
if (isFunction(methods[key])) {
|
|
2356
|
+
return methods[key](value, validations);
|
|
2357
|
+
}
|
|
2358
|
+
return namedRule(value, validations[key], methods);
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2276
2361
|
/**
|
|
2277
2362
|
* Represents a form field with observables for managing form state, validations, and API requests.
|
|
2278
2363
|
*/
|
|
@@ -2663,7 +2748,7 @@ class FormField {
|
|
|
2663
2748
|
const errors = {};
|
|
2664
2749
|
const schemaValidations = (_a = this.validations) === null || _a === void 0 ? void 0 : _a.methods;
|
|
2665
2750
|
schemaValidations && Object.keys(schemaValidations).forEach(validationKey => {
|
|
2666
|
-
const error =
|
|
2751
|
+
const error = handleValidation(this.value, schemaValidations, validations, validationKey);
|
|
2667
2752
|
// setting valid flag
|
|
2668
2753
|
valid = !error && valid;
|
|
2669
2754
|
// setting error messages
|
|
@@ -2723,7 +2808,7 @@ class FormField {
|
|
|
2723
2808
|
let valid = true;
|
|
2724
2809
|
const preConditions = config.preConditions;
|
|
2725
2810
|
preConditions && Object.keys(preConditions).forEach(validationKey => {
|
|
2726
|
-
const error =
|
|
2811
|
+
const error = handleValidation(this.value, preConditions, validations, validationKey);
|
|
2727
2812
|
valid = valid && !error;
|
|
2728
2813
|
});
|
|
2729
2814
|
if (config.blockRequestWhenInvalid) {
|
|
@@ -3203,7 +3288,7 @@ class FormCore {
|
|
|
3203
3288
|
structVisibility.forEach(structElement => {
|
|
3204
3289
|
if (!structElement.events.includes(event)) return;
|
|
3205
3290
|
Object.keys(structElement.validations).forEach(validationKey => {
|
|
3206
|
-
const error =
|
|
3291
|
+
const error = handleValidation(field.value, structElement.validations, validations, validationKey);
|
|
3207
3292
|
if (Array.isArray(structElement.fields)) {
|
|
3208
3293
|
structElement.fields.forEach(fieldKey => {
|
|
3209
3294
|
if (!this.fields.has(fieldKey)) console.warn(`failed to update visibility onto field ${fieldKey}`);else this.fields.get(fieldKey).visibility = !error;
|
|
@@ -3231,7 +3316,7 @@ class FormCore {
|
|
|
3231
3316
|
structResetValue.forEach(structElement => {
|
|
3232
3317
|
if (!structElement.events.includes(event)) return;
|
|
3233
3318
|
Object.keys(structElement.validations).forEach(validationKey => {
|
|
3234
|
-
const error =
|
|
3319
|
+
const error = handleValidation(field.value, structElement.validations, validations, validationKey);
|
|
3235
3320
|
if (!error) {
|
|
3236
3321
|
if (Array.isArray(structElement.fields)) {
|
|
3237
3322
|
structElement.fields.forEach((fieldKey, index) => {
|
package/package.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TSchemaValidation, TValidationMethods } from '../types/schema';
|
|
2
|
+
import { TValidationHandler } from '../types/utility';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
* Handles the validation of a given value based on specified validation methods and rules.
|
|
6
|
+
*
|
|
7
|
+
* @param {string | number | boolean | unknown} value - The value to be validated.
|
|
8
|
+
* @param {TSchemaValidation} validations - The schema validations to be applied.
|
|
9
|
+
* @param {TValidationHandler} methods - The validation handler methods.
|
|
10
|
+
* @param {keyof TValidationMethods} key - The specific key of the validation method to be used.
|
|
11
|
+
* @returns {boolean} - Returns true if the value passes the validation, otherwise false.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const value = 'example@example.com';
|
|
15
|
+
* const validations = {
|
|
16
|
+
* required: true,
|
|
17
|
+
* customName: { email: true }
|
|
18
|
+
* };
|
|
19
|
+
* const methods = {
|
|
20
|
+
* email: (value) => /\S+@\S+\.\S+/.test(value)
|
|
21
|
+
* };
|
|
22
|
+
* const key = 'required';
|
|
23
|
+
*
|
|
24
|
+
* const isValid = handleValidation(value, validations, methods, key);
|
|
25
|
+
* console.log(isValid); // Output: true
|
|
26
|
+
*/
|
|
27
|
+
export default function handleValidation(value: string | number | boolean | unknown, validations: TSchemaValidation, methods: TValidationHandler, key: keyof TValidationMethods): boolean;
|
package/src/types/schema.d.ts
CHANGED
|
@@ -159,7 +159,7 @@ type TConditionsValidation = {
|
|
|
159
159
|
set: TConditionsValidationSet[];
|
|
160
160
|
conditions?: TConditionsValidation;
|
|
161
161
|
};
|
|
162
|
-
type TAvailableValidations = Omit<TValidationMethods, 'multipleValidations'
|
|
162
|
+
type TAvailableValidations = Omit<TValidationMethods, 'multipleValidations'> | TGenericValidationRule;
|
|
163
163
|
/**
|
|
164
164
|
* @type TMultipleValidation
|
|
165
165
|
* Represents a set of multiple validation methods combined with a logical rule.
|
|
@@ -313,7 +313,6 @@ type TDateValidation = {
|
|
|
313
313
|
* @property {number} [min] - Minimum value or length.
|
|
314
314
|
* @property {TLengthValidation} [length] - Length validation rule.
|
|
315
315
|
* @property {boolean} [required] - Indicates if the field is required.
|
|
316
|
-
* @property {number} [greaterThan] - Value must be greater than this number.
|
|
317
316
|
* @property {unknown} [value] - Specific value to match.
|
|
318
317
|
* @property {string} [regex] - Regular expression for validation.
|
|
319
318
|
* @property {boolean} [email] - Indicates if the value should be a valid email.
|
|
@@ -397,7 +396,6 @@ type TValidationMethods = {
|
|
|
397
396
|
min?: number;
|
|
398
397
|
length?: TLengthValidation;
|
|
399
398
|
required?: boolean;
|
|
400
|
-
greaterThan?: number;
|
|
401
399
|
value?: unknown;
|
|
402
400
|
regex?: string;
|
|
403
401
|
email?: boolean;
|
|
@@ -424,6 +422,47 @@ type TValidationMethods = {
|
|
|
424
422
|
date?: TDateValidation;
|
|
425
423
|
validDate?: TDateFormatsValidation;
|
|
426
424
|
};
|
|
425
|
+
/**
|
|
426
|
+
* @type {Object.<string, TValidationMethods>} TGenericValidationRule
|
|
427
|
+
* Represents a generic validation rule where each key is associated with a set of validation methods.
|
|
428
|
+
*
|
|
429
|
+
* @example
|
|
430
|
+
* const genericValidationRule = {
|
|
431
|
+
* email: {
|
|
432
|
+
* required: true,
|
|
433
|
+
* email: true,
|
|
434
|
+
* },
|
|
435
|
+
* password: {
|
|
436
|
+
* required: true,
|
|
437
|
+
* minLength: 8,
|
|
438
|
+
* },
|
|
439
|
+
* };
|
|
440
|
+
*/
|
|
441
|
+
type TGenericValidationRule = {
|
|
442
|
+
[key: string]: TValidationMethods;
|
|
443
|
+
};
|
|
444
|
+
/**
|
|
445
|
+
* @type {TValidationMethods | TGenericValidationRule} TSchemaValidation
|
|
446
|
+
* Represents the schema validation which can be either a set of validation methods or a generic validation rule.
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* const schemaValidation = {
|
|
450
|
+
* required: true,
|
|
451
|
+
* maxLength: 10,
|
|
452
|
+
* };
|
|
453
|
+
*
|
|
454
|
+
* const genericSchemaValidation = {
|
|
455
|
+
* email: {
|
|
456
|
+
* required: true,
|
|
457
|
+
* email: true,
|
|
458
|
+
* },
|
|
459
|
+
* password: {
|
|
460
|
+
* required: true,
|
|
461
|
+
* minLength: 8,
|
|
462
|
+
* },
|
|
463
|
+
* };
|
|
464
|
+
*/
|
|
465
|
+
type TSchemaValidation = TValidationMethods | TGenericValidationRule;
|
|
427
466
|
/**
|
|
428
467
|
* Formatter types
|
|
429
468
|
* @type TSplitterFormatterValue
|
|
@@ -579,7 +618,7 @@ type TMasks = {
|
|
|
579
618
|
* @type TVisibility
|
|
580
619
|
* Represents the visibility conditions for form fields based on validations.
|
|
581
620
|
*
|
|
582
|
-
* @property {
|
|
621
|
+
* @property {TSchemaValidation} validations - The validation methods to determine visibility.
|
|
583
622
|
* @property {string[] | string} fields - The fields to be shown or hidden based on validations.
|
|
584
623
|
*
|
|
585
624
|
* @example
|
|
@@ -591,7 +630,7 @@ type TMasks = {
|
|
|
591
630
|
* ```
|
|
592
631
|
*/
|
|
593
632
|
type TVisibility = {
|
|
594
|
-
validations:
|
|
633
|
+
validations: TSchemaValidation;
|
|
595
634
|
fields: string[] | string;
|
|
596
635
|
events: Partial<TEvents>[];
|
|
597
636
|
};
|
|
@@ -622,7 +661,7 @@ type TResetValueMethods = TVisibility & {
|
|
|
622
661
|
* @property {OutgoingHttpHeaders} [headers] - The headers for the request.
|
|
623
662
|
* @property {string} [resultPath] - The path to extract the result from the response.
|
|
624
663
|
* @property {unknown} [fallbackValue] - The fallback value if the request fails.
|
|
625
|
-
* @property {
|
|
664
|
+
* @property {TSchemaValidation} preConditions - validation conditions to execute the API call
|
|
626
665
|
* @property {boolean} blockRequestWhenInvalid - blocks request when field validation fails
|
|
627
666
|
*
|
|
628
667
|
* @example
|
|
@@ -647,7 +686,7 @@ type TApiConfig = {
|
|
|
647
686
|
headers?: OutgoingHttpHeaders;
|
|
648
687
|
resultPath?: string;
|
|
649
688
|
fallbackValue?: unknown;
|
|
650
|
-
preConditions?:
|
|
689
|
+
preConditions?: TSchemaValidation;
|
|
651
690
|
blockRequestWhenInvalid?: boolean;
|
|
652
691
|
};
|
|
653
692
|
/**
|
|
@@ -659,7 +698,7 @@ type TProps = Record<string, unknown>;
|
|
|
659
698
|
* @type TValidations
|
|
660
699
|
* Represents the validation configuration for form fields, including methods, event-specific messages, and error messages.
|
|
661
700
|
*
|
|
662
|
-
* @property {
|
|
701
|
+
* @property {TSchemaValidation} methods - The validation methods to be applied.
|
|
663
702
|
* @property {Partial<Record<TEvents, string[]>>} eventMessages - The messages to be displayed for specific validation events.
|
|
664
703
|
* @property {TErrorMessages} messages - The general error messages for validation methods.
|
|
665
704
|
*
|
|
@@ -683,7 +722,7 @@ type TProps = Record<string, unknown>;
|
|
|
683
722
|
* };
|
|
684
723
|
*/
|
|
685
724
|
type TValidations = {
|
|
686
|
-
methods:
|
|
725
|
+
methods: TSchemaValidation;
|
|
687
726
|
eventMessages?: Partial<Record<TEvents, string[]>>;
|
|
688
727
|
messages?: TErrorMessages;
|
|
689
728
|
};
|
|
@@ -700,7 +739,7 @@ type TValidations = {
|
|
|
700
739
|
* };
|
|
701
740
|
* ```
|
|
702
741
|
*/
|
|
703
|
-
type TErrorMessages = Partial<Record<keyof
|
|
742
|
+
type TErrorMessages = Partial<Record<keyof TSchemaValidation & 'default', string>>;
|
|
704
743
|
/**
|
|
705
744
|
* Represents an event configuration with a specific type.
|
|
706
745
|
*
|
|
@@ -746,4 +785,4 @@ type TSchemaFormConfig = {
|
|
|
746
785
|
defaultAPIdebounceTimeMS?: number;
|
|
747
786
|
defaultStateRefreshTimeMS?: number;
|
|
748
787
|
};
|
|
749
|
-
export { TApiConfig, TErrorMessages, TValidations, TMasks, TProps, TResetValueMethods, TFormatters, TValidationMethods, TEvent, TVisibility, TApiEvent, TApiResponse, TSchemaFormConfig, TLengthValidation, TCreditCardMatch, TDocumentValidation, TCallbackValidation, TBetweenValidation, TMaskGeneric, TSplitterFormatterValue, TCurrencyMask, TDateOperatorsValidation, TConditionsValidationSet, TConditionsValidation, TMultipleValidation, TAvailableValidations, TDateValidation, TBetweenDatesValidation, TDateFormatsValidation, TDateInterval, };
|
|
788
|
+
export { TApiConfig, TErrorMessages, TValidations, TMasks, TProps, TResetValueMethods, TFormatters, TValidationMethods, TGenericValidationRule, TSchemaValidation, TEvent, TVisibility, TApiEvent, TApiResponse, TSchemaFormConfig, TLengthValidation, TCreditCardMatch, TDocumentValidation, TCallbackValidation, TBetweenValidation, TMaskGeneric, TSplitterFormatterValue, TCurrencyMask, TDateOperatorsValidation, TConditionsValidationSet, TConditionsValidation, TMultipleValidation, TAvailableValidations, TDateValidation, TBetweenDatesValidation, TDateFormatsValidation, TDateInterval, };
|
package/src/types/utility.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { TValidationMethods } from './schema';
|
|
1
2
|
export type AllowOnly<T, K extends keyof T> = Pick<T, K> & {
|
|
2
3
|
[P in keyof Omit<T, K>]?: never;
|
|
3
4
|
};
|
|
4
5
|
export type OneOf<T, K = keyof T> = K extends keyof T ? AllowOnly<T, K> : never;
|
|
6
|
+
export type TValidationHandler = Record<string, (value: unknown, validations: TValidationMethods) => boolean>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const validations:
|
|
1
|
+
import { TValidationHandler } from '../types/utility';
|
|
2
|
+
export declare const validations: TValidationHandler;
|
|
@@ -3,7 +3,7 @@ import { TValidationMethods } from '../types/schema';
|
|
|
3
3
|
* Validates that a value meets multiple validation rules.
|
|
4
4
|
*
|
|
5
5
|
* @param {number | string | boolean} value - The value to be validated.
|
|
6
|
-
* @param {TValidationMethods}
|
|
6
|
+
* @param {TValidationMethods} methods - The validation methods object containing the multipleValidations rule set.
|
|
7
7
|
* @returns {boolean} - Returns `true` if the value meets the specified multiple validation rules, otherwise `false`.
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
@@ -28,4 +28,4 @@ import { TValidationMethods } from '../types/schema';
|
|
|
28
28
|
* console.log(result2); // false
|
|
29
29
|
* ```
|
|
30
30
|
*/
|
|
31
|
-
export declare const multipleValidations: (value: number | string | boolean,
|
|
31
|
+
export declare const multipleValidations: (value: number | string | boolean, methods: TValidationMethods) => boolean;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TValidationMethods } from '../types/schema';
|
|
2
|
+
import { TValidationHandler } from '../types/utility';
|
|
3
|
+
/**
|
|
4
|
+
* Validates a given value based on specified validation methods inside a custom named validation.
|
|
5
|
+
*
|
|
6
|
+
* @param {unknown} value - The value to be validated.
|
|
7
|
+
* @param {TValidationMethods} methods - The validation methods to be applied.
|
|
8
|
+
* @param {TValidationHandler} validations - An object containing every validation methods to be executed.
|
|
9
|
+
* @returns {boolean} - Returns true if any of the validation methods pass, otherwise false.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const value = 'example@example.com';
|
|
13
|
+
* const methods = {
|
|
14
|
+
* required: true,
|
|
15
|
+
* email: true
|
|
16
|
+
* };
|
|
17
|
+
*
|
|
18
|
+
* const isValid = validateValue(value, methods);
|
|
19
|
+
* console.log(isValid); // Output: true
|
|
20
|
+
*/
|
|
21
|
+
declare const _default: (value: unknown, methods: TValidationMethods, validations: TValidationHandler) => boolean;
|
|
22
|
+
export default _default;
|