@bolttech/form-engine-core 0.0.1-beta.18 → 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/index.esm.js +66 -75
- package/package.json +1 -1
- package/src/helpers/{validation/handler.d.ts → validation.d.ts} +2 -2
- package/src/types/schema.d.ts +1 -1
- package/src/types/utility.d.ts +2 -0
- package/src/validations/handler.d.ts +1 -2
- package/src/validations/namedRule.d.ts +3 -1
- package/src/helpers/validation/runner.d.ts +0 -21
package/index.esm.js
CHANGED
|
@@ -2135,7 +2135,7 @@ const validDate = (value, validations) => {
|
|
|
2135
2135
|
* const results = run('test@example.com', handlers);
|
|
2136
2136
|
* console.log(results); // [false, false, true] (value fails 'max', passes 'required', passes 'email')
|
|
2137
2137
|
*/
|
|
2138
|
-
function
|
|
2138
|
+
function run$1(value, handlers, validations) {
|
|
2139
2139
|
const runner = [];
|
|
2140
2140
|
Object.keys(handlers).forEach(rule => {
|
|
2141
2141
|
runner.push(validations[rule](value, {
|
|
@@ -2144,6 +2144,28 @@ function runner(value, handlers, validations) {
|
|
|
2144
2144
|
});
|
|
2145
2145
|
return runner;
|
|
2146
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
|
+
});
|
|
2147
2169
|
|
|
2148
2170
|
/**
|
|
2149
2171
|
* @internal
|
|
@@ -2155,7 +2177,7 @@ function runner(value, handlers, validations) {
|
|
|
2155
2177
|
* const isValid = validations.max(5, { max: 10 });
|
|
2156
2178
|
* console.log(isValid); // false (5 is not greater than 10)
|
|
2157
2179
|
*/
|
|
2158
|
-
const validations$
|
|
2180
|
+
const validations$1 = {
|
|
2159
2181
|
max,
|
|
2160
2182
|
min,
|
|
2161
2183
|
length,
|
|
@@ -2185,6 +2207,38 @@ const validations$2 = {
|
|
|
2185
2207
|
date,
|
|
2186
2208
|
betweenDates
|
|
2187
2209
|
};
|
|
2210
|
+
/**
|
|
2211
|
+
* @internal
|
|
2212
|
+
* Runs a set of validation handlers against a given value.
|
|
2213
|
+
*
|
|
2214
|
+
* @param {unknown} value - The value to be validated.
|
|
2215
|
+
* @param {TValidationMethods} handlers - An object containing validation methods to be applied.
|
|
2216
|
+
* @returns {boolean[]} - An array of boolean results for each validation method.
|
|
2217
|
+
*
|
|
2218
|
+
* @example
|
|
2219
|
+
* const handlers = {
|
|
2220
|
+
* max: { max: 10 },
|
|
2221
|
+
* required: true,
|
|
2222
|
+
* email: true
|
|
2223
|
+
* };
|
|
2224
|
+
* const results = run('test@example.com', handlers);
|
|
2225
|
+
* console.log(results); // [false, false, true] (value fails 'max', passes 'required', passes 'email')
|
|
2226
|
+
*/
|
|
2227
|
+
function run(value, handlers) {
|
|
2228
|
+
const runner = [];
|
|
2229
|
+
Object.keys(handlers).forEach(rule => {
|
|
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);
|
|
2239
|
+
});
|
|
2240
|
+
return runner;
|
|
2241
|
+
}
|
|
2188
2242
|
/**
|
|
2189
2243
|
* Validates that a value meets multiple validation rules.
|
|
2190
2244
|
*
|
|
@@ -2216,16 +2270,16 @@ const validations$2 = {
|
|
|
2216
2270
|
*/
|
|
2217
2271
|
const multipleValidations = (value, methods) => {
|
|
2218
2272
|
if (!methods.multipleValidations) return false;
|
|
2219
|
-
const runner
|
|
2273
|
+
const runner = run(value, methods.multipleValidations.validations);
|
|
2220
2274
|
const rulesMapper = {
|
|
2221
|
-
AND: () => runner
|
|
2222
|
-
OR: () => runner
|
|
2223
|
-
NOT: () => !runner
|
|
2275
|
+
AND: () => runner.every(validation => validation),
|
|
2276
|
+
OR: () => runner.some(validation => validation),
|
|
2277
|
+
NOT: () => !runner.every(validation => validation)
|
|
2224
2278
|
};
|
|
2225
2279
|
return rulesMapper[methods.multipleValidations.rule]();
|
|
2226
2280
|
};
|
|
2227
2281
|
|
|
2228
|
-
const validations
|
|
2282
|
+
const validations = {
|
|
2229
2283
|
max,
|
|
2230
2284
|
min,
|
|
2231
2285
|
length,
|
|
@@ -2273,69 +2327,6 @@ class SafeSubject extends Subject {
|
|
|
2273
2327
|
}
|
|
2274
2328
|
}
|
|
2275
2329
|
|
|
2276
|
-
/**
|
|
2277
|
-
* @internal
|
|
2278
|
-
* An object mapping validation keys to their respective validation functions.
|
|
2279
|
-
*
|
|
2280
|
-
* @type {Record<keyof TAvailableValidations, (value: unknown, validations: TAvailableValidations) => boolean>}
|
|
2281
|
-
*
|
|
2282
|
-
* @example
|
|
2283
|
-
* const isValid = validations.max(5, { max: 10 });
|
|
2284
|
-
* console.log(isValid); // false (5 is not greater than 10)
|
|
2285
|
-
*/
|
|
2286
|
-
const validations = {
|
|
2287
|
-
max,
|
|
2288
|
-
min,
|
|
2289
|
-
length,
|
|
2290
|
-
regex,
|
|
2291
|
-
url,
|
|
2292
|
-
email,
|
|
2293
|
-
onlyLetters,
|
|
2294
|
-
notAllowSpaces,
|
|
2295
|
-
callback,
|
|
2296
|
-
hasNoExtraSpaces,
|
|
2297
|
-
between,
|
|
2298
|
-
sequential,
|
|
2299
|
-
includes,
|
|
2300
|
-
repeated,
|
|
2301
|
-
document,
|
|
2302
|
-
isCreditCard,
|
|
2303
|
-
isCreditCodeMatch,
|
|
2304
|
-
isCreditCardAndLength,
|
|
2305
|
-
required,
|
|
2306
|
-
value,
|
|
2307
|
-
notEmpty,
|
|
2308
|
-
bool,
|
|
2309
|
-
exists,
|
|
2310
|
-
isNumber,
|
|
2311
|
-
conditions,
|
|
2312
|
-
validDate,
|
|
2313
|
-
date,
|
|
2314
|
-
betweenDates,
|
|
2315
|
-
multipleValidations
|
|
2316
|
-
};
|
|
2317
|
-
/**
|
|
2318
|
-
* Validates a given value based on specified validation methods inside a custom named validation.
|
|
2319
|
-
*
|
|
2320
|
-
* @param {unknown} value - The value to be validated.
|
|
2321
|
-
* @param {TValidationMethods} methods - The validation methods to be applied.
|
|
2322
|
-
* @returns {boolean} - Returns true if any of the validation methods pass, otherwise false.
|
|
2323
|
-
*
|
|
2324
|
-
* @example
|
|
2325
|
-
* const value = 'example@example.com';
|
|
2326
|
-
* const methods = {
|
|
2327
|
-
* required: true,
|
|
2328
|
-
* email: true
|
|
2329
|
-
* };
|
|
2330
|
-
*
|
|
2331
|
-
* const isValid = validateValue(value, methods);
|
|
2332
|
-
* console.log(isValid); // Output: true
|
|
2333
|
-
*/
|
|
2334
|
-
var namedRule = ((value, methods) => {
|
|
2335
|
-
if (!methods) return false;
|
|
2336
|
-
return runner(value, methods, validations).some(validation => validation);
|
|
2337
|
-
});
|
|
2338
|
-
|
|
2339
2330
|
/**
|
|
2340
2331
|
* @internal
|
|
2341
2332
|
* Handles the validation of a given value based on specified validation methods and rules.
|
|
@@ -2364,7 +2355,7 @@ function handleValidation(value, validations, methods, key) {
|
|
|
2364
2355
|
if (isFunction(methods[key])) {
|
|
2365
2356
|
return methods[key](value, validations);
|
|
2366
2357
|
}
|
|
2367
|
-
return namedRule(value, validations[key]);
|
|
2358
|
+
return namedRule(value, validations[key], methods);
|
|
2368
2359
|
}
|
|
2369
2360
|
|
|
2370
2361
|
/**
|
|
@@ -2757,7 +2748,7 @@ class FormField {
|
|
|
2757
2748
|
const errors = {};
|
|
2758
2749
|
const schemaValidations = (_a = this.validations) === null || _a === void 0 ? void 0 : _a.methods;
|
|
2759
2750
|
schemaValidations && Object.keys(schemaValidations).forEach(validationKey => {
|
|
2760
|
-
const error = handleValidation(this.value, schemaValidations, validations
|
|
2751
|
+
const error = handleValidation(this.value, schemaValidations, validations, validationKey);
|
|
2761
2752
|
// setting valid flag
|
|
2762
2753
|
valid = !error && valid;
|
|
2763
2754
|
// setting error messages
|
|
@@ -2817,7 +2808,7 @@ class FormField {
|
|
|
2817
2808
|
let valid = true;
|
|
2818
2809
|
const preConditions = config.preConditions;
|
|
2819
2810
|
preConditions && Object.keys(preConditions).forEach(validationKey => {
|
|
2820
|
-
const error = handleValidation(this.value, preConditions, validations
|
|
2811
|
+
const error = handleValidation(this.value, preConditions, validations, validationKey);
|
|
2821
2812
|
valid = valid && !error;
|
|
2822
2813
|
});
|
|
2823
2814
|
if (config.blockRequestWhenInvalid) {
|
|
@@ -3297,7 +3288,7 @@ class FormCore {
|
|
|
3297
3288
|
structVisibility.forEach(structElement => {
|
|
3298
3289
|
if (!structElement.events.includes(event)) return;
|
|
3299
3290
|
Object.keys(structElement.validations).forEach(validationKey => {
|
|
3300
|
-
const error = handleValidation(field.value, structElement.validations, validations
|
|
3291
|
+
const error = handleValidation(field.value, structElement.validations, validations, validationKey);
|
|
3301
3292
|
if (Array.isArray(structElement.fields)) {
|
|
3302
3293
|
structElement.fields.forEach(fieldKey => {
|
|
3303
3294
|
if (!this.fields.has(fieldKey)) console.warn(`failed to update visibility onto field ${fieldKey}`);else this.fields.get(fieldKey).visibility = !error;
|
|
@@ -3325,7 +3316,7 @@ class FormCore {
|
|
|
3325
3316
|
structResetValue.forEach(structElement => {
|
|
3326
3317
|
if (!structElement.events.includes(event)) return;
|
|
3327
3318
|
Object.keys(structElement.validations).forEach(validationKey => {
|
|
3328
|
-
const error = handleValidation(field.value, structElement.validations, validations
|
|
3319
|
+
const error = handleValidation(field.value, structElement.validations, validations, validationKey);
|
|
3329
3320
|
if (!error) {
|
|
3330
3321
|
if (Array.isArray(structElement.fields)) {
|
|
3331
3322
|
structElement.fields.forEach((fieldKey, index) => {
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { TSchemaValidation, TValidationMethods } from '
|
|
2
|
-
import { TValidationHandler } from '
|
|
1
|
+
import { TSchemaValidation, TValidationMethods } from '../types/schema';
|
|
2
|
+
import { TValidationHandler } from '../types/utility';
|
|
3
3
|
/**
|
|
4
4
|
* @internal
|
|
5
5
|
* Handles the validation of a given value based on specified validation methods and rules.
|
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.
|
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,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type TValidationHandler = Record<string, (value: unknown, validations: TValidationMethods) => boolean>;
|
|
1
|
+
import { TValidationHandler } from '../types/utility';
|
|
3
2
|
export declare const validations: TValidationHandler;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { TValidationMethods } from '../types/schema';
|
|
2
|
+
import { TValidationHandler } from '../types/utility';
|
|
2
3
|
/**
|
|
3
4
|
* Validates a given value based on specified validation methods inside a custom named validation.
|
|
4
5
|
*
|
|
5
6
|
* @param {unknown} value - The value to be validated.
|
|
6
7
|
* @param {TValidationMethods} methods - The validation methods to be applied.
|
|
8
|
+
* @param {TValidationHandler} validations - An object containing every validation methods to be executed.
|
|
7
9
|
* @returns {boolean} - Returns true if any of the validation methods pass, otherwise false.
|
|
8
10
|
*
|
|
9
11
|
* @example
|
|
@@ -16,5 +18,5 @@ import { TValidationMethods } from '../types/schema';
|
|
|
16
18
|
* const isValid = validateValue(value, methods);
|
|
17
19
|
* console.log(isValid); // Output: true
|
|
18
20
|
*/
|
|
19
|
-
declare const _default: (value: unknown, methods: TValidationMethods) => boolean;
|
|
21
|
+
declare const _default: (value: unknown, methods: TValidationMethods, validations: TValidationHandler) => boolean;
|
|
20
22
|
export default _default;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { TValidationMethods } from '../../types/schema';
|
|
2
|
-
import { TValidationHandler } from '../../validations/handler';
|
|
3
|
-
/**
|
|
4
|
-
* @internal
|
|
5
|
-
* Runs a set of validation handlers against a given value.
|
|
6
|
-
*
|
|
7
|
-
* @param {unknown} value - The value to be validated.
|
|
8
|
-
* @param {TValidationMethods} handlers - An object containing validation methods to be applied.
|
|
9
|
-
* @param {TValidationHandler} validations - An object containing every validation methods to be executed.
|
|
10
|
-
* @returns {boolean[]} - An array of boolean results for each validation method.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* const handlers = {
|
|
14
|
-
* max: { max: 10 },
|
|
15
|
-
* required: true,
|
|
16
|
-
* email: true
|
|
17
|
-
* };
|
|
18
|
-
* const results = run('test@example.com', handlers);
|
|
19
|
-
* console.log(results); // [false, false, true] (value fails 'max', passes 'required', passes 'email')
|
|
20
|
-
*/
|
|
21
|
-
export default function runner(value: unknown, handlers: TValidationMethods, validations: TValidationHandler): boolean[];
|