{"version":3,"sources":["../../src/adapters/yup.ts"],"sourcesContent":["// Code based on https://github.com/decs/typeschema/blob/main/packages/yup/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 type { Schema as YupSchema } from \"yup\";\nimport { ValidationError } from \"yup\";\nimport type { IfInstalled, Infer, ValidationAdapter, ValidationIssue } from \"./types\";\n\nclass YupAdapter implements ValidationAdapter {\n\tasync validate<S extends IfInstalled<YupSchema>>(schema: S, data: unknown) {\n\t\ttry {\n\t\t\tconst result = await schema.validate(data, { strict: true });\n\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: result as Infer<S>,\n\t\t\t} as const;\n\t\t} catch (e) {\n\t\t\tif (e instanceof ValidationError) {\n\t\t\t\tconst { message, path } = e;\n\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tissues: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmessage,\n\t\t\t\t\t\t\tpath: path && path.length > 0 ? path.split(\".\") : undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t] as ValidationIssue[],\n\t\t\t\t} as const;\n\t\t\t}\n\n\t\t\tthrow e;\n\t\t}\n\t}\n}\n\nexport function yupAdapter() {\n\treturn new YupAdapter();\n}\n"],"mappings":";AAyBA,SAAS,uBAAuB;AAGhC,IAAM,aAAN,MAA8C;AAAA,EAC7C,MAAM,SAA2C,QAAW,MAAe;AAC1E,QAAI;AACH,YAAM,SAAS,MAAM,OAAO,SAAS,MAAM,EAAE,QAAQ,KAAK,CAAC;AAE3D,aAAO;AAAA,QACN,SAAS;AAAA,QACT,MAAM;AAAA,MACP;AAAA,IACD,SAAS,GAAG;AACX,UAAI,aAAa,iBAAiB;AACjC,cAAM,EAAE,SAAS,KAAK,IAAI;AAE1B,eAAO;AAAA,UACN,SAAS;AAAA,UACT,QAAQ;AAAA,YACP;AAAA,cACC;AAAA,cACA,MAAM,QAAQ,KAAK,SAAS,IAAI,KAAK,MAAM,GAAG,IAAI;AAAA,YACnD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAEA,YAAM;AAAA,IACP;AAAA,EACD;AACD;AAEO,SAAS,aAAa;AAC5B,SAAO,IAAI,WAAW;AACvB;","names":[]}