mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-07 20:37:44 +00:00
chore: ad release label
This commit is contained in:
@@ -1,81 +1,95 @@
|
||||
<script setup lang="ts" generic="T extends ZodObjectOrWrapped">
|
||||
import { computed, toRefs } from 'vue'
|
||||
import type { ZodAny, z } from 'zod'
|
||||
import { toTypedSchema } from '@vee-validate/zod'
|
||||
import type { FormContext, GenericObject } from 'vee-validate'
|
||||
import { type ZodObjectOrWrapped, getBaseSchema, getBaseType, getDefaultValueInZodStack, getObjectFormSchema } from './utils'
|
||||
import type { Config, ConfigItem, Dependency, Shape } from './interface'
|
||||
import AutoFormField from './AutoFormField.vue'
|
||||
import { provideDependencies } from './dependencies'
|
||||
import { Form } from '@/components/ui/form'
|
||||
import { computed, toRefs } from "vue";
|
||||
import type { ZodAny, z } from "zod";
|
||||
import { toTypedSchema } from "@vee-validate/zod";
|
||||
import type { FormContext, GenericObject } from "vee-validate";
|
||||
import {
|
||||
type ZodObjectOrWrapped,
|
||||
getBaseSchema,
|
||||
getBaseType,
|
||||
getDefaultValueInZodStack,
|
||||
getObjectFormSchema,
|
||||
} from "./utils";
|
||||
import type { Config, ConfigItem, Dependency, Shape } from "./interface";
|
||||
import AutoFormField from "./AutoFormField.vue";
|
||||
import { provideDependencies } from "./dependencies";
|
||||
import { Form } from "@/components/ui/form";
|
||||
|
||||
const props = defineProps<{
|
||||
schema: T
|
||||
form?: FormContext<GenericObject>
|
||||
fieldConfig?: Config<z.infer<T>>
|
||||
dependencies?: Dependency<z.infer<T>>[]
|
||||
}>()
|
||||
schema: T;
|
||||
form?: FormContext<GenericObject>;
|
||||
fieldConfig?: Config<z.infer<T>>;
|
||||
dependencies?: Dependency<z.infer<T>>[];
|
||||
}>();
|
||||
|
||||
const emits = defineEmits<{
|
||||
submit: [event: z.infer<T>]
|
||||
}>()
|
||||
submit: [event: z.infer<T>];
|
||||
}>();
|
||||
|
||||
const { dependencies } = toRefs(props)
|
||||
provideDependencies(dependencies)
|
||||
const { dependencies } = toRefs(props);
|
||||
provideDependencies(dependencies);
|
||||
|
||||
const shapes = computed(() => {
|
||||
// @ts-expect-error ignore {} not assignable to object
|
||||
const val: { [key in keyof T]: Shape } = {}
|
||||
const baseSchema = getObjectFormSchema(props.schema)
|
||||
const shape = baseSchema.shape
|
||||
Object.keys(shape).forEach((name) => {
|
||||
const item = shape[name] as ZodAny
|
||||
const baseItem = getBaseSchema(item) as ZodAny
|
||||
let options = (baseItem && 'values' in baseItem._def) ? baseItem._def.values as string[] : undefined
|
||||
if (!Array.isArray(options) && typeof options === 'object')
|
||||
options = Object.values(options)
|
||||
// @ts-expect-error ignore {} not assignable to object
|
||||
const val: { [key in keyof T]: Shape } = {};
|
||||
const baseSchema = getObjectFormSchema(props.schema);
|
||||
const shape = baseSchema.shape;
|
||||
Object.keys(shape).forEach((name) => {
|
||||
const item = shape[name] as ZodAny;
|
||||
const baseItem = getBaseSchema(item) as ZodAny;
|
||||
let options =
|
||||
baseItem && "values" in baseItem._def
|
||||
? (baseItem._def.values as string[])
|
||||
: undefined;
|
||||
if (!Array.isArray(options) && typeof options === "object")
|
||||
options = Object.values(options);
|
||||
|
||||
val[name as keyof T] = {
|
||||
type: getBaseType(item),
|
||||
default: getDefaultValueInZodStack(item),
|
||||
options,
|
||||
required: !['ZodOptional', 'ZodNullable'].includes(item._def.typeName),
|
||||
schema: baseItem,
|
||||
}
|
||||
})
|
||||
return val
|
||||
})
|
||||
val[name as keyof T] = {
|
||||
type: getBaseType(item),
|
||||
default: getDefaultValueInZodStack(item),
|
||||
options,
|
||||
required: !["ZodOptional", "ZodNullable"].includes(item._def.typeName),
|
||||
schema: baseItem,
|
||||
};
|
||||
});
|
||||
return val;
|
||||
});
|
||||
|
||||
const fields = computed(() => {
|
||||
// @ts-expect-error ignore {} not assignable to object
|
||||
const val: { [key in keyof z.infer<T>]: { shape: Shape, fieldName: string, config: ConfigItem } } = {}
|
||||
for (const key in shapes.value) {
|
||||
const shape = shapes.value[key]
|
||||
val[key as keyof z.infer<T>] = {
|
||||
shape,
|
||||
config: props.fieldConfig?.[key] as ConfigItem,
|
||||
fieldName: key,
|
||||
}
|
||||
}
|
||||
return val
|
||||
})
|
||||
// @ts-expect-error ignore {} not assignable to object
|
||||
const val: {
|
||||
[key in keyof z.infer<T>]: {
|
||||
shape: Shape;
|
||||
fieldName: string;
|
||||
config: ConfigItem;
|
||||
};
|
||||
} = {};
|
||||
for (const key in shapes.value) {
|
||||
const shape = shapes.value[key];
|
||||
val[key as keyof z.infer<T>] = {
|
||||
shape,
|
||||
config: props.fieldConfig?.[key] as ConfigItem,
|
||||
fieldName: key,
|
||||
};
|
||||
}
|
||||
return val;
|
||||
});
|
||||
|
||||
const formComponent = computed(() => props.form ? 'form' : Form)
|
||||
const formComponent = computed(() => (props.form ? "form" : Form));
|
||||
const formComponentProps = computed(() => {
|
||||
if (props.form) {
|
||||
return {
|
||||
onSubmit: props.form.handleSubmit(val => emits('submit', val)),
|
||||
}
|
||||
}
|
||||
else {
|
||||
const formSchema = toTypedSchema(props.schema)
|
||||
return {
|
||||
keepValues: true,
|
||||
validationSchema: formSchema,
|
||||
onSubmit: (val: GenericObject) => emits('submit', val),
|
||||
}
|
||||
}
|
||||
})
|
||||
if (props.form) {
|
||||
return {
|
||||
onSubmit: props.form.handleSubmit((val) => emits("submit", val)),
|
||||
};
|
||||
} else {
|
||||
const formSchema = toTypedSchema(props.schema);
|
||||
return {
|
||||
keepValues: true,
|
||||
validationSchema: formSchema,
|
||||
onSubmit: (val: GenericObject) => emits("submit", val),
|
||||
};
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in New Issue
Block a user