modernise file field type.

This commit is contained in:
2025-10-22 16:49:00 +02:00
parent 4d1c06c5be
commit dc0ba32801
5 changed files with 21 additions and 41 deletions

View File

@@ -89,15 +89,11 @@ export function stringifyContent(collections: CollectionModel[], opts: GenerateO
const fileFieldMaxSelect = maxSelectFile ? `.max(${maxSelectFile})` : ''
const fileFieldTypeArray = maxSelectFile === 1 ? '' : `.array()${fileFieldMaxSelect}`
let fileValidation = 'z.instanceof(File)'
const fileMaxSizeFile = maxSizeFile ? `.max(${maxSizeFile})` : ''
const fileMimeTypesArray =
mimeTypesArray.length > 0 ? `.mime(${JSON.stringify(mimeTypesArray)})` : ''
if (maxSizeFile)
fileValidation += `.refine((file) => file.size <= ${maxSizeFile}, { message: "File size too large" })`
if (mimeTypesArray.length > 0)
fileValidation += `.refine((file) => ${JSON.stringify(mimeTypesArray)}.includes(file.type), { message: "Invalid file type" })`
const baseFileSchema = `z.union([z.string(), ${fileValidation}])`
const baseFileSchema = `z.union([z.string(), z.file()${fileMaxSizeFile}${fileMimeTypesArray}])`
schema = `${baseFileSchema}${fileFieldTypeArray}`
break
}

View File

@@ -94,8 +94,8 @@ function fieldsFromRec<S>(schema: S, prefix = '') {
function hasObjectSchemaDescendant(value: unknown): value is any {
// Handle ZodPipe
if (value && typeof value === 'object' && 'constructor' in value) {
const constructor = (value as any).constructor
if (constructor?.name === 'ZodPipe') {
const valueConstructor = value.constructor
if (valueConstructor?.name === 'ZodPipe') {
const inputSchema = (value as any)._def?.in
if (inputSchema) return hasObjectSchemaDescendant(inputSchema)
}
@@ -103,8 +103,8 @@ function hasObjectSchemaDescendant(value: unknown): value is any {
// Handle ZodTransform
if (value && typeof value === 'object' && 'constructor' in value) {
const constructor = (value as any).constructor
if (constructor?.name === 'ZodTransform') {
const valueConstructor = value.constructor
if (valueConstructor?.name === 'ZodTransform') {
const innerSchema =
(value as any)._def?.input || (value as any)._def?.schema || (value as any).sourceType
if (innerSchema) return hasObjectSchemaDescendant(innerSchema)
@@ -123,8 +123,8 @@ function hasObjectSchemaDescendant(value: unknown): value is any {
function getObjectSchemaDescendant<S>(schema: S): ZodObject | undefined {
// Handle ZodPipe
if (schema && typeof schema === 'object' && 'constructor' in schema) {
const constructor = (schema as any).constructor
if (constructor?.name === 'ZodPipe') {
const schemaConstructor = schema.constructor
if (schemaConstructor?.name === 'ZodPipe') {
const inputSchema = (schema as any)._def?.in
if (inputSchema) return getObjectSchemaDescendant(inputSchema)
}
@@ -132,8 +132,8 @@ function getObjectSchemaDescendant<S>(schema: S): ZodObject | undefined {
// Handle ZodTransform
if (schema && typeof schema === 'object' && 'constructor' in schema) {
const constructor = (schema as any).constructor
if (constructor?.name === 'ZodTransform') {
const schemaConstructor = schema.constructor
if (schemaConstructor?.name === 'ZodTransform') {
const innerSchema =
(schema as any)._def?.input || (schema as any)._def?.schema || (schema as any).sourceType
if (innerSchema) return getObjectSchemaDescendant(innerSchema)