|
|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import type { ZodSchema, ZodString } from 'zod'
|
|
|
|
|
import { z } from 'zod'
|
|
|
|
|
import { type BaseConfiguration, BaseVarType } from './types'
|
|
|
|
|
import { type BaseConfiguration, BaseFieldType } from './types'
|
|
|
|
|
|
|
|
|
|
export const generateZodSchema = <T>(fields: BaseConfiguration<T>[]) => {
|
|
|
|
|
const shape: Record<string, ZodSchema> = {}
|
|
|
|
|
@ -9,16 +9,16 @@ export const generateZodSchema = <T>(fields: BaseConfiguration<T>[]) => {
|
|
|
|
|
let zodType
|
|
|
|
|
|
|
|
|
|
switch (field.type) {
|
|
|
|
|
case BaseVarType.textInput:
|
|
|
|
|
case BaseFieldType.textInput:
|
|
|
|
|
zodType = z.string()
|
|
|
|
|
break
|
|
|
|
|
case BaseVarType.numberInput:
|
|
|
|
|
case BaseFieldType.numberInput:
|
|
|
|
|
zodType = z.number()
|
|
|
|
|
break
|
|
|
|
|
case BaseVarType.checkbox:
|
|
|
|
|
case BaseFieldType.checkbox:
|
|
|
|
|
zodType = z.boolean()
|
|
|
|
|
break
|
|
|
|
|
case BaseVarType.select:
|
|
|
|
|
case BaseFieldType.select:
|
|
|
|
|
zodType = z.string()
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
@ -27,7 +27,7 @@ export const generateZodSchema = <T>(fields: BaseConfiguration<T>[]) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (field.required) {
|
|
|
|
|
if ([BaseVarType.textInput].includes(field.type))
|
|
|
|
|
if ([BaseFieldType.textInput].includes(field.type))
|
|
|
|
|
zodType = (zodType as ZodString).nonempty(`${field.label} is required`)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
@ -35,17 +35,17 @@ export const generateZodSchema = <T>(fields: BaseConfiguration<T>[]) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (field.maxLength) {
|
|
|
|
|
if ([BaseVarType.textInput].includes(field.type))
|
|
|
|
|
if ([BaseFieldType.textInput].includes(field.type))
|
|
|
|
|
zodType = (zodType as ZodString).max(field.maxLength, `${field.label} exceeds max length of ${field.maxLength}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (field.min) {
|
|
|
|
|
if ([BaseVarType.numberInput].includes(field.type))
|
|
|
|
|
if ([BaseFieldType.numberInput].includes(field.type))
|
|
|
|
|
zodType = (zodType as ZodString).min(field.min, `${field.label} must be at least ${field.min}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (field.max) {
|
|
|
|
|
if ([BaseVarType.numberInput].includes(field.type))
|
|
|
|
|
if ([BaseFieldType.numberInput].includes(field.type))
|
|
|
|
|
zodType = (zodType as ZodString).max(field.max, `${field.label} exceeds max value of ${field.max}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|