TanStackT
TanStack10mo ago
5 replies
full-green

Validate FormData server action with input type="file"

Hello, I'm trying to validate input file on the server but the validation fails:

issues: [
    {
      kind: 'schema',
      type: 'instance',
      input: {},
      expected: 'File',
      received: 'Object',
      message: 'Veuillez entrer un fichier valide',
      requirement: undefined,
      path: [Array],
      issues: undefined,
      lang: undefined,
      abortEarly: undefined,
      abortPipeEarly: undefined
    }
  ]


export const AddCompanySchema = v.object({
    name: v.pipe(
        v.string(),
        v.nonEmpty("Veuillez entrer le nom de l'entreprise"),
        v.maxLength(255, "Le nom de l'entreprise doit contenir au plus 255 caractères"),
    ),
    siret: v.pipe(
        v.string(),
        v.nonEmpty("Veuillez entrer le siret de l'entreprise"),
        v.maxLength(14, "Le siret de l'entreprise doit contenir 14 caractères"),
    ),
    description: v.pipe(
        v.string(),
        v.maxLength(1500, "La description de l'entreprise doit contenir au plus 1500 caractères"),
    ),
    categories: v.pipe(
        v.array(v.string()),
        v.minLength(1, "Veuillez sélectionner au moins une catégorie"),
        v.maxLength(3, "Veuillez sélectionner au plus 3 catégories"),
    ),
    logo: v.pipe(
        v.instance(File, "Veuillez entrer un fichier valide"),
        v.mimeType(["image/png", "image/jpeg", "image/jpg"], "Veuillez entrer un fichier valide"),
        v.maxSize(1024 * 1024 * 3, "La taille du fichier doit être inférieure à 3MB"),
    ),
    images: v.optional(
        v.pipe(
            v.array(v.file("Veuillez entrer un fichier valide")),
            v.minLength(1, "Veuillez entrer au moins une image"),
            v.maxLength(3, "Veuillez entrer au plus 3 images"),
        ),
    ),
});


is it a normal behavior that server side its not a type File anymore ? client side validation does not trigger an error
Was this page helpful?