react-server-actions
Version:
A package for working with actions in React and Next.js
53 lines • 2.37 kB
TypeScript
import { z } from 'zod';
/**
* HTML5 validation attributes that can be inferred from a Zod schema
*/
export type ZodValidationAttrs = {
/** The input type (text, email, url, number, date, checkbox, radio, file) */
type?: 'text' | 'email' | 'url' | 'number' | 'date' | 'checkbox' | 'radio' | 'file';
/** Whether the field is required */
required?: boolean;
/** Minimum length for string inputs */
minLength?: number;
/** Maximum length for string inputs */
maxLength?: number;
/** Minimum value for number inputs or minimum date for date inputs */
min?: number | string;
/** Maximum value for number inputs or maximum date for date inputs */
max?: number | string;
/** Step value for number inputs */
step?: number;
};
/**
* The return type of getZodValidationAttributes
*/
export type ZodValidationAttributes = {
type: 'string' | 'number' | 'date' | 'boolean' | 'enum' | 'file';
attrs: ZodValidationAttrs;
};
/**
* Get the html5 validation attributes from a Zod schema field
* @param schema - The Zod schema
* @param path - The path to the field in the schema
* @returns The validation attributes for the field
* @note It would be cool to infer also the "type" attribute of the field, but this would not be consistent because a zod rule is not a 1-1 relation with an html input.
* For example, a zod.number() could be an <input type="number" /> but also a <select>. A z.date() could be represented by a <input type="date" /> but also a <input type="datetime-local" />.
* We could make a "guess" based on the zod rule, and then could be overriden by the user, but this would lead to confusion.
* So we leave it as a parameter for now.
*/
export declare function getZodValidationAttributes(schema: z.ZodType<any>, path: string[], options?: {
inferTypeAttr?: boolean;
}): ZodValidationAttributes;
/**
* Convert a date to an <input type="date"> default value
* @param date - The date to convert
* @returns The input default value
*/
export declare const dateToInputDefaultValue: (date: Date) => string | undefined;
/**
* Convert a date to an <input type="datetime-local"> default value
* @param date - The date to convert
* @returns The input default value
*/
export declare const datetimeToInputDefaultValue: (date: Date) => string;
//# sourceMappingURL=helpers.d.ts.map