UNPKG

zod-refine

Version:

An adapter that allows using Zod schemas as Recoil Sync validators instead of Refine.

67 lines (63 loc) 2.17 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { getRefineCheckerForZodSchema: () => getRefineCheckerForZodSchema }); module.exports = __toCommonJS(src_exports); var import_zod = require("zod"); var import_refine2 = require("@recoiljs/refine"); // src/util.ts var import_refine = require("@recoiljs/refine"); function zodPathToRecoilPath(elements) { return elements.reduce((parent, element) => parent.extend(element.toString()), new import_refine.Path()); } function recoilPathToZodPath(path) { return path.parent ? recoilPathToZodPath(path.parent).concat(path.field) : [path.field]; } // src/index.ts function getRefineCheckerForZodSchema(schema) { return function ZodRefineChecker(value, path) { try { return { type: "success", value: schema.parse(value, { path: path && recoilPathToZodPath(path) }), warnings: [] }; } catch (e) { if (e instanceof import_zod.ZodError) { const [error] = e.errors; return { type: "failure", message: error.message, path: zodPathToRecoilPath(error.path) }; } return { type: "failure", message: String(e), path: path ?? new import_refine2.Path() }; } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getRefineCheckerForZodSchema });