zod-form-data
Version:
Validation helpers for [zod](https://github.com/colinhacks/zod) specifically for parsing `FormData` or `URLSearchParams`. This is particularly useful when using [Remix](https://github.com/remix-run/remix) and combos well with [remix-validated-form](https:
151 lines (149 loc) • 4.87 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
checkbox: () => checkbox,
file: () => file,
formData: () => formData,
json: () => json,
numeric: () => numeric,
preprocessFormData: () => preprocessFormData,
repeatable: () => repeatable,
repeatableOfType: () => repeatableOfType,
text: () => text,
zfd: () => helpers_exports
});
module.exports = __toCommonJS(src_exports);
// src/helpers.ts
var helpers_exports = {};
__export(helpers_exports, {
checkbox: () => checkbox,
file: () => file,
formData: () => formData,
json: () => json,
numeric: () => numeric,
preprocessFormData: () => preprocessFormData,
repeatable: () => repeatable,
repeatableOfType: () => repeatableOfType,
text: () => text
});
var import_set_get = require("@rvf/set-get");
var z = __toESM(require("zod/v4"));
var import_v4 = require("zod/v4");
var stripEmpty = z.literal("").transform(() => void 0);
var preprocessIfValid = (schema) => (val) => {
const result = schema.safeParse(val);
if (result.success)
return result.data;
return val;
};
var text = (schema = z.string()) => z.preprocess(preprocessIfValid(stripEmpty), schema);
var numeric = (schema = z.number()) => z.preprocess(
preprocessIfValid(
z.union([
stripEmpty,
z.string().transform((val) => Number(val)).refine((val) => !Number.isNaN(val))
])
),
schema
);
var checkbox = ({ trueValue = "on" } = {}) => z.union([
z.literal(trueValue).transform(() => true),
z.literal(void 0).transform(() => false)
]);
var file = (schema = z.instanceof(File)) => z.preprocess((val) => {
return val instanceof File && val.size === 0 ? void 0 : val;
}, schema);
var repeatable = (schema = z.array(text())) => {
return z.preprocess((val) => {
if (Array.isArray(val))
return val;
if (val === void 0)
return [];
return [val];
}, schema);
};
var repeatableOfType = (schema) => repeatable(z.array(schema));
var entries = z.array(z.tuple([z.string(), z.any()]));
var safeParseJson = (jsonString) => {
try {
return JSON.parse(jsonString);
} catch {
return jsonString;
}
};
var json = (schema) => z.preprocess(
preprocessIfValid(
z.union([stripEmpty, z.string().transform((val) => safeParseJson(val))])
),
schema
);
var processFormData = preprocessIfValid(
// We're avoiding using `instanceof` here because different environments
// won't necessarily have `FormData` or `URLSearchParams`
z.any().refine((val) => Symbol.iterator in val, { abort: true }).transform((val) => [...val]).refine(
(val) => entries.safeParse(val).success,
{ abort: true }
).transform((data) => {
const map = /* @__PURE__ */ new Map();
for (const [key, value] of data) {
if (map.has(key)) {
map.get(key).push(value);
} else {
map.set(key, [value]);
}
}
return [...map.entries()].reduce(
(acc, [key, value]) => {
return (0, import_set_get.setPath)(acc, key, value.length === 1 ? value[0] : value);
},
{}
);
})
);
var preprocessFormData = processFormData;
var formData = (shapeOrSchema) => z.preprocess(
processFormData,
shapeOrSchema instanceof import_v4.ZodType ? shapeOrSchema : z.object(shapeOrSchema)
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
checkbox,
file,
formData,
json,
numeric,
preprocessFormData,
repeatable,
repeatableOfType,
text,
zfd
});
//# sourceMappingURL=index.js.map
;