UNPKG

better-auth

Version:

The most comprehensive authentication library for TypeScript.

60 lines (54 loc) 1.28 kB
'use strict'; const z = require('zod/v4'); function _interopNamespaceCompat(e) { if (e && typeof e === 'object' && 'default' in e) return e; const n = Object.create(null); if (e) { for (const k in e) { n[k] = e[k]; } } n.default = e; return n; } const z__namespace = /*#__PURE__*/_interopNamespaceCompat(z); function toZodSchema({ fields, isClientSide }) { const zodFields = Object.keys(fields).reduce((acc, key) => { const field = fields[key]; if (!field) { return acc; } if (isClientSide && field.input === false) { return acc; } if (field.type === "string[]" || field.type === "number[]") { return { ...acc, [key]: z__namespace.array(field.type === "string[]" ? z__namespace.string() : z__namespace.number()) }; } if (Array.isArray(field.type)) { return { ...acc, [key]: z__namespace.any() }; } let schema2 = z__namespace[field.type](); if (field?.required === false) { schema2 = schema2.optional(); } if (field?.returned === false) { return acc; } return { ...acc, [key]: schema2 }; }, {}); const schema = z__namespace.object(zodFields); return schema; } exports.toZodSchema = toZodSchema;