@stubber/form-fields 1.5.2 → 1.6.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/dist/fields2/Form.svelte +10 -4
- package/dist/fields2/Form.svelte.d.ts +1 -0
- package/dist/fields2/interfaces.d.ts +1 -3
- package/dist/fields2/utils.js +1 -0
- package/dist/fields2/validations/validate_field.d.ts +5 -2
- package/dist/fields2/validations/validate_field.js +14 -12
- package/package.json +1 -1
package/dist/fields2/Form.svelte
CHANGED
|
@@ -6,19 +6,25 @@ export let initial_form;
|
|
|
6
6
|
export let form;
|
|
7
7
|
export let attachments;
|
|
8
8
|
export let dependencies = void 0;
|
|
9
|
+
export let form_valid;
|
|
9
10
|
if (initial_form.data) {
|
|
10
11
|
form.update((data) => {
|
|
11
12
|
return { ...data, ...initial_form.data };
|
|
12
13
|
});
|
|
13
14
|
}
|
|
14
15
|
const fields = build_fields(form, attachments, dependencies, initial_form.spec.fields);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
16
|
+
let form_errors = {};
|
|
17
|
+
$: validate_form($form);
|
|
18
18
|
const validate_form = async (_) => {
|
|
19
|
+
form_errors = {};
|
|
19
20
|
for (const fieldStore of fields) {
|
|
20
|
-
await validate_field(form, fieldStore);
|
|
21
|
+
const { key, errors } = await validate_field(form, fieldStore);
|
|
22
|
+
if (errors.length > 0) {
|
|
23
|
+
form_errors[key] = errors;
|
|
24
|
+
}
|
|
21
25
|
}
|
|
26
|
+
form_errors = { ...form_errors };
|
|
27
|
+
form_valid = Object.keys(form_errors).length === 0;
|
|
22
28
|
};
|
|
23
29
|
</script>
|
|
24
30
|
|
|
@@ -154,6 +154,7 @@ export interface IInitialForm {
|
|
|
154
154
|
export type IFieldType = keyof typeof fields;
|
|
155
155
|
export interface IBuiltField<T = {}> {
|
|
156
156
|
id: string;
|
|
157
|
+
key: string;
|
|
157
158
|
formStore: Writable<any>;
|
|
158
159
|
attachmentsStore: Writable<UploadedFile[]>;
|
|
159
160
|
formDependencies?: IFormDependencies;
|
|
@@ -202,6 +203,3 @@ export interface IValidationResult {
|
|
|
202
203
|
type: "error" | "info";
|
|
203
204
|
message: string;
|
|
204
205
|
}
|
|
205
|
-
export type ValidationTree = {
|
|
206
|
-
[key: string]: ValidationTree | IValidationResult[] | undefined;
|
|
207
|
-
};
|
package/dist/fields2/utils.js
CHANGED
|
@@ -10,6 +10,7 @@ export const build_field = (formStore, attachmentsStore, formDependencies, field
|
|
|
10
10
|
let final_path = generate_field_data_path(field_key, field, base_path);
|
|
11
11
|
const built_field = {
|
|
12
12
|
id: `${field_key}-${final_path}`,
|
|
13
|
+
key: field_key,
|
|
13
14
|
formStore,
|
|
14
15
|
attachmentsStore,
|
|
15
16
|
formDependencies,
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import type { IJsonataValidation, IRequiredValidation, IRexegValidation, IValidationResult, IBuiltField } from "../interfaces";
|
|
2
1
|
import type { Writable } from "svelte/store";
|
|
3
|
-
|
|
2
|
+
import type { IBuiltField, IJsonataValidation, IRequiredValidation, IRexegValidation, IValidationResult } from "../interfaces";
|
|
3
|
+
export declare const validate_field: (formStore: Writable<any>, fieldStore: Writable<IBuiltField>) => Promise<{
|
|
4
|
+
key: string;
|
|
5
|
+
errors: IValidationResult[];
|
|
6
|
+
}>;
|
|
4
7
|
export declare const required_validation: (results: IValidationResult[], value: any, validation: IRequiredValidation) => void;
|
|
5
8
|
export declare const regex_validation: (results: IValidationResult[], value: any, validation: IRexegValidation) => void;
|
|
6
9
|
export declare const jsonata_validation: (results: IValidationResult[], _value: any, validation: IJsonataValidation, context?: any) => Promise<void>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import jsonata from "jsonata";
|
|
2
|
-
import {
|
|
2
|
+
import { unset } from "lodash-es";
|
|
3
3
|
import { get as getStoreValue } from "svelte/store";
|
|
4
4
|
export const validate_field = async (formStore, fieldStore) => {
|
|
5
5
|
const results = [];
|
|
@@ -20,7 +20,7 @@ export const validate_field = async (formStore, fieldStore) => {
|
|
|
20
20
|
f.hidden = true;
|
|
21
21
|
return f;
|
|
22
22
|
});
|
|
23
|
-
return; // Skip validation if conditions are not met
|
|
23
|
+
return { key: field.key, errors: [] }; // Skip validation if conditions are not met
|
|
24
24
|
}
|
|
25
25
|
else {
|
|
26
26
|
fieldStore.update((f) => {
|
|
@@ -31,10 +31,11 @@ export const validate_field = async (formStore, fieldStore) => {
|
|
|
31
31
|
}
|
|
32
32
|
// validate sub_fields if they exist (arraybuilder and section fields)
|
|
33
33
|
if (field.sub_fields && field.sub_fields.length > 0) {
|
|
34
|
-
// console.log("sub_fields", field.sub_fields);
|
|
35
34
|
for (const sub_field of field.sub_fields || []) {
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
const { key, errors } = await validate_field(formStore, sub_field);
|
|
36
|
+
if (errors.length > 0) {
|
|
37
|
+
results.push({ message: `${field.fieldtype} has errors in child fields`, type: "error" });
|
|
38
|
+
}
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
const validations = field.validations || [];
|
|
@@ -56,14 +57,15 @@ export const validate_field = async (formStore, fieldStore) => {
|
|
|
56
57
|
unset(f, "errors");
|
|
57
58
|
return f;
|
|
58
59
|
});
|
|
59
|
-
return;
|
|
60
60
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
else {
|
|
62
|
+
// fieldStore.errors = results;
|
|
63
|
+
fieldStore.update((f) => {
|
|
64
|
+
f.errors = results;
|
|
65
|
+
return f;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return { key: field.key, errors: results };
|
|
67
69
|
};
|
|
68
70
|
export const required_validation = (results, value, validation) => {
|
|
69
71
|
if (!value || value.length === 0 || (typeof value === "string" && value.trim() === "")) {
|