@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 1.33 kB
Source Map (JSON)
{"version":3,"file":"zod-safe-parse.mjs","names":[],"sources":["../../../../../src/libs/collection/custom-fields/utils/zod-safe-parse.ts"],"sourcesContent":["import type z from \"zod\";\nimport tidyZodError from \"../../../../utils/errors/tidy-zod-errors.js\";\nimport { copy } from \"../../../i18n/index.js\";\nimport type { CustomFieldValidateResponse } from \"../types.js\";\n\n/**\n * Removes new lines, and \" →\"\n */\nexport const modifyMessage = (errorMessage: string): string => {\n\treturn errorMessage.replace(/\\n/g, \" \").trim().replaceAll(\" →\", \" →\");\n};\n\n/**\n * Parses zod errors for custom fields\n */\nconst zodSafeParse = (\n\tvalue: unknown,\n\tschema: z.ZodType,\n): CustomFieldValidateResponse => {\n\tconst response = schema.safeParse(value);\n\tif (response?.success) {\n\t\treturn {\n\t\t\tvalid: true,\n\t\t};\n\t}\n\n\tconst message = modifyMessage(tidyZodError(response.error));\n\n\treturn {\n\t\tvalid: false,\n\t\tmessage: copy.literal(message),\n\t};\n};\n\nexport default zodSafeParse;\n"],"mappings":"0GAQA,MAAa,EAAiB,GACtB,EAAa,QAAQ,MAAO,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,WAAW,OAAQ,IAAI,EAMjE,GACL,EACA,IACiC,CACjC,IAAM,EAAW,EAAO,UAAU,CAAK,EACvC,GAAI,GAAU,QACb,MAAO,CACN,MAAO,EACR,EAGD,IAAM,EAAU,EAAc,EAAa,EAAS,KAAK,CAAC,EAE1D,MAAO,CACN,MAAO,GACP,QAAS,EAAK,QAAQ,CAAO,CAC9B,CACD"}