@bach.software/vue-dynamic-form 0.1.3 → 0.2.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/README.md +203 -7
- package/dist/examples.cjs.js +12 -0
- package/dist/examples.cjs.js.map +1 -0
- package/dist/examples.es.js +552 -0
- package/dist/examples.es.js.map +1 -0
- package/dist/src/components/DynamicForm.vue.d.ts +2 -0
- package/dist/src/components/DynamicFormItem.analytics.test.d.ts +1 -0
- package/dist/src/components/DynamicFormItem.logic.test.d.ts +1 -0
- package/dist/src/components/DynamicFormItem.test-helpers.d.ts +21 -0
- package/dist/src/components/DynamicFormItem.validation.test.d.ts +1 -0
- package/dist/src/components/DynamicFormItem.vue.d.ts +9 -2
- package/dist/src/components/DynamicFormItemArray.logic.test.d.ts +1 -0
- package/dist/src/components/DynamicFormItemArray.validation.test.d.ts +1 -0
- package/dist/src/components/DynamicFormItemArray.vue.d.ts +7 -2
- package/dist/src/components/DynamicFormItemChoice.logic.test.d.ts +1 -0
- package/dist/src/components/DynamicFormItemChoice.validation.test.d.ts +1 -0
- package/dist/src/components/DynamicFormItemChoice.vue.d.ts +7 -2
- package/dist/src/components/DynamicFormTemplate.vue.d.ts +78 -20
- package/dist/src/core/defineMetadata.d.ts +31 -7
- package/dist/src/core/useDynamicForm.d.ts +14 -0
- package/dist/src/core/useFieldArrayExtended.d.ts +16 -0
- package/dist/src/core/validation.d.ts +7 -0
- package/dist/src/core/validation.test.d.ts +1 -0
- package/dist/src/examples/DynamicForm.test.d.ts +1 -0
- package/dist/src/examples/TestForm.vue.d.ts +9 -0
- package/dist/src/examples/TestFormTemplate.testcases.d.ts +8 -0
- package/dist/src/examples/TestFormTemplate.vue.d.ts +75 -0
- package/dist/src/examples/components/IconButton.vue.d.ts +10 -0
- package/dist/src/examples/index.d.ts +3 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/test-setup.d.ts +1 -0
- package/dist/src/types/DynamicFormItemProps.d.ts +56 -1
- package/dist/src/types/DynamicFormSettings.d.ts +89 -0
- package/dist/src/types/FieldMetadata.d.ts +87 -17
- package/dist/src/types/InternalFieldMetadata.d.ts +2 -2
- package/dist/src/types/KeyValue.d.ts +3 -0
- package/dist/src/types/MetadataConfiguration.d.ts +1 -0
- package/dist/src/types/ValidationMessage.d.ts +3 -0
- package/dist/src/utils/camelize.d.ts +5 -0
- package/dist/src/utils/checkTreeHasValue.d.ts +9 -0
- package/dist/src/utils/checkTreeHasValue.test.d.ts +1 -0
- package/dist/src/utils/createValidation.d.ts +12 -0
- package/dist/src/utils/createValidation.test.d.ts +1 -0
- package/dist/src/utils/getFieldLabel.d.ts +16 -0
- package/dist/src/utils/normalizePath.d.ts +15 -0
- package/dist/src/utils/normalizePath.test.d.ts +1 -0
- package/dist/src/utils/overridePath.d.ts +19 -0
- package/dist/src/utils/overridePath.test.d.ts +1 -0
- package/dist/src/utils/resolveMessage.d.ts +17 -0
- package/dist/src/utils/resolveMessage.test.d.ts +1 -0
- package/dist/src/utils/setInPath.d.ts +10 -0
- package/dist/src/utils/setInPath.test.d.ts +1 -0
- package/dist/src/utils/splitValidationFunctions.d.ts +15 -0
- package/dist/src/utils/splitValidationFunctions.test.d.ts +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/useDynamicForm-3E1RNkVq.cjs +6 -0
- package/dist/useDynamicForm-3E1RNkVq.cjs.map +1 -0
- package/dist/useDynamicForm-B6YN35Qb.js +684 -0
- package/dist/useDynamicForm-B6YN35Qb.js.map +1 -0
- package/dist/vue-dynamic-form.cjs.js +2 -0
- package/dist/vue-dynamic-form.cjs.js.map +1 -0
- package/dist/vue-dynamic-form.es.js +13 -178
- package/dist/vue-dynamic-form.es.js.map +1 -0
- package/package.json +17 -9
- package/dist/vue-dynamic-form.umd.js +0 -1
|
@@ -1,25 +1,47 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FieldOptions, RuleExpression } from 'vee-validate';
|
|
2
|
+
import type { Ref } from 'vue';
|
|
2
3
|
export type FieldMetadata<ExtendedFieldTypes extends string = string, ExtendedProperties extends object = object> = {
|
|
3
4
|
name?: string;
|
|
4
5
|
type?: ExtendedFieldTypes;
|
|
5
6
|
path?: string;
|
|
7
|
+
/**
|
|
8
|
+
* The minimal occurrence of a value.
|
|
9
|
+
* Optional fields have a value of 0.
|
|
10
|
+
* Required fields have a value of 1.
|
|
11
|
+
* We also allow to specify if you need a minimum higher then 1.
|
|
12
|
+
*/
|
|
6
13
|
minOccurs?: number;
|
|
14
|
+
/**
|
|
15
|
+
* The maximal occurrence of a value.
|
|
16
|
+
*/
|
|
7
17
|
maxOccurs?: number;
|
|
8
18
|
/**
|
|
9
|
-
* Simple restrictions to the data
|
|
19
|
+
* Simple restrictions to the data, these are available in XSD and can be applied easily.
|
|
20
|
+
* For more advance validation use the validation property.
|
|
10
21
|
*/
|
|
11
|
-
|
|
12
|
-
maxLength?: number;
|
|
22
|
+
restriction?: {
|
|
13
23
|
minLength?: number;
|
|
24
|
+
maxLength?: number;
|
|
14
25
|
pattern?: string;
|
|
15
26
|
minInclusive?: number;
|
|
16
27
|
maxInclusive?: number;
|
|
17
|
-
|
|
28
|
+
minExclusive?: number;
|
|
29
|
+
maxExclusive?: number;
|
|
30
|
+
enumeration?: unknown[];
|
|
18
31
|
length?: number;
|
|
19
|
-
whiteSpace?:
|
|
32
|
+
whiteSpace?: 'preserve' | 'replace' | 'collapse';
|
|
20
33
|
fractionDigits?: number;
|
|
21
34
|
totalDigits?: number;
|
|
22
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Vee-Validate validation rules that should be applied to this field.
|
|
38
|
+
*/
|
|
39
|
+
validation?: RuleExpression<unknown>;
|
|
40
|
+
/**
|
|
41
|
+
* The vee-validate field options.
|
|
42
|
+
* - label: you can set a ref to make this dynamically update.
|
|
43
|
+
*/
|
|
44
|
+
fieldOptions?: Partial<FieldOptions<unknown>>;
|
|
23
45
|
children?: FieldMetadata<ExtendedFieldTypes, ExtendedProperties>[];
|
|
24
46
|
/**
|
|
25
47
|
* Choice is similar to children, with the difference that only one of the items in the array
|
|
@@ -50,15 +72,34 @@ export type FieldMetadata<ExtendedFieldTypes extends string = string, ExtendedPr
|
|
|
50
72
|
* <ContactInfo>
|
|
51
73
|
* <Email>someone@example.com</Email>
|
|
52
74
|
* </ContactInfo>
|
|
53
|
-
* or
|
|
75
|
+
* <!-- or -->
|
|
54
76
|
* <ContactInfo>
|
|
55
77
|
* <Phone>+31 123 456 789</Phone>
|
|
56
78
|
* </ContactInfo>
|
|
57
79
|
*
|
|
58
|
-
* but this is invalid:
|
|
80
|
+
* <!-- but this is invalid: -->
|
|
59
81
|
* <ContactInfo>
|
|
60
82
|
* </ContactInfo>
|
|
83
|
+
* ```
|
|
61
84
|
*
|
|
85
|
+
* Or json
|
|
86
|
+
* ```json
|
|
87
|
+
* {
|
|
88
|
+
* "contactInfo": {
|
|
89
|
+
* "email": "someone@example.com"
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* <!-- or -->
|
|
93
|
+
* {
|
|
94
|
+
* "contactInfo": {
|
|
95
|
+
* "phone": "+31 123 456 789"
|
|
96
|
+
* }
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* <!-- but this is invalid: -->
|
|
100
|
+
* {
|
|
101
|
+
* "contactInfo": { }
|
|
102
|
+
* }
|
|
62
103
|
* ```
|
|
63
104
|
*
|
|
64
105
|
*/
|
|
@@ -69,33 +110,62 @@ export type FieldMetadata<ExtendedFieldTypes extends string = string, ExtendedPr
|
|
|
69
110
|
* such as for example whether the data is verified.
|
|
70
111
|
*
|
|
71
112
|
* These attributes will be shown when the parent will have a value filled in.
|
|
113
|
+
* The field automatically becomes a complex type, meaning that the value will be
|
|
114
|
+
* an object containing the value and the attributes.
|
|
72
115
|
*
|
|
73
116
|
* For example:
|
|
74
117
|
* ```
|
|
75
118
|
* <ContactInfo>
|
|
76
119
|
* <Phone certainty="unverified">+31 123 456 789</Phone>
|
|
77
120
|
* </ContactInfo>
|
|
78
|
-
* or
|
|
121
|
+
* <!-- or -->
|
|
79
122
|
* <ContactInfo>
|
|
80
123
|
* <Phone certainty="verified">+31 123 456 789</Phone>
|
|
81
124
|
* </ContactInfo>
|
|
82
125
|
* ```
|
|
83
126
|
*/
|
|
84
127
|
attributes?: FieldMetadata<ExtendedFieldTypes, ExtendedProperties>[];
|
|
128
|
+
/**
|
|
129
|
+
* To support generating a form based on a xsd, we need to be able to work with complex types.
|
|
130
|
+
* In case a SimpleContent has a ComplexType as parent, the json converter creates a value object
|
|
131
|
+
* with the value inside i.e. { value: 'the actual value' } and the attributes alongside it.
|
|
132
|
+
*
|
|
133
|
+
* By setting isComplexType to true, the form generator knows it needs to look for the value inside
|
|
134
|
+
* the value property.
|
|
135
|
+
*
|
|
136
|
+
* When attributes are present, isComplexType is automatically set to true.
|
|
137
|
+
*
|
|
138
|
+
* In case you need to change the default value property 'value', do this in the DynamicFormSettings.
|
|
139
|
+
*/
|
|
140
|
+
isComplexType?: boolean;
|
|
85
141
|
/**
|
|
86
142
|
* The parent of the current field.
|
|
143
|
+
* This is purely for reference, these values cannot be updated.
|
|
87
144
|
*/
|
|
88
|
-
parent?: FieldMetadata<ExtendedFieldTypes, ExtendedProperties
|
|
145
|
+
parent?: Readonly<FieldMetadata<ExtendedFieldTypes, ExtendedProperties>>;
|
|
89
146
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* This
|
|
147
|
+
* Compute field values locally in the component that represents the field.
|
|
148
|
+
*
|
|
149
|
+
* This is a list of functions that are executed inside a computed in the component of the field.
|
|
150
|
+
* This means that if you reference other computed, refs, etc, these functions will be re-evaluated once one of the used reactive values changes.
|
|
151
|
+
*
|
|
152
|
+
* Allowing you to change the properties in the fieldMetadata, based on other values in the form, without re-rendering the entire component tree.
|
|
153
|
+
*
|
|
154
|
+
* It is also possible to read & write
|
|
94
155
|
*
|
|
95
156
|
* @param thisField the reactive & cloned current MetadataField that can be changed.
|
|
157
|
+
* @param fieldValue the value of the field, you can read and modify it. If you do, the reactivity is activated and the computed
|
|
158
|
+
* is re-computed every time the value changes. If you don't use it, a change in value will not re-trigger the reactivity of the computed.
|
|
96
159
|
* @returns A transformed FieldMetadata
|
|
97
160
|
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
161
|
+
computedProps?: ((thisField: ComputedPropsType<FieldMetadata<ExtendedFieldTypes, ExtendedProperties>>, fieldValue: Ref<unknown>) => void)[];
|
|
162
|
+
/**
|
|
163
|
+
* Normally the computedProps are not re-calculate if a child value changes. If you need this functionality, you can enable this by
|
|
164
|
+
* setting this property to true.
|
|
165
|
+
*/
|
|
166
|
+
computeOnChildValueChange?: boolean;
|
|
101
167
|
} & ExtendedProperties;
|
|
168
|
+
export type ComputedPropsType<T> = Omit<T, 'name' | 'path' | 'children' | 'choice' | 'attributes' | 'fieldOptions' | 'computedProps' | 'maxOccurs'> & Readonly<{
|
|
169
|
+
name: string;
|
|
170
|
+
path: string;
|
|
171
|
+
}>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { FieldMetadata } from '
|
|
2
|
-
import type { RequireOnly } from '
|
|
1
|
+
import type { FieldMetadata } from '../types/FieldMetadata.js';
|
|
2
|
+
import type { RequireOnly } from '../types/RequireOnly.js';
|
|
3
3
|
export type InternalFieldMetadata<Metadata extends FieldMetadata> = RequireOnly<Metadata, 'name' | 'type' | 'minOccurs' | 'maxOccurs'>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively traverses a data structure to determine if it holds any meaningful data.
|
|
3
|
+
* Returns false only when the entire structure consists of null/undefined values.
|
|
4
|
+
* Particularly useful for validation frameworks to detect populated form fields.
|
|
5
|
+
*
|
|
6
|
+
* @param element Data structure to analyze (supports primitives, objects, arrays)
|
|
7
|
+
* @returns Boolean indicating presence of non-nullish values
|
|
8
|
+
*/
|
|
9
|
+
export declare function checkTreeHasValue(element: unknown): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ValidationRule } from '../core/validation';
|
|
2
|
+
import type { FieldValidationMetaInfo, ValidationMessage } from '../types/ValidationMessage';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a validation that uses a globally defined rule in our 'vdf' namespace.
|
|
5
|
+
* When the validation fails we either show the message override from the settings.messages
|
|
6
|
+
* or let the user override the message using the configure.generateMessage
|
|
7
|
+
*
|
|
8
|
+
* @param rule
|
|
9
|
+
* @param param
|
|
10
|
+
* @param customMessage
|
|
11
|
+
*/
|
|
12
|
+
export declare function createValidation(rule: ValidationRule, param?: unknown, customMessage?: ValidationMessage): (value: unknown, ctx: FieldValidationMetaInfo) => Promise<string | true>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '../core/validation';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FieldValidationMetaInfo } from '../types/ValidationMessage';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the most user-friendly label for a field validation context.
|
|
4
|
+
*
|
|
5
|
+
* Most components (e.g. DynamicFormItem, DynamicFormItemArray) call `useField`
|
|
6
|
+
* with the field's `fieldOptions`, so vee-validate already sets `ctx.label`
|
|
7
|
+
* and `ctx.field` to a human-readable name. Choice fields are special: they
|
|
8
|
+
* validate at the root path (`''`) and historically registered without any
|
|
9
|
+
* `fieldOptions`, which left vee-validate with the literal placeholder
|
|
10
|
+
* string `{field}`. Normalizing the label here ensures shared helpers like
|
|
11
|
+
* createValidation/splitValidationFunctions always receive a real display name
|
|
12
|
+
* even for those “virtual” fields.
|
|
13
|
+
*
|
|
14
|
+
* Preference order: explicit label → ctx.field → ctx.name → empty string.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getFieldLabel(ctx: FieldValidationMetaInfo): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper function that converts a path with bracket notation to a dot notation.
|
|
3
|
+
* When adding an extra path segment, we do this by using the bracket notation, i.e.: person['value'].
|
|
4
|
+
*
|
|
5
|
+
* Vee-validate can't work with this notation, therefore this function fixes those cases.
|
|
6
|
+
*
|
|
7
|
+
* const actualPath = normalizePath(
|
|
8
|
+
* 'this.is["added"].to.my.path.and.more',
|
|
9
|
+
* )
|
|
10
|
+
*
|
|
11
|
+
* console.log(actualPath) // 'this.is.added.to.my.path.and.more'
|
|
12
|
+
*
|
|
13
|
+
* @param path to be normalized
|
|
14
|
+
*/
|
|
15
|
+
export declare function normalizePath(path: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merges two dot-notation paths by replacing leading segments of the base path
|
|
3
|
+
* with segments from the replacement path. Useful for resolving array indices
|
|
4
|
+
* in nested field paths where parent context contains array positions.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* // Given a generic path and an indexed parent path, produce the indexed child path
|
|
8
|
+
* const result = overridePath(
|
|
9
|
+
* 'data.users.contact.email',
|
|
10
|
+
* 'data.users[2]'
|
|
11
|
+
* )
|
|
12
|
+
*
|
|
13
|
+
* console.log(result) // 'data.users[2].contact.email'
|
|
14
|
+
*
|
|
15
|
+
* @param path Base path string to be modified
|
|
16
|
+
* @param overrideWithPath Replacement path that will substitute initial segments
|
|
17
|
+
* @returns Modified path with replaced segments
|
|
18
|
+
*/
|
|
19
|
+
export declare function overridePath(path: string, overrideWithPath: string | undefined): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { FieldValidationMetaInfo, ValidationMessage } from '../types/ValidationMessage';
|
|
2
|
+
/**
|
|
3
|
+
* Helper function to resolve a validation message, with parameters.
|
|
4
|
+
*
|
|
5
|
+
* All three param styles work:
|
|
6
|
+
* 'Must be at least {0} characters' // positional
|
|
7
|
+
* 'Must be at least {min} characters' // named (object params)
|
|
8
|
+
* 'The {field} value {value} is invalid' // context properties
|
|
9
|
+
*
|
|
10
|
+
* This is exactly how @vee-validate/i18n handles it internally.
|
|
11
|
+
* @param message - The message template or function
|
|
12
|
+
* @param ctx - The vee-validate field validation context
|
|
13
|
+
* @param params - Optional param override. Array for positional `{0}, {1}`, or object for
|
|
14
|
+
* named `{min}, {length}` (also supports positional via numeric keys). Falls back to ctx.rule.params if not provided.
|
|
15
|
+
* @returns The resolved message string
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveMessage(message: ValidationMessage, ctx: FieldValidationMetaInfo, params?: unknown[] | Record<string, unknown>): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets a value at a nested path within an object.
|
|
3
|
+
* Based on vee-validate's internal setInPath implementation.
|
|
4
|
+
* Creates intermediate objects/arrays as needed.
|
|
5
|
+
*
|
|
6
|
+
* @param object - The object to modify
|
|
7
|
+
* @param path - Dot-separated path (e.g., "user.address.city") or with array notation (e.g., "items[0].name")
|
|
8
|
+
* @param value - The value to set at the path
|
|
9
|
+
*/
|
|
10
|
+
export declare function setInPath(object: Record<string, any>, path: string, value: unknown): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { GenericValidateFunction, RuleExpression } from 'vee-validate';
|
|
2
|
+
/**
|
|
3
|
+
* Splits a vee-validate `RuleExpression` into an array of individual `GenericValidateFunction`s,
|
|
4
|
+
* one per rule. This allows vee-validate to run each rule independently and collect all errors
|
|
5
|
+
* when `bails: false` is configured.
|
|
6
|
+
*
|
|
7
|
+
* Handles all `RuleExpression` formats:
|
|
8
|
+
* - **Function** — returned as-is in a single-element array
|
|
9
|
+
* - **Array** — each element is recursively split
|
|
10
|
+
* - **String** — pipe-separated rules (e.g. `'required|email'`) are split into separate functions,
|
|
11
|
+
* each delegating to vee-validate's `validate()`
|
|
12
|
+
* - **Object** — each key/value pair (e.g. `{ required: true, min: 3 }`) becomes a separate function,
|
|
13
|
+
* each delegating to vee-validate's `validate()`
|
|
14
|
+
*/
|
|
15
|
+
export declare function splitToValidationFunctions(userRules: RuleExpression<unknown>): GenericValidateFunction[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|