@volverjs/form-vue 1.0.0-beta.11 → 1.0.0-beta.13

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/src/types.ts CHANGED
@@ -1,87 +1,85 @@
1
- import type { Component, DeepReadonly, Ref, WatchStopHandle } from 'vue'
1
+ import type { Component, DeepReadonly, Ref, RendererElement, RendererNode, VNode, WatchStopHandle } from 'vue'
2
2
  import type { z } from 'zod'
3
3
  import type { IgnoredUpdater } from '@vueuse/core'
4
4
  import type { FormFieldType, FormStatus } from './enums'
5
5
 
6
6
  export type FormSchema =
7
- | z.AnyZodObject
8
- | z.ZodEffects<z.AnyZodObject>
9
- | z.ZodEffects<z.ZodEffects<z.AnyZodObject>>
7
+ | z.AnyZodObject
8
+ | z.ZodEffects<z.AnyZodObject>
9
+ | z.ZodEffects<z.ZodEffects<z.AnyZodObject>>
10
10
 
11
11
  export type FormFieldComponentOptions = {
12
- lazyLoad?: boolean
13
- sideEffects?: (type: `${FormFieldType}`) => Promise<void> | void
12
+ lazyLoad?: boolean
13
+ sideEffects?: (type: `${FormFieldType}`) => Promise<void> | void
14
14
  }
15
15
 
16
16
  export type FormComponentOptions<Schema> = {
17
- updateThrottle?: number
18
- continuousValidation?: boolean
19
- readonly?: boolean
20
- template?: Schema extends FormSchema ? FormTemplate<Schema> : never
21
- onUpdate?: Schema extends FormSchema
22
- ? (data?: Partial<z.infer<Schema>>) => void
23
- : never
24
- onSubmit?: Schema extends FormSchema
25
- ? (data?: z.infer<Schema>) => void
26
- : never
27
- onInvalid?: Schema extends FormSchema
28
- ? (error?: z.inferFormattedError<Schema>) => void
29
- : never
30
- onValid?: Schema extends FormSchema
31
- ? (data?: z.infer<Schema>) => void
32
- : never
17
+ updateThrottle?: number
18
+ continuousValidation?: boolean
19
+ readonly?: boolean
20
+ template?: Schema extends FormSchema ? FormTemplate<Schema> : never
21
+ onUpdate?: Schema extends FormSchema
22
+ ? (data?: Partial<z.infer<Schema>>) => void
23
+ : never
24
+ onSubmit?: Schema extends FormSchema
25
+ ? (data?: z.infer<Schema>) => void
26
+ : never
27
+ onInvalid?: Schema extends FormSchema
28
+ ? (error?: z.inferFormattedError<Schema>) => void
29
+ : never
30
+ onValid?: Schema extends FormSchema
31
+ ? (data?: z.infer<Schema>) => void
32
+ : never
33
33
  }
34
34
 
35
35
  export type FormComposableOptions<Schema> = FormFieldComponentOptions &
36
- FormComponentOptions<Schema>
36
+ FormComponentOptions<Schema>
37
37
 
38
38
  type FormPluginOptionsSchema = {
39
- schema?: FormSchema
39
+ schema?: FormSchema
40
40
  }
41
41
 
42
42
  export type FormPluginOptions = FormPluginOptionsSchema &
43
- FormComposableOptions<FormPluginOptionsSchema['schema']>
43
+ FormComposableOptions<FormPluginOptionsSchema['schema']>
44
44
 
45
45
  export type InjectedFormData<Schema extends FormSchema> = {
46
- formData: Ref<Partial<z.infer<Schema>> | undefined>
47
- errors: Readonly<
46
+ formData: Ref<Partial<z.infer<Schema>> | undefined>
47
+ errors: Readonly<
48
48
  Ref<DeepReadonly<z.inferFormattedError<Schema>> | undefined>
49
49
  >
50
- submit: () => Promise<boolean>
51
- validate: () => Promise<boolean>
52
- ignoreUpdates: IgnoredUpdater
53
- stopUpdatesWatch: WatchStopHandle
54
- status: Readonly<Ref<FormStatus | undefined>>
55
- invalid: Readonly<Ref<boolean>>
56
- readonly: Ref<boolean>
50
+ submit: () => Promise<boolean>
51
+ validate: () => Promise<boolean>
52
+ ignoreUpdates: IgnoredUpdater
53
+ stopUpdatesWatch: WatchStopHandle
54
+ status: Readonly<Ref<FormStatus | undefined>>
55
+ invalid: Readonly<Ref<boolean>>
56
+ readonly: Ref<boolean>
57
57
  }
58
58
 
