@volverjs/form-vue 1.0.0 → 1.1.0-beta.1

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.
@@ -1,5 +1,4 @@
1
1
  import type { Component, ConcreteComponent, DeepReadonly, InjectionKey, PropType, Ref, SlotsType } from 'vue'
2
- import type { z } from 'zod'
3
2
  import type {
4
3
  FormFieldComponentOptions,
5
4
  FormSchema,
@@ -7,6 +6,8 @@ import type {
7
6
  InjectedFormFieldData,
8
7
  InjectedFormWrapperData,
9
8
  Path,
9
+ InferSchema,
10
+ InferFormattedError,
10
11
  } from './types'
11
12
  import { get, set } from 'ts-dot-prop'
12
13
  import {
@@ -44,18 +45,18 @@ export function defineFormField<Schema extends FormSchema, Type = undefined>(for
44
45
  },
45
46
  name: {
46
47
  type: [String, Number, Boolean, Symbol] as PropType<
47
- Path<z.infer<Schema>>
48
+ Path<InferSchema<Schema>>
48
49
  >,
49
50
  required: true,
50
51
  },
51
52
  props: {
52
53
  type: [Object, Function] as PropType<
53
54
  Partial<
54
- | z.infer<Schema>
55
+ | InferSchema<Schema>
55
56
  | undefined
56
57
  | ((
57
58
  formData?: Ref<ObjectConstructor>,
58
- ) => Partial<z.infer<Schema>> | undefined)
59
+ ) => Partial<InferSchema<Schema>> | undefined)
59
60
  >
60
61
  >,
61
62
  default: () => ({}),
@@ -95,9 +96,9 @@ export function defineFormField<Schema extends FormSchema, Type = undefined>(for
95
96
  slots: Object as SlotsType<{
96
97
  [key: string]: any
97
98
  default: {
98
- errors: DeepReadonly<z.inferFormattedError<Schema>>
99
- formData?: undefined extends Type ? Partial<z.infer<Schema>> : Type
100
- formErrors?: DeepReadonly<z.inferFormattedError<Schema, string>>
99
+ errors: DeepReadonly<InferFormattedError<Schema>>
100
+ formData?: undefined extends Type ? Partial<InferSchema<Schema>> : Type
101
+ formErrors?: DeepReadonly<InferFormattedError<Schema>>
101
102
  invalid: boolean
102
103
  invalidLabel?: string[]
103
104
  modelValue: any
@@ -266,7 +267,7 @@ export function defineFormField<Schema extends FormSchema, Type = undefined>(for
266
267
 
267
268
  // provide data to children
268
269
  provide(formFieldInjectionKey, {
269
- name: readonly(fieldName) as Readonly<Ref<Path<z.infer<Schema>>>>,
270
+ name: readonly(fieldName) as Readonly<Ref<Path<InferSchema<Schema>>>>,
270
271
  errors: readonly(errors),
271
272
  })
272
273
 
@@ -1,11 +1,12 @@
1
1
  import type { Component, DeepReadonly, InjectionKey, PropType, Ref, SlotsType } from 'vue'
2
- import type { z } from 'zod'
3
2
  import type {
4
3
  FormSchema,
5
4
  InjectedFormData,
6
5
  InjectedFormFieldsGroupData,
7
6
  InjectedFormWrapperData,
8
7
  Path,
8
+ InferSchema,
9
+ InferFormattedError,
9
10
  } from './types'
10
11
  import { get, set } from 'ts-dot-prop'
11
12
  import {
@@ -33,18 +34,18 @@ export function defineFormFieldsGroup<Schema extends FormSchema, Type = undefine
33
34
  },
34
35
  names: {
35
36
  type: [Array, Object] as PropType<
36
- Path<z.infer<Schema>>[] | Record<string, Path<z.infer<Schema>>>
37
+ Path<InferSchema<Schema>>[] | Record<string, Path<InferSchema<Schema>>>
37
38
  >,
38
39
  required: true,
39
40
  },
40
41
  props: {
41
42
  type: [Object, Function] as PropType<
42
43
  Partial<
43
- | z.infer<Schema>
44
+ | InferSchema<Schema>
44
45
  | undefined
45
46
  | ((
46
47
  formData?: Ref<ObjectConstructor>,
47
- ) => Partial<z.infer<Schema>> | undefined)
48
+ ) => Partial<InferSchema<Schema>> | undefined)
48
49
  >
49
50
  >,
50
51
  default: () => ({}),
@@ -55,7 +56,7 @@ export function defineFormFieldsGroup<Schema extends FormSchema, Type = undefine
55
56
  },
56
57
  defaultValues: {
57
58
  type: [Object] as PropType<
58
- Record<Path<z.infer<Schema>>, any>
59
+ Record<Path<InferSchema<Schema>>, any>
59
60
  >,
60
61
  default: undefined,
61
62
  },
@@ -81,9 +82,9 @@ export function defineFormFieldsGroup<Schema extends FormSchema, Type = undefine
81
82
  slots: Object as SlotsType<{
82
83
  [key: string]: any
83
84
  default: {
84
- errors?: Record<Path<z.infer<Schema>>, z.inferFormattedError<Schema>>
85
- formData?: undefined extends Type ? Partial<z.infer<Schema>> : Type
86
- formErrors?: DeepReadonly<z.inferFormattedError<Schema>>
85
+ errors?: Record<Path<InferSchema<Schema>>, InferFormattedError<Schema>>
86
+ formData?: undefined extends Type ? Partial<InferSchema<Schema>> : Type
87
+ formErrors?: DeepReadonly<InferFormattedError<Schema>>
87
88
  invalid: boolean
88
89
  invalids: Record<string, boolean>
89
90
  invalidLabels?: Record<string, string[]>
@@ -98,7 +99,7 @@ export function defineFormFieldsGroup<Schema extends FormSchema, Type = undefine
98
99
  setup(props, { slots, emit }) {
99
100
  const { props: fieldProps, names: fieldsNames, defaultValues } = toRefs(props)
100
101
  const fieldGroupId = useId()
101
- const names = computed<Path<z.infer<Schema>>[]>(() => {
102
+ const names = computed<Path<InferSchema<Schema>>[]>(() => {
102
103
  if (Array.isArray(fieldsNames.value)) {
103
104
  return fieldsNames.value
104
105
  }
@@ -112,7 +113,7 @@ export function defineFormFieldsGroup<Schema extends FormSchema, Type = undefine
112
113
  })
113
114
  const namesMap = computed(() => {
114
115
  if (Array.isArray(fieldsNames.value)) {
115
- return fieldsNames.value.reduce<Record<string, Path<z.infer<Schema>>>>((
116
+ return fieldsNames.value.reduce<Record<string, Path<InferSchema<Schema>>>>((
116
117
  acc,
117
118
  name,
118
119
  ) => {
@@ -203,7 +204,7 @@ export function defineFormFieldsGroup<Schema extends FormSchema, Type = undefine
203
204
  if (!injectedFormData?.errors.value) {
204
205
  return undefined
205
206
  }
206
- const toReturn = names.value.reduce<Record<string, z.inferFormattedError<Schema>>>((acc, name) => {
207
+ const toReturn = names.value.reduce<Record<string, InferFormattedError<Schema>>>((acc, name) => {
207
208
  if (!injectedFormData.errors.value) {
208
209
  return acc
209
210
  }
@@ -343,7 +344,7 @@ export function defineFormFieldsGroup<Schema extends FormSchema, Type = undefine
343
344
 
344
345
  // provide data to children
345
346
  provide(formFieldsGroupInjectionKey, {
346
- names: readonly(fieldsNames) as DeepReadonly<Ref<Path<z.infer<Schema>>[]>>,
347
+ names: readonly(fieldsNames) as DeepReadonly<Ref<Path<InferSchema<Schema>>[]>>,
347
348
  errors: readonly(errors),
348
349
  })
349
350
 
@@ -1,6 +1,12 @@
1
1
  import type { Component, DeepReadonly, InjectionKey, PropType, SlotsType, VNode } from 'vue'
2
- import type { FormSchema, InjectedFormData, FormTemplate, RenderFunctionOutput } from './types'
3
- import type { z } from 'zod'
2
+ import type {
3
+ FormSchema,
4
+ InjectedFormData,
5
+ FormTemplate,
6
+ RenderFunctionOutput,
7
+ InferFormattedError,
8
+ InferSchema,
9
+ } from './types'
4
10
  import type { FormStatus } from './enums'
5
11
  import { get } from 'ts-dot-prop'
6
12
  import {
@@ -25,8 +31,8 @@ export function defineFormTemplate<Schema extends FormSchema, Type = undefined>(
25
31
  },
26
32
  slots: Object as SlotsType<{
27
33
  default: {
28
- errors?: DeepReadonly<z.inferFormattedError<Schema>>
29
- formData?: undefined extends Type ? Partial<z.infer<Schema>> : Type
34
+ errors?: DeepReadonly<InferFormattedError<Schema>>
35
+ formData?: undefined extends Type ? Partial<InferSchema<Schema>> : Type
30
36
  invalid: boolean
31
37
  status?: FormStatus
32
38
  submit?: InjectedFormData<Schema, Type>['submit']
@@ -1,10 +1,11 @@
1
1
  import type { DeepReadonly, InjectionKey, Ref, SlotsType } from 'vue'
2
- import type { z } from 'zod'
3
2
  import type {
4
3
  FormSchema,
5
4
  InjectedFormData,
6
5
  InjectedFormWrapperData,
7
6
  Path,
7
+ InferFormattedError,
8
+ InferSchema,
8
9
  } from './types'
9
10
  import {
10
11
  computed,
@@ -54,10 +55,10 @@ export function defineFormWrapper<Schema extends FormSchema, Type = undefined>(f
54
55
  ],
55
56
  slots: Object as SlotsType<{
56
57
  default: {
57
- errors?: DeepReadonly<z.inferFormattedError<Schema>>
58
- fieldsErrors: Map<string, z.inferFormattedError<Schema>>
59
- formData?: undefined extends Type ? Partial<z.infer<Schema>> : Type
60
- formErrors?: DeepReadonly<z.inferFormattedError<Schema>>
58
+ errors?: DeepReadonly<InferFormattedError<Schema>>
59
+ fieldsErrors: Map<string, InferFormattedError<Schema>>
60
+ formData?: undefined extends Type ? Partial<InferSchema<Schema>> : Type
61
+ formErrors?: DeepReadonly<InferFormattedError<Schema>>
61
62
  invalid: boolean
62
63
  readonly: boolean
63
64
  clear?: InjectedFormData<Schema, Type>['clear']
@@ -72,9 +73,9 @@ export function defineFormWrapper<Schema extends FormSchema, Type = undefined>(f
72
73
  const injectedFormData = inject(formProvideKey)
73
74
  // inject data from parent form wrapper
74
75
  const injectedWrapperData = inject(wrapperProvideKey, undefined)
75
- const fields: Ref<Map<string, Path<z.infer<Schema>>>> = ref(new Map())
76
+ const fields: Ref<Map<string, Path<InferSchema<Schema>>>> = ref(new Map())
76
77
  const fieldsErrors: Ref<
77
- Map<string, z.inferFormattedError<Schema>>
78
+ Map<string, InferFormattedError<Schema>>
78
79
  > = ref(new Map())
79
80
  const { name } = toRefs(props)
80
81
 
package/src/index.ts CHANGED
@@ -1,11 +1,5 @@
1
- import {
2
- getCurrentInstance,
3
-
4
- inject,
5
-
6
- } from 'vue'
7
1
  import type { App, InjectionKey, Plugin } from 'vue'
8
- import type { AnyZodObject } from 'zod'
2
+ import { getCurrentInstance, inject } from 'vue'
9
3
  import { defineForm } from './VvForm'
10
4
  import { defineFormField } from './VvFormField'
11
5
  import { defineFormFieldsGroup } from './VvFormFieldsGroup'
@@ -100,7 +94,8 @@ export const pluginInjectionKey = Symbol('pluginInjectionKey') as InjectionKey<F
100
94
  export function createForm(options: FormPluginOptions): Plugin & Partial<ReturnType<typeof useForm>> {
101
95
  let toReturn: Partial<ReturnType<typeof useForm>> = {}
102
96
  if (options.schema) {
103
- toReturn = _formType(options.schema as AnyZodObject, options)
97
+ // @ts-expect-error - options.schema is always defined
98
+ toReturn = _formType(options.schema, options)
104
99
  }
105
100
  return {
106
101
  ...toReturn,
package/src/types.ts CHANGED
@@ -1,18 +1,45 @@
1
1
  import type { Component, DeepReadonly, Ref, RendererElement, RendererNode, VNode, WatchStopHandle } from 'vue'
2
- import type { z, AnyZodObject, ZodEffects, ZodOptional, ZodTypeAny, RefinementCtx } from 'zod'
2
+ import type * as z3 from 'zod/v3'
3
+ import type * as z4 from 'zod/v4/core'
4
+ import type { RefinementCtx as z4RefinementCtx } from 'zod/v4'
3
5
  import type { IgnoredUpdater } from '@vueuse/core'
4
6
  import type { FormFieldType, FormStatus } from './enums'
5
7
 
6
- type Depth = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // Adjust the depth limit as needed
7
-
8
+ type Depth = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
8
9
  type DecrementDepth<D extends Depth[number]> = Depth[D]
9
10
 
10
- export type EffectType<T extends ZodTypeAny, D extends Depth[number] = 10>
11
+ export type EffectType<T extends z3.ZodTypeAny, D extends Depth[number] = 10>
11
12
  = D extends 0
12
13
  ? T
13
- : T | ZodOptional<T> | ZodEffects<EffectType<T, DecrementDepth<D>>>
14
+ : T | z3.ZodOptional<T> | z3.ZodNullable<T> | z3.ZodDefault<T> | z3.ZodEffects<EffectType<T, DecrementDepth<D>>>
15
+
16
+ export type $EffectType<T extends z4.$ZodType, D extends Depth[number] = 10>
17
+ = D extends 0
18
+ ? T
19
+ : T
20
+ | z4.$ZodOptional<$EffectType<T, DecrementDepth<D>>>
21
+ | z4.$ZodNullable<$EffectType<T, DecrementDepth<D>>>
22
+ | z4.$ZodDefault<$EffectType<T, DecrementDepth<D>>>
23
+ | z4.$ZodPipe<$EffectType<T, DecrementDepth<D>>, z4.$ZodType>
24
+
25
+ // FormSchema garantisce sempre la presenza di safeParse
26
+ export type FormSchema = z3.ZodTypeAny | z4.$ZodType
27
+
28
+ export type InferSchema<T extends FormSchema> = T extends EffectType<z3.AnyZodObject>
29
+ ? z3.z.infer<T>
30
+ : z4.infer<T>
31
+
32
+ export type InferFormattedError<T extends FormSchema> = T extends EffectType<z3.AnyZodObject>
33
+ ? z3.z.inferFormattedError<T>
34
+ : z4.$ZodFormattedError<T>
35
+
36
+ export type RefinementCtx<T extends FormSchema> = z3.RefinementCtx | z4RefinementCtx<T>
37
+
38
+ export type VvZodError<T extends FormSchema> = T extends EffectType<z3.AnyZodObject>
39
+ ? z3.ZodError<T>
40
+ : z4.$ZodError<T>
14
41
 
15
- export type FormSchema = EffectType<AnyZodObject>
42
+ export type ZodIssue = z3.ZodIssue | z4.$ZodIssue
16
43
 
17
44
  export type FormFieldComponentOptions = {
18
45
  lazyLoad?: boolean
@@ -24,19 +51,19 @@ export type FormComponentOptions<Schema, Type> = {
24
51
  continuousValidation?: boolean
25
52
  readonly?: boolean
26
53
  template?: Schema extends FormSchema ? FormTemplate<Schema, Type> : never
27
- class?: Schema extends FormSchema ? new (data?: Partial<z.infer<Schema>>) => Type : never
54
+ class?: Schema extends FormSchema ? new (data?: Partial<InferSchema<Schema>>) => Type : never
28
55
  onUpdate?: Schema extends FormSchema
29
- ? (data?: undefined extends Type ? Partial<z.infer<Schema>> : Type) => void
56
+ ? (data?: undefined extends Type ? Partial<InferSchema<Schema>> : Type) => void
30
57
  : never
31
58
  onSubmit?: Schema extends FormSchema
32
- ? (data?: undefined extends Type ? Partial<z.infer<Schema>> : Type) => void
59
+ ? (data?: undefined extends Type ? Partial<InferSchema<Schema>> : Type) => void
33
60
  : never
34
- onReset?: Schema extends FormSchema ? (data?: undefined extends Type ? Partial<z.infer<Schema>> : Type) => void : never
61
+ onReset?: Schema extends FormSchema ? (data?: undefined extends Type ? Partial<InferSchema<Schema>> : Type) => void : never
35
62
  onInvalid?: Schema extends FormSchema
36
- ? (error?: z.inferFormattedError<Schema>) => void
63
+ ? (error?: InferFormattedError<Schema>) => void
37
64
  : never
38
65
  onValid?: Schema extends FormSchema
39
- ? (data?: undefined extends Type ? Partial<z.infer<Schema>> : Type) => void
66
+ ? (data?: undefined extends Type ? Partial<InferSchema<Schema>> : Type) => void
40
67
  : never
41
68
  }
42
69
 
@@ -45,21 +72,26 @@ export type FormComposableOptions<Schema, Type> = FormFieldComponentOptions
45
72
  scope?: string
46
73
  }
47
74
 
48
- type FormPluginOptionsSchema<T = Partial<z.infer<FormSchema>>> = {
75
+ type FormPluginOptionsSchema<T = Partial<InferSchema<FormSchema>>> = {
49
76
  schema?: FormSchema
50
- factory?: (data?: Partial<z.infer<FormSchema>>) => T
77
+ factory?: (data?: Partial<InferSchema<FormSchema>>) => T
51
78
  }
52
79
 
53
80
  export type FormPluginOptions = FormPluginOptionsSchema
54
81
  & FormComposableOptions<FormPluginOptionsSchema['schema'], FormPluginOptionsSchema['factory']>
55
82
 
56
83
  export type InjectedFormData<Schema extends FormSchema, Type> = {
57
- formData: Ref<(undefined extends Type ? Partial<z.infer<Schema>> : Type) | undefined>
84
+ formData: Ref<(undefined extends Type ? Partial<InferSchema<Schema>> : Type) | undefined>
58
85
  errors: Readonly<
59
- Ref<DeepReadonly<z.inferFormattedError<Schema>> | undefined>
86
+ Ref<DeepReadonly<InferFormattedError<Schema>> | undefined>
60
87
  >
61
88
  submit: () => Promise<boolean>
62
- validate: (formData?: undefined extends Type ? Partial<z.infer<Schema>> : Type, options?: { fields?: Set<Path<z.infer<Schema>>>, superRefine?: (arg: z.infer<Schema>, ctx: RefinementCtx) => void | Promise<void> }) => Promise<boolean>
89
+ validate: (formData?: undefined extends Type
90
+ ? Partial<InferSchema<Schema>>
91
+ : Type, options?: {
92
+ fields?: Set<Path<InferSchema<Schema>>>
93
+ superRefine?: (arg: InferSchema<Schema>, ctx: RefinementCtx<Schema>) => void | Promise<void>
94
+ }) => Promise<boolean>
63
95
  clear: () => void
64
96
  reset: () => void
65
97
  ignoreUpdates: IgnoredUpdater
@@ -72,20 +104,20 @@ export type InjectedFormData<Schema extends FormSchema, Type> = {
72
104
 
73
105
  export type InjectedFormWrapperData<Schema extends FormSchema> = {
74
106
  name: Readonly<Ref<string>>
75
- errors: Ref<Map<string, z.inferFormattedError<Schema>>>
107
+ errors: Ref<Map<string, InferFormattedError<Schema>>>
76
108
  invalid: Readonly<Ref<boolean>>
77
109
  readonly: Readonly<Ref<boolean>>
78
110
  fields: Ref<Map<string, string>>
79
111
  }
80
112
 
81
113
  export type InjectedFormFieldData<Schema extends FormSchema> = {
82
- name: Readonly<Ref<Path<z.infer<Schema>>>>
83
- errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
114
+ name: Readonly<Ref<Path<InferSchema<Schema>>>>
115
+ errors: Readonly<Ref<DeepReadonly<InferFormattedError<Schema>>>>
84
116
  }
85
117
 
86
118
  export type InjectedFormFieldsGroupData<Schema extends FormSchema> = {
87
- names: DeepReadonly<Ref<Path<z.infer<Schema>>[]>>
88
- errors: Readonly<Ref<DeepReadonly<Record<string, z.inferFormattedError<Schema>> | undefined>>>
119
+ names: DeepReadonly<Ref<Path<InferSchema<Schema>>[]>>
120
+ errors: Readonly<Ref<DeepReadonly<Record<string, InferFormattedError<Schema>> | undefined>>>
89
121
  }
90
122
 
91
123
  export type Primitive
@@ -150,7 +182,7 @@ export type SimpleFormTemplateItem<Schema extends FormSchema, Type> = Record<
150
182
  any
151
183
  > & {
152
184
  vvIs?: string | Component
153
- vvName?: Path<z.infer<Schema>>
185
+ vvName?: Path<InferSchema<Schema>>
154
186
  vvSlots?: Record<string, any>
155
187
  vvChildren?:
156
188
  | Array<
@@ -170,8 +202,8 @@ export type SimpleFormTemplateItem<Schema extends FormSchema, Type> = Record<
170
202
  scope?: Record<string, unknown>,
171
203
  ) => SimpleFormTemplateItem<Schema, Type>)
172
204
  >)
173
- vvIf?: AnyBoolean<Schema, Type> | Path<z.infer<Schema>>
174
- vvElseIf?: AnyBoolean<Schema, Type> | Path<z.infer<Schema>>
205
+ vvIf?: AnyBoolean<Schema, Type> | Path<InferSchema<Schema>>
206
+ vvElseIf?: AnyBoolean<Schema, Type> | Path<InferSchema<Schema>>
175
207
  vvType?: `${FormFieldType}`
176
208
  vvShowValid?: boolean
177
209
  vvContent?: string