Validacion y restriccion de archivos + almacenamiento en carpeta
This commit is contained in:
@@ -7,31 +7,9 @@ export type EditInventory = z.infer<typeof editInventory>;
|
||||
export type ProductApiResponseSchema = z.infer<typeof productApiResponseSchema>;
|
||||
export type allProducts = z.infer<typeof productDetails>;
|
||||
|
||||
// --- Esquemas de validación para archivos ---
|
||||
|
||||
// Esquema básico para un solo archivo
|
||||
const fileSchema = z.object({
|
||||
name: z.string().min(1, 'El nombre del archivo no puede estar vacío.'),
|
||||
size: z.number().int().positive('El tamaño del archivo debe ser positivo.'),
|
||||
type: z.string().refine(
|
||||
(type) => type.startsWith('image/'),
|
||||
'Solo se permiten archivos de imagen.'
|
||||
),
|
||||
// Puedes añadir más validaciones personalizadas aquí
|
||||
// Por ejemplo, limitar el tamaño máximo a 5MB (5 * 1024 * 1024 bytes)
|
||||
}).refine(
|
||||
(file) => file.size <= 5 * 1024 * 1024,
|
||||
'El tamaño del archivo no debe exceder los 5MB.'
|
||||
);
|
||||
|
||||
// Esquema para un array de archivos (cuando el input es multiple)
|
||||
const filesArraySchema = z.array(fileSchema).max(5, 'Solo se permiten hasta 5 archivos.');
|
||||
|
||||
// const formSchema = z.object({
|
||||
// file: z
|
||||
// .instanceof(FileList)
|
||||
// .refine((file) => file?.length == 1, 'File is required.')
|
||||
// });
|
||||
const MAX_FILE_SIZE = 5242880; // 5MB en bytes
|
||||
const ACCEPTED_IMAGE_TYPES = ["image/jpeg", "image/jpg", "image/png", "image/webp"];
|
||||
const MAX_FILENAME_LENGTH = 50;
|
||||
|
||||
export const product = z.object({
|
||||
id: z.number().optional(),
|
||||
@@ -41,7 +19,7 @@ export const product = z.object({
|
||||
// category: z.string(),
|
||||
stock: z.number(),
|
||||
price: z.string(),
|
||||
urlImg: z.any(),
|
||||
urlImg: z.custom<FileList | undefined>().optional(),
|
||||
// urlImg: z.string(),
|
||||
status: z.string(),
|
||||
userId: z.number().optional()
|
||||
@@ -60,22 +38,23 @@ export const editInventory = z.object({
|
||||
stock: z.number(),
|
||||
address: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }),
|
||||
price: z.string(),
|
||||
urlImg: z.any(),
|
||||
urlImg: z.custom<FileList | undefined>()
|
||||
.refine((files) => files && files.length > 0, "Se requiere al menos una imagen")
|
||||
.refine((files) => files && files.length <= 10, "Máximo 10 imágenes")
|
||||
.refine((files) =>
|
||||
files && Array.from(files).every(file => file.size <= MAX_FILE_SIZE),
|
||||
`El tamaño máximo de cada imagen es de 5MB`
|
||||
).refine((files) =>
|
||||
files && Array.from(files).every(file => ACCEPTED_IMAGE_TYPES.includes(file.type)),
|
||||
"Solo se aceptan archivos .jpg, .jpeg, .png y .webp"
|
||||
).refine((files) =>
|
||||
files && Array.from(files).every(file => file.name.length <= MAX_FILENAME_LENGTH),
|
||||
`El nombre de cada archivo no puede superar los ${MAX_FILENAME_LENGTH} caracteres`
|
||||
),
|
||||
status: z.string().min(1, { message: "Debe de seleccionar un valor" }),
|
||||
userId: z.number().optional(),
|
||||
})
|
||||
|
||||
// export const productDetails = z.object({
|
||||
// id: z.number().optional(),
|
||||
// title: z.string().min(5),
|
||||
// description: z.string().min(10),
|
||||
// stock: z.number(),
|
||||
// price: z.string(),
|
||||
// address: z.string(),
|
||||
// urlImg: z.string(),
|
||||
// userId: z.number(),
|
||||
// })
|
||||
|
||||
export const ApiResponseSchema = z.object({
|
||||
message: z.string(),
|
||||
data: z.array(product),
|
||||
|
||||
Reference in New Issue
Block a user