59
59
  export type InjectedFormWrapperData<Schema extends FormSchema> = {
60
- name: Ref<string>
61
- fields: Ref<Set<string>>
62
- errors: Ref<Map<string, z.inferFormattedError<Schema>>>
60
+ name: Ref<string>
61
+ fields: Ref<Set<string>>
62
+ errors: Ref<Map<string, z.inferFormattedError<Schema>>>
63
63
  }
64
64
 
65
65
  export type InjectedFormFieldData<Schema extends FormSchema> = {
66
- name: Ref<string>
67
- errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
66
+ name: Ref<string>
67
+ errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
68
68
  }
69
69
 
70
70
  export type Primitive =
71
- | null
72
- | undefined
73
- | string
74
- | number
75
- | boolean
76
- | symbol
77
- | bigint
78
-
79
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
71
+ | null
72
+ | undefined
73
+ | string
74
+ | number
75
+ | boolean
76
+ | symbol
77
+ | bigint
78
+
80
79
  type IsTuple<T extends readonly any[]> = number extends T['length']
81
- ? false
82
- : true
80
+ ? false
81
+ : true
83
82
 
84
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
83
  type TupleKeys<T extends readonly any[]> = Exclude<keyof T, keyof any[]>
86
84
 
87
85
  export type PathConcat<
@@ -90,89 +88,87 @@ export type PathConcat<
90
88
  > = TValue extends Primitive ? `${TKey}` : `${TKey}` | `${TKey}.${Path<TValue>}`
91
89
 
92
90
  export type Path<T> = T extends readonly (infer V)[]
93
- ? IsTuple<T> extends true
94
- ? {
95
- [K in TupleKeys<T>]-?: PathConcat<K & string, T[K]>
96
- }[TupleKeys<T>]
97
- : PathConcat<number, V>
98
- : {
99
- [K in keyof T]-?: PathConcat<K & string, T[K]>
100
- }[keyof T]
101
-
102
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
+ ? IsTuple<T> extends true
92
+ ? {
93
+ [K in TupleKeys<T>]-?: PathConcat<K & string, T[K]>
94
+ }[TupleKeys<T>]
95
+ : PathConcat<number, V>
96
+ : {
97
+ [K in keyof T]-?: PathConcat<K & string, T[K]>
98
+ }[keyof T]
99
+
103
100
  export type PathValue<T, TPath extends Path<T> | Path<T>[]> = T extends any
104
- ? TPath extends `${infer K}.${infer R}`
105
- ? K extends keyof T
106
- ? R extends Path<T[K]>
107
- ? undefined extends T[K]
108
- ? PathValue<T[K], R> | undefined
109
- : PathValue<T[K], R>
110
- : never
111
- : K extends `${number}`
112
- ? T extends readonly (infer V)[]
113
- ? PathValue<V, R & Path<V>>
114
- : never
115
- : never
116
- : TPath extends keyof T
117
- ? T[TPath]
118
- : TPath extends `${number}`
119
- ? T extends readonly (infer V)[]
120
- ? V
121
- : never
122
- : never
123
- : never
101
+ ? TPath extends `${infer K}.${infer R}`
102
+ ? K extends keyof T
103
+ ? R extends Path<T[K]>
104
+ ? undefined extends T[K]
105
+ ? PathValue<T[K], R> | undefined
106
+ : PathValue<T[K], R>
107
+ : never
108
+ : K extends `${number}`
109
+ ? T extends readonly (infer V)[]
110
+ ? PathValue<V, R & Path<V>>
111
+ : never
112
+ : never
113
+ : TPath extends keyof T
114
+ ? T[TPath]
115
+ : TPath extends `${number}`
116
+ ? T extends readonly (infer V)[]
117
+ ? V
118
+ : never
119
+ : never
120
+ : never
124
121
 
125
122
  export type AnyBoolean<Schema extends FormSchema> =
126
- | boolean
127
- | Ref<boolean>
128
- | ((data?: InjectedFormData<Schema>) => boolean | Ref<boolean>)
123
+ | boolean
124
+ | Ref<boolean>
125
+ | ((data?: InjectedFormData<Schema>) => boolean | Ref<boolean>)
129
126
 
130
127
  export type SimpleFormTemplateItem<Schema extends FormSchema> = Record<
131
128
  string,
132
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
133
129
  any
