zod-form-data
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -59,2 +59,3 @@ import { z, ZodArray, ZodEffects, ZodNumber, ZodObject, ZodString, ZodTypeAny } from "zod"; | ||
}; | ||
export declare const json: <T extends z.ZodTypeAny>(schema: T) => z.ZodEffects<T, T["_output"], T["_input"]>; | ||
/** | ||
@@ -61,0 +62,0 @@ * This helper takes the place of the `z.object` at the root of your schema. |
@@ -76,2 +76,11 @@ import { z, ZodType, } from "zod"; | ||
const entries = z.array(z.tuple([z.string(), z.any()])); | ||
const safeParseJson = (jsonString) => { | ||
try { | ||
return JSON.parse(jsonString); | ||
} | ||
catch { | ||
return jsonString; | ||
} | ||
}; | ||
export const json = (schema) => z.preprocess(preprocessIfValid(z.union([stripEmpty, z.string().transform((val) => safeParseJson(val))])), schema); | ||
/** | ||
@@ -78,0 +87,0 @@ * This helper takes the place of the `z.object` at the root of your schema. |
@@ -59,2 +59,3 @@ import { z, ZodArray, ZodEffects, ZodNumber, ZodObject, ZodString, ZodTypeAny } from "zod"; | ||
}; | ||
export declare const json: <T extends z.ZodTypeAny>(schema: T) => z.ZodEffects<T, T["_output"], T["_input"]>; | ||
/** | ||
@@ -61,0 +62,0 @@ * This helper takes the place of the `z.object` at the root of your schema. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formData = exports.repeatableOfType = exports.repeatable = exports.file = exports.checkbox = exports.numeric = exports.text = void 0; | ||
exports.formData = exports.json = exports.repeatableOfType = exports.repeatable = exports.file = exports.checkbox = exports.numeric = exports.text = void 0; | ||
const zod_1 = require("zod"); | ||
@@ -85,2 +85,12 @@ const stripEmpty = zod_1.z.literal("").transform(() => undefined); | ||
const entries = zod_1.z.array(zod_1.z.tuple([zod_1.z.string(), zod_1.z.any()])); | ||
const safeParseJson = (jsonString) => { | ||
try { | ||
return JSON.parse(jsonString); | ||
} | ||
catch { | ||
return jsonString; | ||
} | ||
}; | ||
const json = (schema) => zod_1.z.preprocess(preprocessIfValid(zod_1.z.union([stripEmpty, zod_1.z.string().transform((val) => safeParseJson(val))])), schema); | ||
exports.json = json; | ||
/** | ||
@@ -87,0 +97,0 @@ * This helper takes the place of the `z.object` at the root of your schema. |
{ | ||
"name": "zod-form-data", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"browser": "./browser/index.js", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
@@ -121,2 +121,18 @@ import { | ||
const safeParseJson = (jsonString: string) => { | ||
try { | ||
return JSON.parse(jsonString); | ||
} catch { | ||
return jsonString; | ||
} | ||
}; | ||
export const json = <T extends ZodTypeAny>(schema: T): ZodEffects<T> => | ||
z.preprocess( | ||
preprocessIfValid( | ||
z.union([stripEmpty, z.string().transform((val) => safeParseJson(val))]) | ||
), | ||
schema | ||
); | ||
/** | ||
@@ -123,0 +139,0 @@ * This helper takes the place of the `z.object` at the root of your schema. |
Sorry, the diff of this file is not supported yet
31686
599