Skip to content

API Reference

The single hook exported by rapid-form. Call it inside any React component to get form state and the validation attachment function.

import { useRapidForm } from 'rapid-form'
const { refValidation, values, errors, numberOfRequiredFields } = useRapidForm()
NameTypeDescription
refValidation(ref: HTMLFormElement | null, config?: Config) => voidAttaches validation logic to a form element. Pass it as the form’s ref callback.
valuesRecord<string, Value>Live map of every tracked field, keyed by the field’s name attribute.
errorsRecord<string, FieldError>Map of validation errors for each field, keyed by the field’s name attribute. Only fields that have been interacted with appear here.
numberOfRequiredFieldsnumberCount of fields that carry the required attribute in the attached form.

Attaches Rapid Form’s validation listeners to a form element. Typically called inside a React ref callback on the <form> element.

<form
ref={(ref) => {
refValidation(ref, config)
}}
>
{/* fields */}
</form>
ParameterTypeRequiredDescription
refHTMLFormElement | nullYesThe native form element. React passes null when the component unmounts — the function handles this safely.
configConfigNoOptional configuration object. See Config below.

The optional second argument to refValidation. All properties are optional.

type Config = {
eventType?: 'input' | 'blur' | 'change'
resetOnSubmit?: boolean
validations?: Record<string, ValidationEntry>
resolver?: SchemaResolver
}
PropertyTypeDefaultDescription
eventType'input' | 'blur' | 'change''input'The DOM event that triggers validation for all fields. Can be overridden per field inside validations.
resetOnSubmitbooleantrueWhen true, form field values are cleared and the values / errors state is reset after the form’s submit event fires.
validationsRecord<string, ValidationEntry>Per-field validation overrides. The record key must match the field’s name attribute. Ignored when resolver is set.
resolverSchemaResolverSchema resolver function. When provided it replaces per-field validations. See Schema Validation for built-in Zod and Yup adapters.

A per-field configuration object supplied as a value inside Config.validations.

type ValidationEntry = {
validation: (props: {
value: string
formElements: HTMLFormControlsCollection
}) => boolean
eventType?: 'input' | 'blur' | 'change'
message?: string
}
PropertyTypeRequiredDescription
validation(props: { value: string; formElements: HTMLFormControlsCollection }) => booleanYesCustom validation function. Return true when the value is valid, false when it is invalid. Receives the current field value and the full formElements collection so you can cross-reference other fields.
eventType'input' | 'blur' | 'change'NoOverrides the top-level eventType for this specific field only.
messagestringNoError message stored in errors[name].message when this field’s validation returns false.

Example — require a field to be longer than 10 characters, validated on blur:

refValidation(ref, {
validations: {
username: {
eventType: 'blur',
validation: ({ value }) => value.length > 10,
message: 'Username must be more than 10 characters',
},
},
})

Example — cross-field validation (password confirmation):

refValidation(ref, {
validations: {
confirmPassword: {
validation: ({ value, formElements }) => {
const password = formElements.namedItem('password') as HTMLInputElement
return value === password?.value
},
message: 'Passwords do not match',
},
},
})

Each entry in the errors map conforms to the FieldError type.

type FieldError = {
name: string
value: string
isInvalid: boolean
errorType?: 'invalidFormat'
message?: string
}
PropertyTypeDescription
namestringThe name attribute of the form field.
valuestringThe current value of the field at the time the error was recorded.
isInvalidbooleantrue when the field fails validation, false when it passes. Use this as the primary flag for disabling submit buttons or showing error UI.
errorType'invalidFormat' | undefinedSet to 'invalidFormat' for built-in format checks (e.g. email format, password length). undefined for custom validation errors.
messagestring | undefinedHuman-readable error message. Populated from the message property of a ValidationEntry, or a built-in default message for format errors.

Typical usage:

const { errors } = useRapidForm()
const isFormValid = Object.values(errors).every((e) => !e.isInvalid)
// Render an inline error
{errors.email?.isInvalid && (
<span role="alert">{errors.email.message}</span>
)}
// Disable the submit button until all fields are valid
<button type="submit" disabled={!isFormValid}>
Submit
</button>

A function type that receives all current form values and returns a map of error messages. Both sync and async resolvers are supported.

import type { SchemaResolver } from 'rapid-form'
type SchemaResolver = (
values: Record<string, string>
) =>
| Record<string, string | undefined>
| Promise<Record<string, string | undefined>>

Return undefined (or omit the key) for fields that pass validation. Return a string message for fields that fail.

Built-in adapters are available for Zod and Yup — see the Schema Validation guide.


All types are re-exported from the package root and can be imported directly.

import type { Config, Value, FieldError, NumberOfRequiredFields, SchemaResolver } from 'rapid-form'
TypeDefinitionDescription
Config{ eventType?: EventType; resetOnSubmit?: boolean; validations?: Record<string, ValidationEntry>; resolver?: SchemaResolver }The optional config parameter accepted by refValidation.
Value{ name: string; value: string }Shape of each entry in the values map returned by useRapidForm.
FieldError{ name: string; value: string; isInvalid: boolean; errorType?: 'invalidFormat'; message?: string }Shape of each entry in the errors map returned by useRapidForm.
NumberOfRequiredFieldsnumberAlias for the numberOfRequiredFields return value.
SchemaResolver(values: Record<string, string>) => Record<string, string | undefined> | Promise<...>Schema resolver function type. Use with the built-in adapters or write your own.