mobx-formkit 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/AGENTS.md +761 -0
- package/LICENSE +21 -0
- package/README.md +214 -0
- package/lib/ArrayMap.d.ts +50 -0
- package/lib/ArrayMap.d.ts.map +1 -0
- package/lib/ArrayMap.js +151 -0
- package/lib/ArrayMap.js.map +1 -0
- package/lib/Base.d.ts +237 -0
- package/lib/Base.d.ts.map +1 -0
- package/lib/Base.js +872 -0
- package/lib/Base.js.map +1 -0
- package/lib/Bindings.d.ts +28 -0
- package/lib/Bindings.d.ts.map +1 -0
- package/lib/Bindings.js +73 -0
- package/lib/Bindings.js.map +1 -0
- package/lib/Field.d.ts +195 -0
- package/lib/Field.d.ts.map +1 -0
- package/lib/Field.js +734 -0
- package/lib/Field.js.map +1 -0
- package/lib/Form.d.ts +75 -0
- package/lib/Form.d.ts.map +1 -0
- package/lib/Form.js +199 -0
- package/lib/Form.js.map +1 -0
- package/lib/Options.d.ts +9 -0
- package/lib/Options.d.ts.map +1 -0
- package/lib/Options.js +85 -0
- package/lib/Options.js.map +1 -0
- package/lib/State.d.ts +43 -0
- package/lib/State.d.ts.map +1 -0
- package/lib/State.js +147 -0
- package/lib/State.js.map +1 -0
- package/lib/Validator.d.ts +25 -0
- package/lib/Validator.d.ts.map +1 -0
- package/lib/Validator.js +143 -0
- package/lib/Validator.js.map +1 -0
- package/lib/compat.d.ts +29 -0
- package/lib/compat.d.ts.map +1 -0
- package/lib/compat.js +117 -0
- package/lib/compat.js.map +1 -0
- package/lib/composer.d.ts +26 -0
- package/lib/composer.d.ts.map +1 -0
- package/lib/composer.js +65 -0
- package/lib/composer.js.map +1 -0
- package/lib/esm/ArrayMap.js +146 -0
- package/lib/esm/ArrayMap.js.map +1 -0
- package/lib/esm/Base.js +868 -0
- package/lib/esm/Base.js.map +1 -0
- package/lib/esm/Bindings.js +69 -0
- package/lib/esm/Bindings.js.map +1 -0
- package/lib/esm/Field.js +730 -0
- package/lib/esm/Field.js.map +1 -0
- package/lib/esm/Form.js +195 -0
- package/lib/esm/Form.js.map +1 -0
- package/lib/esm/Options.js +81 -0
- package/lib/esm/Options.js.map +1 -0
- package/lib/esm/State.js +143 -0
- package/lib/esm/State.js.map +1 -0
- package/lib/esm/Validator.js +139 -0
- package/lib/esm/Validator.js.map +1 -0
- package/lib/esm/compat.js +96 -0
- package/lib/esm/compat.js.map +1 -0
- package/lib/esm/composer.js +63 -0
- package/lib/esm/composer.js.map +1 -0
- package/lib/esm/index.js +8 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/models/FieldProps.js +116 -0
- package/lib/esm/models/FieldProps.js.map +1 -0
- package/lib/esm/models/OptionsModel.js +51 -0
- package/lib/esm/models/OptionsModel.js.map +1 -0
- package/lib/esm/models/StateInterface.js +9 -0
- package/lib/esm/models/StateInterface.js.map +1 -0
- package/lib/esm/models/ValidatorInterface.js +8 -0
- package/lib/esm/models/ValidatorInterface.js.map +1 -0
- package/lib/esm/parser.js +235 -0
- package/lib/esm/parser.js.map +1 -0
- package/lib/esm/props.js +118 -0
- package/lib/esm/props.js.map +1 -0
- package/lib/esm/utils.js +156 -0
- package/lib/esm/utils.js.map +1 -0
- package/lib/esm/validators/AJV.js +105 -0
- package/lib/esm/validators/AJV.js.map +1 -0
- package/lib/esm/validators/DVR.js +104 -0
- package/lib/esm/validators/DVR.js.map +1 -0
- package/lib/esm/validators/JOI.js +54 -0
- package/lib/esm/validators/JOI.js.map +1 -0
- package/lib/esm/validators/VALIBOT.js +50 -0
- package/lib/esm/validators/VALIBOT.js.map +1 -0
- package/lib/esm/validators/VINEJS.js +60 -0
- package/lib/esm/validators/VINEJS.js.map +1 -0
- package/lib/esm/validators/VJF.js +88 -0
- package/lib/esm/validators/VJF.js.map +1 -0
- package/lib/esm/validators/YUP.js +69 -0
- package/lib/esm/validators/YUP.js.map +1 -0
- package/lib/esm/validators/ZOD.js +43 -0
- package/lib/esm/validators/ZOD.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +15 -0
- package/lib/index.js.map +1 -0
- package/lib/models/BaseInterface.d.ts +40 -0
- package/lib/models/BaseInterface.d.ts.map +1 -0
- package/lib/models/BindingsInterface.d.ts +18 -0
- package/lib/models/BindingsInterface.d.ts.map +1 -0
- package/lib/models/FieldInterface.d.ts +88 -0
- package/lib/models/FieldInterface.d.ts.map +1 -0
- package/lib/models/FieldProps.d.ts +111 -0
- package/lib/models/FieldProps.d.ts.map +1 -0
- package/lib/models/FieldProps.js +116 -0
- package/lib/models/FieldProps.js.map +1 -0
- package/lib/models/FormInterface.d.ts +184 -0
- package/lib/models/FormInterface.d.ts.map +1 -0
- package/lib/models/OptionsInterface.d.ts +8 -0
- package/lib/models/OptionsInterface.d.ts.map +1 -0
- package/lib/models/OptionsModel.d.ts +99 -0
- package/lib/models/OptionsModel.d.ts.map +1 -0
- package/lib/models/OptionsModel.js +51 -0
- package/lib/models/OptionsModel.js.map +1 -0
- package/lib/models/SharedActionsInterface.d.ts +27 -0
- package/lib/models/SharedActionsInterface.d.ts.map +1 -0
- package/lib/models/SharedEventsInterface.d.ts +8 -0
- package/lib/models/SharedEventsInterface.d.ts.map +1 -0
- package/lib/models/SharedHelpersInterface.d.ts +26 -0
- package/lib/models/SharedHelpersInterface.d.ts.map +1 -0
- package/lib/models/SharedInitializerInterface.d.ts +6 -0
- package/lib/models/SharedInitializerInterface.d.ts.map +1 -0
- package/lib/models/SharedUtilsInterface.d.ts +10 -0
- package/lib/models/SharedUtilsInterface.d.ts.map +1 -0
- package/lib/models/StateInterface.d.ts +37 -0
- package/lib/models/StateInterface.d.ts.map +1 -0
- package/lib/models/StateInterface.js +9 -0
- package/lib/models/StateInterface.js.map +1 -0
- package/lib/models/ValidatorInterface.d.ts +74 -0
- package/lib/models/ValidatorInterface.d.ts.map +1 -0
- package/lib/models/ValidatorInterface.js +8 -0
- package/lib/models/ValidatorInterface.js.map +1 -0
- package/lib/parser.d.ts +13 -0
- package/lib/parser.d.ts.map +1 -0
- package/lib/parser.js +247 -0
- package/lib/parser.js.map +1 -0
- package/lib/props.d.ts +14 -0
- package/lib/props.d.ts.map +1 -0
- package/lib/props.js +120 -0
- package/lib/props.js.map +1 -0
- package/lib/utils.d.ts +34 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +182 -0
- package/lib/utils.js.map +1 -0
- package/lib/validators/AJV.d.ts +4 -0
- package/lib/validators/AJV.d.ts.map +1 -0
- package/lib/validators/AJV.js +109 -0
- package/lib/validators/AJV.js.map +1 -0
- package/lib/validators/DVR.d.ts +4 -0
- package/lib/validators/DVR.d.ts.map +1 -0
- package/lib/validators/DVR.js +108 -0
- package/lib/validators/DVR.js.map +1 -0
- package/lib/validators/JOI.d.ts +4 -0
- package/lib/validators/JOI.d.ts.map +1 -0
- package/lib/validators/JOI.js +58 -0
- package/lib/validators/JOI.js.map +1 -0
- package/lib/validators/VALIBOT.d.ts +4 -0
- package/lib/validators/VALIBOT.d.ts.map +1 -0
- package/lib/validators/VALIBOT.js +73 -0
- package/lib/validators/VALIBOT.js.map +1 -0
- package/lib/validators/VINEJS.d.ts +4 -0
- package/lib/validators/VINEJS.d.ts.map +1 -0
- package/lib/validators/VINEJS.js +64 -0
- package/lib/validators/VINEJS.js.map +1 -0
- package/lib/validators/VJF.d.ts +4 -0
- package/lib/validators/VJF.d.ts.map +1 -0
- package/lib/validators/VJF.js +92 -0
- package/lib/validators/VJF.js.map +1 -0
- package/lib/validators/YUP.d.ts +4 -0
- package/lib/validators/YUP.d.ts.map +1 -0
- package/lib/validators/YUP.js +73 -0
- package/lib/validators/YUP.js.map +1 -0
- package/lib/validators/ZOD.d.ts +4 -0
- package/lib/validators/ZOD.d.ts.map +1 -0
- package/lib/validators/ZOD.js +47 -0
- package/lib/validators/ZOD.js.map +1 -0
- package/package.json +183 -0
- package/src/ArrayMap.ts +165 -0
- package/src/Base.ts +1229 -0
- package/src/Bindings.ts +85 -0
- package/src/Field.ts +1034 -0
- package/src/Form.ts +264 -0
- package/src/Options.ts +91 -0
- package/src/State.ts +183 -0
- package/src/Validator.ts +177 -0
- package/src/compat.ts +104 -0
- package/src/composer.ts +82 -0
- package/src/index.ts +7 -0
- package/src/models/BaseInterface.ts +45 -0
- package/src/models/BindingsInterface.ts +25 -0
- package/src/models/FieldInterface.ts +97 -0
- package/src/models/FieldProps.ts +116 -0
- package/src/models/FormInterface.ts +203 -0
- package/src/models/OptionsInterface.ts +9 -0
- package/src/models/OptionsModel.ts +100 -0
- package/src/models/SharedActionsInterface.ts +30 -0
- package/src/models/SharedEventsInterface.ts +8 -0
- package/src/models/SharedHelpersInterface.ts +12 -0
- package/src/models/SharedInitializerInterface.ts +6 -0
- package/src/models/SharedUtilsInterface.ts +10 -0
- package/src/models/StateInterface.ts +44 -0
- package/src/models/ValidatorInterface.ts +88 -0
- package/src/parser.ts +327 -0
- package/src/props.ts +127 -0
- package/src/utils.ts +218 -0
- package/src/validators/AJV.ts +140 -0
- package/src/validators/DVR.ts +145 -0
- package/src/validators/JOI.ts +79 -0
- package/src/validators/VALIBOT.ts +70 -0
- package/src/validators/VINEJS.ts +83 -0
- package/src/validators/VJF.ts +123 -0
- package/src/validators/YUP.ts +90 -0
- package/src/validators/ZOD.ts +66 -0
- package/umd/MobxFormkit.umd.js +3093 -0
- package/umd/MobxFormkit.umd.js.map +1 -0
- package/umd/MobxFormkit.umd.min.js +2 -0
- package/umd/MobxFormkit.umd.min.js.map +1 -0
- package/umd/MobxFormkitComposer.umd.js +183 -0
- package/umd/MobxFormkitComposer.umd.js.map +1 -0
- package/umd/MobxFormkitComposer.umd.min.js +2 -0
- package/umd/MobxFormkitComposer.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorAJV.umd.js +113 -0
- package/umd/MobxFormkitValidatorAJV.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorAJV.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorAJV.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorDVR.umd.js +110 -0
- package/umd/MobxFormkitValidatorDVR.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorDVR.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorDVR.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorJOI.umd.js +62 -0
- package/umd/MobxFormkitValidatorJOI.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorJOI.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorJOI.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorVALIBOT.umd.js +8032 -0
- package/umd/MobxFormkitValidatorVALIBOT.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorVALIBOT.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorVALIBOT.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorVINEJS.umd.js +11737 -0
- package/umd/MobxFormkitValidatorVINEJS.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorVINEJS.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorVINEJS.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorVJF.umd.js +96 -0
- package/umd/MobxFormkitValidatorVJF.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorVJF.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorVJF.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorYUP.umd.js +77 -0
- package/umd/MobxFormkitValidatorYUP.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorYUP.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorYUP.umd.min.js.map +1 -0
- package/umd/MobxFormkitValidatorZOD.umd.js +49 -0
- package/umd/MobxFormkitValidatorZOD.umd.js.map +1 -0
- package/umd/MobxFormkitValidatorZOD.umd.min.js +2 -0
- package/umd/MobxFormkitValidatorZOD.umd.min.js.map +1 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
import FieldInterface from "../models/FieldInterface";
|
|
3
|
+
import FormInterface from "../models/FormInterface";
|
|
4
|
+
import StateInterface from "../models/StateInterface";
|
|
5
|
+
import {
|
|
6
|
+
ValidationPlugin,
|
|
7
|
+
ValidationPluginConfig,
|
|
8
|
+
ValidationPluginConstructor,
|
|
9
|
+
ValidationPluginInterface,
|
|
10
|
+
} from "../models/ValidatorInterface";
|
|
11
|
+
|
|
12
|
+
class VALIBOT<TValidator = any> implements ValidationPluginInterface {
|
|
13
|
+
promises: Promise<any>[];
|
|
14
|
+
config: ValidationPluginConfig<TValidator>;
|
|
15
|
+
state: StateInterface | null;
|
|
16
|
+
extend?: (args: { validator: TValidator; form: FormInterface }) => void;
|
|
17
|
+
validator: any;
|
|
18
|
+
schema: any;
|
|
19
|
+
|
|
20
|
+
constructor({
|
|
21
|
+
config,
|
|
22
|
+
state = null,
|
|
23
|
+
promises = [],
|
|
24
|
+
}: ValidationPluginConstructor<TValidator>) {
|
|
25
|
+
this.state = state;
|
|
26
|
+
this.promises = promises;
|
|
27
|
+
this.config = config;
|
|
28
|
+
this.extend = config?.extend;
|
|
29
|
+
this.validator = config.package ?? v;
|
|
30
|
+
this.schema = config.schema;
|
|
31
|
+
|
|
32
|
+
this.extendValidator();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
extendValidator(): void {
|
|
36
|
+
if (typeof this.extend === "function") {
|
|
37
|
+
this.extend({
|
|
38
|
+
validator: this.validator,
|
|
39
|
+
form: this.state!.form,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Convert a Valibot issue.path (array of { key }) into an MRF-style string path
|
|
45
|
+
// e.g. [{ key: 'members' }, { key: 0 }, { key: 'name' }] => 'members.0.name'
|
|
46
|
+
normalizePath(issuePath: any[] = []): string {
|
|
47
|
+
return issuePath.map((seg) => seg?.key).join(".");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
validate(field: FieldInterface): void {
|
|
51
|
+
const result = v.safeParse(this.schema, field.state.form.flatMapValues);
|
|
52
|
+
|
|
53
|
+
if (result.success) return;
|
|
54
|
+
|
|
55
|
+
const messages = result.issues
|
|
56
|
+
.filter((issue: any) => this.normalizePath(issue.path) === field.path)
|
|
57
|
+
.map((issue: any) => issue.message);
|
|
58
|
+
|
|
59
|
+
if (messages?.length) {
|
|
60
|
+
field.validationErrorStack = messages;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default <TValidator = any>(
|
|
66
|
+
config?: ValidationPluginConfig<TValidator>
|
|
67
|
+
): ValidationPlugin<TValidator> => ({
|
|
68
|
+
class: VALIBOT<TValidator>,
|
|
69
|
+
config,
|
|
70
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import vine from "@vinejs/vine";
|
|
2
|
+
import FieldInterface from "../models/FieldInterface";
|
|
3
|
+
import FormInterface from "../models/FormInterface";
|
|
4
|
+
import StateInterface from "../models/StateInterface";
|
|
5
|
+
import {
|
|
6
|
+
ValidationPlugin,
|
|
7
|
+
ValidationPluginConfig,
|
|
8
|
+
ValidationPluginConstructor,
|
|
9
|
+
ValidationPluginInterface,
|
|
10
|
+
} from "../models/ValidatorInterface";
|
|
11
|
+
|
|
12
|
+
class VINEJS<TValidator = any> implements ValidationPluginInterface {
|
|
13
|
+
promises: Promise<any>[] = [];
|
|
14
|
+
config: ValidationPluginConfig<TValidator>;
|
|
15
|
+
state: StateInterface | null;
|
|
16
|
+
extend?: (args: { validator: TValidator; form: FormInterface }) => void;
|
|
17
|
+
validator: any;
|
|
18
|
+
schema: any;
|
|
19
|
+
compiled: any;
|
|
20
|
+
|
|
21
|
+
constructor({
|
|
22
|
+
config,
|
|
23
|
+
state = null,
|
|
24
|
+
promises = [],
|
|
25
|
+
}: ValidationPluginConstructor<TValidator>) {
|
|
26
|
+
this.state = state;
|
|
27
|
+
this.promises = promises;
|
|
28
|
+
this.config = config;
|
|
29
|
+
this.extend = config?.extend;
|
|
30
|
+
this.validator = config.package ?? vine;
|
|
31
|
+
this.schema = config.schema;
|
|
32
|
+
this.compiled = this.validator.compile(this.schema);
|
|
33
|
+
|
|
34
|
+
this.extendValidator();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
extendValidator(): void {
|
|
38
|
+
if (typeof this.extend === "function") {
|
|
39
|
+
this.extend({
|
|
40
|
+
validator: this.validator,
|
|
41
|
+
form: this.state!.form,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// VineJS emits dot-notation paths (e.g. 'members.0.name') which match
|
|
47
|
+
// the MRF field.path convention.
|
|
48
|
+
validate(field: FieldInterface): void {
|
|
49
|
+
const validationPromise = this.compiled
|
|
50
|
+
.validate(field.state.form.flatMapValues)
|
|
51
|
+
.then(() => field.setValidationAsyncData(true))
|
|
52
|
+
.catch((error: any) => this.handleAsyncFail(field, error))
|
|
53
|
+
.then(() => this.executeAsyncValidation(field));
|
|
54
|
+
|
|
55
|
+
this.promises.push(validationPromise);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
handleAsyncFail(field: FieldInterface, error: any): void {
|
|
59
|
+
const messages = error?.messages ?? [];
|
|
60
|
+
if (!messages.length) return;
|
|
61
|
+
|
|
62
|
+
const fieldError = messages.find(
|
|
63
|
+
(item: any) => item.field === field.path
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
if (fieldError) {
|
|
67
|
+
field.setValidationAsyncData(false, fieldError.message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
executeAsyncValidation(field: FieldInterface): void {
|
|
72
|
+
if (field.validationAsyncData.valid === false) {
|
|
73
|
+
field.invalidate(field.validationAsyncData.message ?? undefined, false, true);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export default <TValidator = any>(
|
|
79
|
+
config?: ValidationPluginConfig<TValidator>
|
|
80
|
+
): ValidationPlugin<TValidator> => ({
|
|
81
|
+
class: VINEJS<TValidator>,
|
|
82
|
+
config,
|
|
83
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
|
|
2
|
+
import FieldInterface from "../models/FieldInterface";
|
|
3
|
+
import FormInterface from "../models/FormInterface";
|
|
4
|
+
import StateInterface from "../models/StateInterface";
|
|
5
|
+
import {
|
|
6
|
+
ValidationPlugin,
|
|
7
|
+
ValidationPluginConfig,
|
|
8
|
+
ValidationPluginConstructor,
|
|
9
|
+
ValidationPluginInterface,
|
|
10
|
+
} from "../models/ValidatorInterface";
|
|
11
|
+
|
|
12
|
+
function isPromise(obj: any): obj is Promise<any> {
|
|
13
|
+
return (
|
|
14
|
+
!!obj &&
|
|
15
|
+
typeof obj.then === "function" &&
|
|
16
|
+
(typeof obj === "object" || typeof obj === "function")
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class VJF<TValidator = any> implements ValidationPluginInterface<TValidator> {
|
|
21
|
+
promises: Promise<any>[];
|
|
22
|
+
config: ValidationPluginConfig<TValidator>;
|
|
23
|
+
state: StateInterface | null;
|
|
24
|
+
extend?: (args: { validator: TValidator; form: FormInterface }) => void;
|
|
25
|
+
validator: TValidator;
|
|
26
|
+
|
|
27
|
+
constructor({
|
|
28
|
+
config,
|
|
29
|
+
state = null,
|
|
30
|
+
promises = [],
|
|
31
|
+
}: ValidationPluginConstructor<TValidator>) {
|
|
32
|
+
this.state = state;
|
|
33
|
+
this.promises = promises;
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.extend = config?.extend;
|
|
36
|
+
this.validator = config?.package!;
|
|
37
|
+
this.extendValidator();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
extendValidator(): void {
|
|
41
|
+
if (typeof this.extend === "function") {
|
|
42
|
+
this.extend({
|
|
43
|
+
validator: this.validator,
|
|
44
|
+
form: this.state!.form,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
validate(field: FieldInterface): void {
|
|
50
|
+
if (!field.validators) return;
|
|
51
|
+
|
|
52
|
+
const validators = Array.isArray(field.validators) ? field.validators : [field.validators];
|
|
53
|
+
|
|
54
|
+
validators.forEach((fn) => this.collectData(fn, field));
|
|
55
|
+
|
|
56
|
+
this.executeValidation(field);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
collectData(fn: (args: { validator: TValidator; form: FormInterface; field: FieldInterface }) => [boolean, string] | Promise<[boolean, string]>, field: FieldInterface): void {
|
|
60
|
+
const result = this.handleFunctionResult(fn, field);
|
|
61
|
+
|
|
62
|
+
if (isPromise(result)) {
|
|
63
|
+
const $p = result
|
|
64
|
+
.then(([valid, message]) => field.setValidationAsyncData(valid, message))
|
|
65
|
+
.then(() => this.executeAsyncValidation(field));
|
|
66
|
+
|
|
67
|
+
this.promises.push($p);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
field.validationFunctionsData.unshift({
|
|
72
|
+
valid: result[0],
|
|
73
|
+
message: result[1],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
executeValidation(field: FieldInterface): void {
|
|
78
|
+
field.validationFunctionsData.forEach(({ valid, message }) => {
|
|
79
|
+
if (valid === false) {
|
|
80
|
+
field.invalidate(message, false);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
executeAsyncValidation(field: FieldInterface): void {
|
|
86
|
+
if (field.validationAsyncData.valid === false) {
|
|
87
|
+
field.invalidate(field.validationAsyncData.message ?? undefined, false, true);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
handleFunctionResult(fn: (args: { validator: TValidator; form: FormInterface; field: FieldInterface }) => [boolean, string] | Promise<[boolean, string]>, field: FieldInterface): [boolean, string] | Promise<[boolean, string]> {
|
|
92
|
+
const result = fn({
|
|
93
|
+
validator: this.validator,
|
|
94
|
+
form: this.state!.form,
|
|
95
|
+
field,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
if (Array.isArray(result)) {
|
|
99
|
+
return [result[0] || false, result[1] || "Error"];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (typeof result === 'boolean') {
|
|
103
|
+
return [result, "Error"];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (typeof result === 'string') {
|
|
107
|
+
return [false, result];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (isPromise(result)) {
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return [false, "Error"];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export default <TValidator = any>(
|
|
119
|
+
config?: ValidationPluginConfig<TValidator>
|
|
120
|
+
): ValidationPlugin<TValidator> => ({
|
|
121
|
+
class: VJF<TValidator>,
|
|
122
|
+
config,
|
|
123
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ValidationPlugin,
|
|
3
|
+
ValidationPluginConfig,
|
|
4
|
+
ValidationPluginConstructor,
|
|
5
|
+
ValidationPluginInterface,
|
|
6
|
+
} from "../models/ValidatorInterface";
|
|
7
|
+
import FormInterface from "../models/FormInterface";
|
|
8
|
+
import StateInterface from "../models/StateInterface";
|
|
9
|
+
|
|
10
|
+
class YUP<TValidator = any> implements ValidationPluginInterface {
|
|
11
|
+
promises: Promise<any>[] = [];
|
|
12
|
+
config: ValidationPluginConfig<TValidator>;
|
|
13
|
+
state: StateInterface | null;
|
|
14
|
+
extend?: (args: { validator: TValidator; form: FormInterface }) => void;
|
|
15
|
+
validator: TValidator;
|
|
16
|
+
schema: any;
|
|
17
|
+
|
|
18
|
+
constructor({
|
|
19
|
+
config,
|
|
20
|
+
state = null,
|
|
21
|
+
promises = [],
|
|
22
|
+
}: ValidationPluginConstructor) {
|
|
23
|
+
this.state = state;
|
|
24
|
+
this.promises = promises;
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.extend = config?.extend;
|
|
27
|
+
this.validator = config.package;
|
|
28
|
+
this.schema = config.schema(this.validator);
|
|
29
|
+
this.extendValidator();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Metodo per estendere il validatore
|
|
33
|
+
extendValidator(): void {
|
|
34
|
+
if (typeof this.extend === 'function') {
|
|
35
|
+
this.extend({
|
|
36
|
+
validator: this.validator,
|
|
37
|
+
form: this.state!.form,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Metodo di validazione principale
|
|
43
|
+
validate(field: any): void {
|
|
44
|
+
const fieldValidationPromise = this.createValidationPromise(field);
|
|
45
|
+
this.promises.push(fieldValidationPromise);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Creazione della promise per la validazione
|
|
49
|
+
private createValidationPromise(field: any): Promise<any> {
|
|
50
|
+
return new Promise((resolve) => {
|
|
51
|
+
this.schema
|
|
52
|
+
.validateAt(field.path, this.state!.form.values(), { strict: true })
|
|
53
|
+
.then(() => this.handleAsyncPasses(field, resolve))
|
|
54
|
+
.catch((error: any) => this.handleAsyncFails(field, resolve, error));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// Gestione dei successi della validazione asincrona
|
|
58
|
+
private handleAsyncPasses(field: any, resolve: (value?: undefined) => void): void {
|
|
59
|
+
field.setValidationAsyncData(true);
|
|
60
|
+
resolve();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Gestione dei fallimenti della validazione asincrona
|
|
64
|
+
private handleAsyncFails(field: any, resolve: (value?: undefined) => void, error: any): void {
|
|
65
|
+
// Yup a volte restituisce errori senza path (es. array vuoti)
|
|
66
|
+
const isSameField = error.path === field.path || error.path === undefined;
|
|
67
|
+
|
|
68
|
+
if (isSameField) {
|
|
69
|
+
const message = error.message?.replace(error.path ?? field.path, field.label);
|
|
70
|
+
field.setValidationAsyncData(false, message);
|
|
71
|
+
this.executeAsyncValidation(field);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
resolve(undefined);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Esecuzione della validazione asincrona
|
|
78
|
+
private executeAsyncValidation(field: any): void {
|
|
79
|
+
if (field.validationAsyncData.valid === false) {
|
|
80
|
+
field.invalidate(field.validationAsyncData.message, false, true);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default <TValidator = any>(
|
|
86
|
+
config?: ValidationPluginConfig<TValidator>
|
|
87
|
+
): ValidationPlugin<TValidator> => ({
|
|
88
|
+
class: YUP<TValidator>,
|
|
89
|
+
config,
|
|
90
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { get } from "lodash";
|
|
2
|
+
import FieldInterface from "../models/FieldInterface";
|
|
3
|
+
import FormInterface from "../models/FormInterface";
|
|
4
|
+
import StateInterface from "../models/StateInterface";
|
|
5
|
+
import { ZodSchema } from "zod";
|
|
6
|
+
import {
|
|
7
|
+
ValidationPlugin,
|
|
8
|
+
ValidationPluginConfig,
|
|
9
|
+
ValidationPluginConstructor,
|
|
10
|
+
ValidationPluginInterface,
|
|
11
|
+
} from "../models/ValidatorInterface";
|
|
12
|
+
|
|
13
|
+
class ZOD<TValidator = any> implements ValidationPluginInterface {
|
|
14
|
+
promises: Promise<any>[];
|
|
15
|
+
config: ValidationPluginConfig;
|
|
16
|
+
state: StateInterface | null;
|
|
17
|
+
extend?: (args: { validator: TValidator; form: FormInterface }) => void;
|
|
18
|
+
validator: any;
|
|
19
|
+
schema: ZodSchema<any>;
|
|
20
|
+
|
|
21
|
+
constructor({
|
|
22
|
+
config,
|
|
23
|
+
state = null,
|
|
24
|
+
promises = [],
|
|
25
|
+
}: ValidationPluginConstructor) {
|
|
26
|
+
this.state = state;
|
|
27
|
+
this.promises = promises;
|
|
28
|
+
this.config = config;
|
|
29
|
+
this.extend = config?.extend;
|
|
30
|
+
this.validator = config.package;
|
|
31
|
+
this.schema = config.schema as ZodSchema<any>;
|
|
32
|
+
|
|
33
|
+
this.extendValidator();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
extendValidator(): void {
|
|
37
|
+
if (typeof this.extend === "function") {
|
|
38
|
+
this.extend({
|
|
39
|
+
validator: this.validator,
|
|
40
|
+
form: this.state!.form,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
validate(field: FieldInterface): void {
|
|
46
|
+
const result = this.schema.safeParse(field.state.form.flatMapValues);
|
|
47
|
+
|
|
48
|
+
if (result.success) return;
|
|
49
|
+
|
|
50
|
+
const fieldErrors = get(
|
|
51
|
+
(result as any).error.format(),
|
|
52
|
+
field.path ?? ""
|
|
53
|
+
)?._errors;
|
|
54
|
+
|
|
55
|
+
if (fieldErrors?.length) {
|
|
56
|
+
field.validationErrorStack = fieldErrors;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default <TValidator = any>(
|
|
62
|
+
config?: ValidationPluginConfig<TValidator>
|
|
63
|
+
): ValidationPlugin<TValidator> => ({
|
|
64
|
+
class: ZOD<TValidator>,
|
|
65
|
+
config,
|
|
66
|
+
});
|