next-safe-action
Version:
Type safe and validated Server Actions in your Next.js project.
1 lines • 2.57 kB
Source Map (JSON)
{"version":3,"sources":["../../src/adapters/valibot.ts"],"sourcesContent":["// Code based on https://github.com/decs/typeschema/blob/main/packages/valibot/src/validation.ts\n\n// MIT License\n\n// Copyright (c) 2023 André Costa\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\nimport { getDotPath, safeParseAsync, type GenericSchema, type GenericSchemaAsync } from \"valibot\";\nimport type { IfInstalled, Infer, ValidationAdapter } from \"./types\";\n\nclass ValibotAdapter implements ValidationAdapter {\n\tasync validate<S extends IfInstalled<GenericSchema | GenericSchemaAsync>>(schema: S, data: unknown) {\n\t\tconst result = await safeParseAsync(schema, data);\n\n\t\tif (result.success) {\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: result.output as Infer<S>,\n\t\t\t} as const;\n\t\t}\n\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\tissues: result.issues.map((issue) => ({\n\t\t\t\tmessage: issue.message,\n\t\t\t\tpath: getDotPath(issue)?.split(\".\"),\n\t\t\t})),\n\t\t} as const;\n\t}\n}\n\nexport function valibotAdapter() {\n\treturn new ValibotAdapter();\n}\n"],"mappings":";AAwBA,SAAS,YAAY,sBAAmE;AAGxF,IAAM,iBAAN,MAAkD;AAAA,EACjD,MAAM,SAAoE,QAAW,MAAe;AACnG,UAAM,SAAS,MAAM,eAAe,QAAQ,IAAI;AAEhD,QAAI,OAAO,SAAS;AACnB,aAAO;AAAA,QACN,SAAS;AAAA,QACT,MAAM,OAAO;AAAA,MACd;AAAA,IACD;AAEA,WAAO;AAAA,MACN,SAAS;AAAA,MACT,QAAQ,OAAO,OAAO,IAAI,CAAC,WAAW;AAAA,QACrC,SAAS,MAAM;AAAA,QACf,MAAM,WAAW,KAAK,GAAG,MAAM,GAAG;AAAA,MACnC,EAAE;AAAA,IACH;AAAA,EACD;AACD;AAEO,SAAS,iBAAiB;AAChC,SAAO,IAAI,eAAe;AAC3B;","names":[]}