zod-form-data
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -6,3 +6,16 @@ import { z, ZodArray, ZodEffects, ZodNumber, ZodObject, ZodString, ZodTypeAny } from "zod"; | ||
}; | ||
/** | ||
* Transforms any empty strings to `undefined` before validating. | ||
* This makes it so empty strings will fail required checks, | ||
* allowing you to use `optional` for optional fields instead of `nonempty` for required fields. | ||
* If you call `zfd.text` with no arguments, it will assume the field is a required string by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
export declare const text: InputType<ZodString>; | ||
/** | ||
* Coerces numerical strings to numbers transforms empty strings to `undefined` before validating. | ||
* If you call `zfd.number` with no arguments, | ||
* it will assume the field is a required number by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
export declare const numeric: InputType<ZodNumber>; | ||
@@ -12,5 +25,32 @@ declare type CheckboxOpts = { | ||
}; | ||
/** | ||
* Turns the value from a checkbox field into a boolean, | ||
* but does not require the checkbox to be checked. | ||
* For checkboxes with a `value` attribute, you can pass that as the `trueValue` option. | ||
* | ||
* @example | ||
* ```ts | ||
* const schema = zfd.formData({ | ||
* defaultCheckbox: zfd.checkbox(), | ||
* checkboxWithValue: zfd.checkbox({ trueValue: "true" }), | ||
* mustBeTrue: zfd | ||
* .checkbox() | ||
* .refine((val) => val, "Please check this box"), | ||
* }); | ||
* }); | ||
* ``` | ||
*/ | ||
export declare const checkbox: ({ trueValue }?: CheckboxOpts) => z.ZodUnion<[z.ZodEffects<z.ZodLiteral<string>, boolean, string>, z.ZodEffects<z.ZodLiteral<undefined>, boolean, undefined>]>; | ||
export declare const file: InputType<z.ZodType<File>>; | ||
/** | ||
* Preprocesses a field where you expect multiple values could be present for the same field name | ||
* and transforms the value of that field to always be an array. | ||
* If you don't provide a schema, it will assume the field is an array of zfd.text fields | ||
* and will not require any values to be present. | ||
*/ | ||
export declare const repeatable: InputType<ZodArray<any>>; | ||
/** | ||
* A convenience wrapper for repeatable. | ||
* Instead of passing the schema for an entire array, you pass in the schema for the item type. | ||
*/ | ||
export declare const repeatableOfType: <T extends z.ZodTypeAny>(schema: T) => z.ZodEffects<z.ZodArray<T, "many">, T["_output"][], T["_input"][]>; | ||
@@ -21,3 +61,10 @@ declare type FormDataType = { | ||
}; | ||
/** | ||
* This helper takes the place of the `z.object` at the root of your schema. | ||
* It wraps your schema in a `z.preprocess` that extracts all the data out of a `FormData` | ||
* and transforms it into a regular object. | ||
* If the `FormData` contains multiple entries with the same field name, | ||
* it will automatically turn that field into an array. | ||
*/ | ||
export declare const formData: FormDataType; | ||
export {}; |
@@ -9,3 +9,16 @@ import { z, ZodType, } from "zod"; | ||
}; | ||
/** | ||
* Transforms any empty strings to `undefined` before validating. | ||
* This makes it so empty strings will fail required checks, | ||
* allowing you to use `optional` for optional fields instead of `nonempty` for required fields. | ||
* If you call `zfd.text` with no arguments, it will assume the field is a required string by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
export const text = (schema = z.string()) => z.preprocess(preprocessIfValid(stripEmpty), schema); | ||
/** | ||
* Coerces numerical strings to numbers transforms empty strings to `undefined` before validating. | ||
* If you call `zfd.number` with no arguments, | ||
* it will assume the field is a required number by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
export const numeric = (schema = z.number()) => z.preprocess(preprocessIfValid(z.union([ | ||
@@ -18,2 +31,19 @@ stripEmpty, | ||
])), schema); | ||
/** | ||
* Turns the value from a checkbox field into a boolean, | ||
* but does not require the checkbox to be checked. | ||
* For checkboxes with a `value` attribute, you can pass that as the `trueValue` option. | ||
* | ||
* @example | ||
* ```ts | ||
* const schema = zfd.formData({ | ||
* defaultCheckbox: zfd.checkbox(), | ||
* checkboxWithValue: zfd.checkbox({ trueValue: "true" }), | ||
* mustBeTrue: zfd | ||
* .checkbox() | ||
* .refine((val) => val, "Please check this box"), | ||
* }); | ||
* }); | ||
* ``` | ||
*/ | ||
export const checkbox = ({ trueValue = "on" } = {}) => z.union([ | ||
@@ -27,2 +57,8 @@ z.literal(trueValue).transform(() => true), | ||
}, schema); | ||
/** | ||
* Preprocesses a field where you expect multiple values could be present for the same field name | ||
* and transforms the value of that field to always be an array. | ||
* If you don't provide a schema, it will assume the field is an array of zfd.text fields | ||
* and will not require any values to be present. | ||
*/ | ||
export const repeatable = (schema = z.array(text())) => { | ||
@@ -37,4 +73,15 @@ return z.preprocess((val) => { | ||
}; | ||
/** | ||
* A convenience wrapper for repeatable. | ||
* Instead of passing the schema for an entire array, you pass in the schema for the item type. | ||
*/ | ||
export const repeatableOfType = (schema) => repeatable(z.array(schema)); | ||
const entries = z.array(z.tuple([z.string(), z.any()])); | ||
/** | ||
* This helper takes the place of the `z.object` at the root of your schema. | ||
* It wraps your schema in a `z.preprocess` that extracts all the data out of a `FormData` | ||
* and transforms it into a regular object. | ||
* If the `FormData` contains multiple entries with the same field name, | ||
* it will automatically turn that field into an array. | ||
*/ | ||
export const formData = (shapeOrSchema) => z.preprocess(preprocessIfValid( | ||
@@ -41,0 +88,0 @@ // We're avoiding using `instanceof` here because different environments |
@@ -6,3 +6,16 @@ import { z, ZodArray, ZodEffects, ZodNumber, ZodObject, ZodString, ZodTypeAny } from "zod"; | ||
}; | ||
/** | ||
* Transforms any empty strings to `undefined` before validating. | ||
* This makes it so empty strings will fail required checks, | ||
* allowing you to use `optional` for optional fields instead of `nonempty` for required fields. | ||
* If you call `zfd.text` with no arguments, it will assume the field is a required string by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
export declare const text: InputType<ZodString>; | ||
/** | ||
* Coerces numerical strings to numbers transforms empty strings to `undefined` before validating. | ||
* If you call `zfd.number` with no arguments, | ||
* it will assume the field is a required number by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
export declare const numeric: InputType<ZodNumber>; | ||
@@ -12,5 +25,32 @@ declare type CheckboxOpts = { | ||
}; | ||
/** | ||
* Turns the value from a checkbox field into a boolean, | ||
* but does not require the checkbox to be checked. | ||
* For checkboxes with a `value` attribute, you can pass that as the `trueValue` option. | ||
* | ||
* @example | ||
* ```ts | ||
* const schema = zfd.formData({ | ||
* defaultCheckbox: zfd.checkbox(), | ||
* checkboxWithValue: zfd.checkbox({ trueValue: "true" }), | ||
* mustBeTrue: zfd | ||
* .checkbox() | ||
* .refine((val) => val, "Please check this box"), | ||
* }); | ||
* }); | ||
* ``` | ||
*/ | ||
export declare const checkbox: ({ trueValue }?: CheckboxOpts) => z.ZodUnion<[z.ZodEffects<z.ZodLiteral<string>, boolean, string>, z.ZodEffects<z.ZodLiteral<undefined>, boolean, undefined>]>; | ||
export declare const file: InputType<z.ZodType<File>>; | ||
/** | ||
* Preprocesses a field where you expect multiple values could be present for the same field name | ||
* and transforms the value of that field to always be an array. | ||
* If you don't provide a schema, it will assume the field is an array of zfd.text fields | ||
* and will not require any values to be present. | ||
*/ | ||
export declare const repeatable: InputType<ZodArray<any>>; | ||
/** | ||
* A convenience wrapper for repeatable. | ||
* Instead of passing the schema for an entire array, you pass in the schema for the item type. | ||
*/ | ||
export declare const repeatableOfType: <T extends z.ZodTypeAny>(schema: T) => z.ZodEffects<z.ZodArray<T, "many">, T["_output"][], T["_input"][]>; | ||
@@ -21,3 +61,10 @@ declare type FormDataType = { | ||
}; | ||
/** | ||
* This helper takes the place of the `z.object` at the root of your schema. | ||
* It wraps your schema in a `z.preprocess` that extracts all the data out of a `FormData` | ||
* and transforms it into a regular object. | ||
* If the `FormData` contains multiple entries with the same field name, | ||
* it will automatically turn that field into an array. | ||
*/ | ||
export declare const formData: FormDataType; | ||
export {}; |
@@ -12,4 +12,17 @@ "use strict"; | ||
}; | ||
/** | ||
* Transforms any empty strings to `undefined` before validating. | ||
* This makes it so empty strings will fail required checks, | ||
* allowing you to use `optional` for optional fields instead of `nonempty` for required fields. | ||
* If you call `zfd.text` with no arguments, it will assume the field is a required string by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
const text = (schema = zod_1.z.string()) => zod_1.z.preprocess(preprocessIfValid(stripEmpty), schema); | ||
exports.text = text; | ||
/** | ||
* Coerces numerical strings to numbers transforms empty strings to `undefined` before validating. | ||
* If you call `zfd.number` with no arguments, | ||
* it will assume the field is a required number by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
const numeric = (schema = zod_1.z.number()) => zod_1.z.preprocess(preprocessIfValid(zod_1.z.union([ | ||
@@ -23,2 +36,19 @@ stripEmpty, | ||
exports.numeric = numeric; | ||
/** | ||
* Turns the value from a checkbox field into a boolean, | ||
* but does not require the checkbox to be checked. | ||
* For checkboxes with a `value` attribute, you can pass that as the `trueValue` option. | ||
* | ||
* @example | ||
* ```ts | ||
* const schema = zfd.formData({ | ||
* defaultCheckbox: zfd.checkbox(), | ||
* checkboxWithValue: zfd.checkbox({ trueValue: "true" }), | ||
* mustBeTrue: zfd | ||
* .checkbox() | ||
* .refine((val) => val, "Please check this box"), | ||
* }); | ||
* }); | ||
* ``` | ||
*/ | ||
const checkbox = ({ trueValue = "on" } = {}) => zod_1.z.union([ | ||
@@ -34,2 +64,8 @@ zod_1.z.literal(trueValue).transform(() => true), | ||
exports.file = file; | ||
/** | ||
* Preprocesses a field where you expect multiple values could be present for the same field name | ||
* and transforms the value of that field to always be an array. | ||
* If you don't provide a schema, it will assume the field is an array of zfd.text fields | ||
* and will not require any values to be present. | ||
*/ | ||
const repeatable = (schema = zod_1.z.array((0, exports.text)())) => { | ||
@@ -45,5 +81,16 @@ return zod_1.z.preprocess((val) => { | ||
exports.repeatable = repeatable; | ||
/** | ||
* A convenience wrapper for repeatable. | ||
* Instead of passing the schema for an entire array, you pass in the schema for the item type. | ||
*/ | ||
const repeatableOfType = (schema) => (0, exports.repeatable)(zod_1.z.array(schema)); | ||
exports.repeatableOfType = repeatableOfType; | ||
const entries = zod_1.z.array(zod_1.z.tuple([zod_1.z.string(), zod_1.z.any()])); | ||
/** | ||
* This helper takes the place of the `z.object` at the root of your schema. | ||
* It wraps your schema in a `z.preprocess` that extracts all the data out of a `FormData` | ||
* and transforms it into a regular object. | ||
* If the `FormData` contains multiple entries with the same field name, | ||
* it will automatically turn that field into an array. | ||
*/ | ||
const formData = (shapeOrSchema) => zod_1.z.preprocess(preprocessIfValid( | ||
@@ -50,0 +97,0 @@ // We're avoiding using `instanceof` here because different environments |
{ | ||
"name": "zod-form-data", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"browser": "./browser/index.js", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
@@ -27,5 +27,18 @@ import { | ||
/** | ||
* Transforms any empty strings to `undefined` before validating. | ||
* This makes it so empty strings will fail required checks, | ||
* allowing you to use `optional` for optional fields instead of `nonempty` for required fields. | ||
* If you call `zfd.text` with no arguments, it will assume the field is a required string by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
export const text: InputType<ZodString> = (schema = z.string()) => | ||
z.preprocess(preprocessIfValid(stripEmpty), schema); | ||
/** | ||
* Coerces numerical strings to numbers transforms empty strings to `undefined` before validating. | ||
* If you call `zfd.number` with no arguments, | ||
* it will assume the field is a required number by default. | ||
* If you want to customize the schema, you can pass that as an argument. | ||
*/ | ||
export const numeric: InputType<ZodNumber> = (schema = z.number()) => | ||
@@ -49,2 +62,19 @@ z.preprocess( | ||
/** | ||
* Turns the value from a checkbox field into a boolean, | ||
* but does not require the checkbox to be checked. | ||
* For checkboxes with a `value` attribute, you can pass that as the `trueValue` option. | ||
* | ||
* @example | ||
* ```ts | ||
* const schema = zfd.formData({ | ||
* defaultCheckbox: zfd.checkbox(), | ||
* checkboxWithValue: zfd.checkbox({ trueValue: "true" }), | ||
* mustBeTrue: zfd | ||
* .checkbox() | ||
* .refine((val) => val, "Please check this box"), | ||
* }); | ||
* }); | ||
* ``` | ||
*/ | ||
export const checkbox = ({ trueValue = "on" }: CheckboxOpts = {}) => | ||
@@ -62,2 +92,8 @@ z.union([ | ||
/** | ||
* Preprocesses a field where you expect multiple values could be present for the same field name | ||
* and transforms the value of that field to always be an array. | ||
* If you don't provide a schema, it will assume the field is an array of zfd.text fields | ||
* and will not require any values to be present. | ||
*/ | ||
export const repeatable: InputType<ZodArray<any>> = ( | ||
@@ -73,2 +109,6 @@ schema = z.array(text()) | ||
/** | ||
* A convenience wrapper for repeatable. | ||
* Instead of passing the schema for an entire array, you pass in the schema for the item type. | ||
*/ | ||
export const repeatableOfType = <T extends ZodTypeAny>( | ||
@@ -85,2 +125,9 @@ schema: T | ||
/** | ||
* This helper takes the place of the `z.object` at the root of your schema. | ||
* It wraps your schema in a `z.preprocess` that extracts all the data out of a `FormData` | ||
* and transforms it into a regular object. | ||
* If the `FormData` contains multiple entries with the same field name, | ||
* it will automatically turn that field into an array. | ||
*/ | ||
export const formData: FormDataType = <T extends z.ZodRawShape | z.ZodTypeAny>( | ||
@@ -87,0 +134,0 @@ shapeOrSchema: T |
Sorry, the diff of this file is not supported yet
30491
564