@matthew.ngo/reform
Version:
A flexible and powerful React form management library with advanced validation, state observation, and multi-group support
51 lines (50 loc) • 1.63 kB
TypeScript
import * as yup from 'yup';
import { ObjectSchema, AnyObject, Schema } from 'yup';
/**
* Type representing the Reform data structure
*/
export declare type ReformSchema<T> = {
groups: Array<{
data: T;
}>;
};
/**
* Creates a Yup schema compatible with Reform's data structure
* Supports multiple ways of defining schemas to match user preferences
*
* @template T - The type of form data
* @param schemaDefinition - Schema definition in various formats
* @returns Yup schema for the entire Reform structure
*
* @example
* // Method 1: Using field-by-field schema definition
* const userSchema = createReformSchema({
* name: yup.string().required(),
* age: yup.number().min(18),
* tags: yup.array().of(yup.string())
* });
*
* @example
* // Method 2: Using a pre-defined Yup object schema
* const fieldSchema = yup.object({
* name: yup.string().required(),
* age: yup.number().min(18)
* });
* const userSchema = createReformSchema(fieldSchema);
*
* @example
* // Method 3: Using a schema builder function
* const userSchema = createReformSchema(fields => ({
* name: fields.string().required(),
* email: fields.string().email(),
* preferences: fields.object({
* theme: fields.string(),
* notifications: fields.boolean()
* })
* }));
*/
export declare function createReformSchema<T extends Record<string, any>>(schemaDefinition: Record<string, Schema<any>> | ObjectSchema<T> | ((fields: typeof yup) => Record<string, Schema<any>>)): ObjectSchema<{
groups: Array<{
data: T;
}>;
}, AnyObject, any>;