{"version":3,"file":"DMuLLlug.js","sources":["../../../../utils/validators/validators.ts"],"sourcesContent":["import * as Yup from 'yup';\n\nconst ONE_MEGABYTE = 1048576;\n\nexport function isValidType(file: File): boolean {\n return ['image/jpg', 'image/jpeg', 'image/png'].includes(file.type);\n}\n\nexport function isValidSize(file: File): boolean {\n if (file.size > ONE_MEGABYTE) {\n return false;\n }\n return true;\n}\n\nexport const isValidFileName = (fileName: string) => {\n const invalidChars = /[<>:\"/\\\\|?*% ]/;\n return !invalidChars.test(fileName);\n};\n\nexport const isZipFile = (file: File): boolean => {\n return ['application/zip', 'application/x-zip-compressed'].includes(\n file.type\n );\n};\n\nexport const allowedFileTypes = [\n 'application/zip',\n 'application/x-zip-compressed',\n 'image/jpeg',\n 'image/jpg',\n 'image/png',\n 'image/gif',\n 'image/webp',\n 'image/svg+xml',\n 'video/mp4',\n 'video/webm',\n 'video/ogg',\n 'video/quicktime',\n 'application/pdf',\n 'text/plain'\n];\n\nexport const isValidFileType = (file: File): boolean => {\n return allowedFileTypes.includes(file.type);\n};\n\nexport function isValidDimension(file: File): Promise {\n return new Promise((resolve, reject) => {\n const img = new Image();\n img.onload = () => {\n if (img.width >= 160 && img.height >= 160) {\n resolve(true);\n } else {\n resolve(false);\n }\n };\n img.onerror = () => {\n reject(new Error('Failed to load the image. Please try again.'));\n };\n img.src = URL.createObjectURL(file);\n });\n}\n\nexport function isImageTransparent(file: File): Promise {\n return new Promise((resolve, reject) => {\n const img = new Image();\n img.onload = () => {\n const canvas: HTMLCanvasElement = document.createElement('canvas');\n canvas.width = img.width;\n canvas.height = img.height;\n const ctx: CanvasRenderingContext2D | null = canvas.getContext('2d');\n if (ctx) {\n ctx.drawImage(img, 0, 0);\n const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);\n const data = imageData.data;\n\n // Check for pixels with alpha value less than 255\n for (let i = 0; i < data.length; i += 4) {\n if (data[i + 3] < 255) {\n resolve(true); // Transparent pixel found\n return;\n }\n }\n }\n resolve(false); // No transparent pixels found\n };\n img.onerror = reject;\n img.src = URL.createObjectURL(file);\n });\n}\n\ninterface ValidationResult {\n isValid: boolean;\n formErrors: { [key: string]: string };\n}\n\nexport const validateForm = async (\n formData: any,\n schema: Yup.ObjectSchema\n): Promise => {\n try {\n await schema.validate(formData, { abortEarly: false });\n\n return {\n isValid: true,\n formErrors: {}\n };\n } catch (error: Error | any) {\n const formErrors: { [key: string]: string } = error.inner.reduce(\n (acc: any, e: any) => {\n acc[e.path] = e.message;\n return acc;\n },\n {}\n );\n return {\n isValid: false,\n formErrors\n };\n }\n};\n\ninterface ValidationRule {\n validate: (file: File) => Promise | boolean;\n message: string;\n}\n\nexport const validateFile = async (\n file: File,\n validationRules: ValidationRule[]\n) => {\n for (const rule of validationRules) {\n try {\n const result = await rule.validate(file);\n if (!result) {\n return { isValid: false, message: rule.message };\n }\n } catch (err: any) {\n return {\n isValid: false,\n message: err.message || 'Error during file validation.'\n };\n }\n }\n return { isValid: true, message: '' };\n};\n"],"names":["ONE_MEGABYTE","isValidType","file","isValidSize","isValidFileName","fileName","allowedFileTypes","isValidFileType","isValidDimension","resolve","reject","img","validateForm","formData","schema","error","acc","e"],"mappings":"+UAEA,MAAMA,EAAe,QAEd,SAASC,EAAYC,EAAqB,CAC/C,MAAO,CAAC,YAAa,aAAc,WAAW,EAAE,SAASA,EAAK,IAAI,CACpE,CAEO,SAASC,EAAYD,EAAqB,CAC3C,MAAA,EAAAA,EAAK,KAAOF,EAIlB,CAEa,MAAAI,EAAmBC,GAEvB,CADc,iBACA,KAAKA,CAAQ,EASvBC,EAAmB,CAC9B,kBACA,+BACA,aACA,YACA,YACA,YACA,aACA,gBACA,YACA,aACA,YACA,kBACA,kBACA,YACF,EAEaC,EAAmBL,GACvBI,EAAiB,SAASJ,EAAK,IAAI,EAGrC,SAASM,EAAiBN,EAA8B,CAC7D,OAAO,IAAI,QAAQ,CAACO,EAASC,IAAW,CAChC,MAAAC,EAAM,IAAI,MAChBA,EAAI,OAAS,IAAM,CACbA,EAAI,OAAS,KAAOA,EAAI,QAAU,IACpCF,EAAQ,EAAI,EAEZA,EAAQ,EAAK,CAEjB,EACAE,EAAI,QAAU,IAAM,CACXD,EAAA,IAAI,MAAM,6CAA6C,CAAC,CACjE,EACIC,EAAA,IAAM,IAAI,gBAAgBT,CAAI,CAAA,CACnC,CACH,CAmCa,MAAAU,EAAe,MAC1BC,EACAC,IAC8B,CAC1B,GAAA,CACF,aAAMA,EAAO,SAASD,EAAU,CAAE,WAAY,GAAO,EAE9C,CACL,QAAS,GACT,WAAY,CAAA,CACd,QACOE,EAAoB,CAQpB,MAAA,CACL,QAAS,GACT,WAT4CA,EAAM,MAAM,OACxD,CAACC,EAAUC,KACLD,EAAAC,EAAE,IAAI,EAAIA,EAAE,QACTD,GAET,CAAA,CACF,CAIA,CAAA,CAEJ"}