{"version":3,"file":"to-zod.mjs","names":[],"sources":["../../src/db/to-zod.ts"],"sourcesContent":["import type { DBFieldAttribute } from \"@better-auth/core/db\";\nimport type { ZodType } from \"zod\";\nimport * as z from \"zod\";\n\nexport function toZodSchema<\n\tFields extends Record<string, DBFieldAttribute | never>,\n\tIsClientSide extends boolean,\n>({\n\tfields,\n\tisClientSide,\n}: {\n\tfields: Fields;\n\t/**\n\t * If true, then any fields that have `input: false` will be removed from the schema to prevent user input.\n\t */\n\tisClientSide: IsClientSide;\n}) {\n\tconst zodFields = Object.keys(fields).reduce((acc, key) => {\n\t\tconst field = fields[key];\n\t\tif (!field) {\n\t\t\treturn acc;\n\t\t}\n\t\tif (isClientSide && field.input === false) {\n\t\t\treturn acc;\n\t\t}\n\n\t\tlet schema: ZodType;\n\t\tif (field.type === \"json\") {\n\t\t\tschema = (z as any).json ? (z as any).json() : z.any();\n\t\t} else if (field.type === \"string[]\" || field.type === \"number[]\") {\n\t\t\tschema = z.array(field.type === \"string[]\" ? z.string() : z.number());\n\t\t} else if (Array.isArray(field.type)) {\n\t\t\tschema = z.any();\n\t\t} else {\n\t\t\tschema = z[field.type]();\n\t\t}\n\n\t\tif (field?.required === false) {\n\t\t\tschema = schema.optional();\n\t\t}\n\t\tif (!isClientSide && field?.returned === false) {\n\t\t\treturn acc;\n\t\t}\n\t\treturn {\n\t\t\t...acc,\n\t\t\t[key]: schema,\n\t\t};\n\t}, {});\n\tconst schema = z.object(zodFields);\n\treturn schema as z.ZodObject<\n\t\tRemoveNeverProps<{\n\t\t\t[key in keyof Fields]: FieldAttributeToSchema<Fields[key], IsClientSide>;\n\t\t}>,\n\t\tz.core.$strip\n\t>;\n}\n\nexport type FieldAttributeToSchema<\n\tField extends DBFieldAttribute | Record<string, never>,\n\t// if it's client side, then field attributes of `input` that are false should be removed\n\tisClientSide extends boolean = false,\n> = Field extends { type: any }\n\t? GetInput<isClientSide, Field, GetRequired<Field, GetType<Field>>>\n\t: Record<string, never>;\n\ntype GetType<F extends DBFieldAttribute> = F extends {\n\ttype: \"string\";\n}\n\t? z.ZodString\n\t: F extends { type: \"number\" }\n\t\t? z.ZodNumber\n\t\t: F extends { type: \"boolean\" }\n\t\t\t? z.ZodBoolean\n\t\t\t: F extends { type: \"date\" }\n\t\t\t\t? z.ZodDate\n\t\t\t\t: z.ZodAny;\n\ntype GetRequired<\n\tF extends DBFieldAttribute,\n\tSchema extends z.core.SomeType,\n> = F extends {\n\trequired: true;\n}\n\t? Schema\n\t: z.ZodOptional<Schema>;\n\ntype GetInput<\n\tisClientSide extends boolean,\n\tField extends DBFieldAttribute,\n\tSchema extends z.core.SomeType,\n> = Field extends {\n\tinput: false;\n}\n\t? isClientSide extends true\n\t\t? never\n\t\t: Schema\n\t: Schema;\n\ntype RemoveNeverProps<T> = {\n\t[K in keyof T as [T[K]] extends [never] ? never : K]: T[K];\n};\n"],"mappings":";;;AAIA,SAAgB,YAGd,EACD,QACA,gBAOE;CACF,MAAM,YAAY,OAAO,KAAK,OAAO,CAAC,QAAQ,KAAK,QAAQ;EAC1D,MAAM,QAAQ,OAAO;AACrB,MAAI,CAAC,MACJ,QAAO;AAER,MAAI,gBAAgB,MAAM,UAAU,MACnC,QAAO;EAGR,IAAI;AACJ,MAAI,MAAM,SAAS,OAClB,UAAU,EAAU,OAAQ,EAAU,MAAM,GAAG,EAAE,KAAK;WAC5C,MAAM,SAAS,cAAc,MAAM,SAAS,WACtD,UAAS,EAAE,MAAM,MAAM,SAAS,aAAa,EAAE,QAAQ,GAAG,EAAE,QAAQ,CAAC;WAC3D,MAAM,QAAQ,MAAM,KAAK,CACnC,UAAS,EAAE,KAAK;MAEhB,UAAS,EAAE,MAAM,OAAO;AAGzB,MAAI,OAAO,aAAa,MACvB,UAAS,OAAO,UAAU;AAE3B,MAAI,CAAC,gBAAgB,OAAO,aAAa,MACxC,QAAO;AAER,SAAO;GACN,GAAG;IACF,MAAM;GACP;IACC,EAAE,CAAC;AAEN,QADe,EAAE,OAAO,UAAU"}