134
130
  > & {
135
- vvIs?: string | Component
136
- vvName?: Path<z.infer<Schema>>
137
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
138
- vvSlots?: Record<string, any>
139
- vvChildren?:
140
- | Array<
141
- | SimpleFormTemplateItem<Schema>
142
- | ((
143
- data?: InjectedFormData<Schema>,
144
- scope?: Record<string, unknown>,
131
+ vvIs?: string | Component
132
+ vvName?: Path<z.infer<Schema>>
133
+ vvSlots?: Record<string, any>
134
+ vvChildren?:
135
+ | Array<
136
+ | SimpleFormTemplateItem<Schema>
137
+ | ((
138
+ data?: InjectedFormData<Schema>,
139
+ scope?: Record<string, unknown>,
145
140
  ) => SimpleFormTemplateItem<Schema>)
146
141
  >
147
- | ((
148
- data?: InjectedFormData<Schema>,
149
- scope?: Record<string, unknown>,
142
+ | ((
143
+ data?: InjectedFormData<Schema>,
144
+ scope?: Record<string, unknown>,
150
145
  ) => Array<
151
- | SimpleFormTemplateItem<Schema>
152
- | ((
153
- data?: InjectedFormData<Schema>,
154
- scope?: Record<string, unknown>,
146
+ | SimpleFormTemplateItem<Schema>
147
+ | ((
148
+ data?: InjectedFormData<Schema>,
149
+ scope?: Record<string, unknown>,
155
150
  ) => SimpleFormTemplateItem<Schema>)
156
151
  >)
157
- vvIf?: AnyBoolean<Schema> | Path<z.infer<Schema>>
158
- vvElseIf?: AnyBoolean<Schema> | Path<z.infer<Schema>>
159
- vvType?: `${FormFieldType}`
160
- vvShowValid?: boolean
161
- vvContent?: string
162
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
163
- vvDefaultValue?: any
152
+ vvIf?: AnyBoolean<Schema> | Path<z.infer<Schema>>
153
+ vvElseIf?: AnyBoolean<Schema> | Path<z.infer<Schema>>
154
+ vvType?: `${FormFieldType}`
155
+ vvShowValid?: boolean
156
+ vvContent?: string
157
+ vvDefaultValue?: any
164
158
  }
165
159
 
166
160
  export type FormTemplateItem<Schema extends FormSchema> =
167
- | SimpleFormTemplateItem<Schema>
168
- | ((
169
- data?: InjectedFormData<Schema>,
170
- scope?: Record<string, unknown>,
161
+ | SimpleFormTemplateItem<Schema>
162
+ | ((
163
+ data?: InjectedFormData<Schema>,
164
+ scope?: Record<string, unknown>,
171
165
  ) => SimpleFormTemplateItem<Schema>)
172
166
 
173
167
  export type FormTemplate<Schema extends FormSchema> =
174
- | FormTemplateItem<Schema>[]
175
- | ((
176
- data?: InjectedFormData<Schema>,
177
- scope?: Record<string, unknown>,
168
+ | FormTemplateItem<Schema>[]
169
+ | ((
170
+ data?: InjectedFormData<Schema>,
171
+ scope?: Record<string, unknown>,
178
172
  ) => FormTemplateItem<Schema>[])
173
+
174
+ export type RenderFunctionOutput = VNode<RendererNode, RendererElement, { [key: string]: any }>
package/src/utils.ts CHANGED
@@ -1,104 +1,100 @@
1
1
  import {
2
- type z,
3
- type AnyZodObject,
4
- type ZodTypeAny,
5
- ZodDefault,
6
- ZodObject,
7
- ZodEffects,
8
- ZodSchema,
9
- ZodNullable,
10
- ZodOptional,
11
- ZodArray,
2
+ type z,
3
+ type AnyZodObject,
4
+ type ZodTypeAny,
5
+ ZodDefault,
6
+ ZodObject,
7
+ ZodEffects,
8
+ ZodSchema,
9
+ ZodNullable,
10
+ ZodOptional,
11
+ ZodArray,
12
12
  } from 'zod'
13
13
  import type { FormSchema } from './types'
14
14
 
15
- export const defaultObjectBySchema = <Schema extends FormSchema>(
16
- schema: Schema,
17
- original: Partial<z.infer<Schema>> & Record<string, unknown> = {},
18
- ): Partial<z.infer<Schema>> => {
19
- const getInnerType = <Type extends ZodTypeAny>(
20
- schema:
21
- | Type
22
- | ZodEffects<Type>
23
- | ZodEffects<ZodEffects<Type>>
24
- | ZodOptional<Type>,
25
- ) => {
26
- let toReturn = schema
27
- while (toReturn instanceof ZodEffects) {
28
- toReturn = toReturn.innerType()
29
- }
30
- if (toReturn instanceof ZodOptional) {
31
- toReturn = toReturn._def.innerType
32
- }
33
- return toReturn
34
- }
35
- const innerType = getInnerType<AnyZodObject>(schema)
36
- const unknownKeys =
37
- innerType instanceof ZodObject
38
- ? innerType._def.unknownKeys === 'passthrough'
39
- : false
40
- return {
41
- ...(unknownKeys ? original : {}),
42
- ...Object.fromEntries(
43
- (Object.entries(innerType.shape) as [string, ZodTypeAny][]).map(
44
- ([key, subSchema]) => {
45
- const originalValue = original[key]
46
- let innerType = getInnerType(subSchema)
47
- let defaultValue: Partial<z.infer<Schema>> | undefined =
48
- undefined
49
- if (innerType instanceof ZodDefault) {
50
- defaultValue = innerType._def.defaultValue()
51
- innerType = innerType._def.innerType
52
- }
53
- if (
54
- originalValue === null &&
55
- innerType instanceof ZodNullable
56
- ) {
57
- return [key, originalValue]
58
- }
59
- if (innerType instanceof ZodSchema) {
60
- const parse = subSchema.safeParse(originalValue)
61
- if (parse.success) {
62
- return [key, parse.data ?? defaultValue]
63
- }
64
- }
65
- if (
66
- innerType instanceof ZodArray &&
67
- Array.isArray(originalValue) &&
68
- originalValue.length
69
- ) {
70
- const arrayType = getInnerType(innerType._def.type)
71
- if (arrayType instanceof ZodObject) {
72
- return [
73
- key,
74
- originalValue.map((element: unknown) =>
75
- defaultObjectBySchema(
76
- arrayType,
77
- (element && typeof element === 'object'
78
- ? element
79
- : undefined) as Partial<
15
+ export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema, original: Partial<z.infer<Schema>> & Record<string, unknown> = {}): Partial<z.infer<Schema>> {
16
+ const getInnerType = <Type extends ZodTypeAny>(
17
+ schema:
18
+ | Type
19
+ | ZodEffects<Type>
20
+ | ZodEffects<ZodEffects<Type>>
21
+ | ZodOptional<Type>,
22
+ ) => {
23
+ let toReturn = schema
24
+ while (toReturn instanceof ZodEffects) {
25
+ toReturn = toReturn.innerType()
26
+ }
27
+ if (toReturn instanceof ZodOptional) {
28
+ toReturn = toReturn._def.innerType
29
+ }
30
+ return toReturn
31
+ }
32
+ const innerType = getInnerType<AnyZodObject>(schema)
33
+ const unknownKeys
34
+ = innerType instanceof ZodObject
35
+ ? innerType._def.unknownKeys === 'passthrough'
36
+ : false
37
+ return {
38
+ ...(unknownKeys ? original : {}),
39
+ ...Object.fromEntries(
40
+ (Object.entries(innerType.shape) as [string, ZodTypeAny][]).map(
41
+ ([key, subSchema]) => {
42
+ const originalValue = original[key]
43
+ let innerType = getInnerType(subSchema)
44
+ let defaultValue: Partial<z.infer<Schema>> | undefined
45
+ if (innerType instanceof ZodDefault) {
46
+ defaultValue = innerType._def.defaultValue()
47
+ innerType = innerType._def.innerType
48
+ }
49
+ if (
50
+ originalValue === null
51
+ && innerType instanceof ZodNullable
52
+ ) {
53
+ return [key, originalValue]
54
+ }
55
+ if (innerType instanceof ZodSchema) {
56
+ const parse = subSchema.safeParse(originalValue)
57
+ if (parse.success) {
58
+ return [key, parse.data ?? defaultValue]
59
+ }
60
+ }
61
+ if (
62
+ innerType instanceof ZodArray
63
+ && Array.isArray(originalValue)
64
+ && originalValue.length
65
+ ) {
66
+ const arrayType = getInnerType(innerType._def.type)
67
+ if (arrayType instanceof ZodObject) {
68
+ return [
69
+ key,
70
+ originalValue.map((element: unknown) =>
71
+ defaultObjectBySchema(
72
+ arrayType,
73
+ (element && typeof element === 'object'
74
+ ? element
75
+ : undefined) as Partial<
80
76
  typeof arrayType
81
77
  >,
82
- ),
83
- ) ?? defaultValue,
84
- ]
85
- }
86
- }
87
- if (innerType instanceof ZodObject) {
88
- return [
89
- key,
90
- defaultObjectBySchema(
91
- innerType,
92
- originalValue &&
93
- typeof originalValue === 'object'
94
- ? originalValue
95
- : defaultValue,
96
- ),
97
- ]
98
- }
99
- return [key, defaultValue]
100
- },
101
- ),
102
- ),
103
- } as Partial<z.infer<Schema>>
78
+ ),
79
+ ) ?? defaultValue,
80
+ ]
81
+ }
82
+ }
83
+ if (innerType instanceof ZodObject) {
84
+ return [
85
+ key,
86
+ defaultObjectBySchema(
87
+ innerType,
88
+ originalValue
89
+ && typeof originalValue === 'object'
90
+ ? originalValue
91
+ : defaultValue,
92
+ ),
93
+ ]
94
+ }
95
+ return [key, defaultValue]
96
+ },
97
+ ),
98
+ ),
99
+ } as Partial<z.infer<Schema>>
104
100
  }