BuzzForm
BuzzFormDocs

Validation

Learn about auto-generated validation, custom validators, and async validation in BuzzForm.

Validation

BuzzForm automatically generates Zod validation schemas from your field definitions. You can also add custom validation logic.

Auto-Generated Validation

Validation is generated from field properties:

const schema = createSchema([
  {
    type: "text",
    name: "username",
    required: true,      // → z.string().min(1)
    minLength: 3,        // → .min(3)
    maxLength: 20,       // → .max(20)
    pattern: /^[a-z]+$/, // → .regex()
  },
  {
    type: "number",
    name: "age",
    required: true,
    min: 18,             // → z.number().min(18)
    max: 120,            // → .max(120)
  },
  {
    type: "email",
    name: "email",
    required: true,      // Automatic email format validation
  },
]);

Validation by Field Type

Field TypeAuto Validations
text, textarearequired, minLength, maxLength, pattern
emailrequired + email format
passwordrequired, minLength, maxLength, criteria
numberrequired, min, max, precision
date, datetimerequired, minDate, maxDate
select, radiorequired, validates against options
tagsminTags, maxTags, maxTagLength
uploadrequired, minFiles, maxFiles, maxSize
arrayminRows, maxRows

Custom Validation

Add the validate prop for custom logic:

{
  type: "text",
  name: "username",
  label: "Username",
  validate: (value, context) => {
    if (value.includes(" ")) {
      return "Username cannot contain spaces";
    }
    return true; // Valid
  },
}

Validation Context

The validation function receives a context object:

validate: (value, context) => {
  // context.data - Complete form data
  // context.siblingData - Data at the same nesting level
  // context.path - Path segments to this field

  if (context.data.country === "US" && !value.match(/^\d{5}$/)) {
    return "US ZIP codes must be 5 digits";
  }
  return true;
}

Cross-Field Validation

Access other fields via context.data:

const schema = createSchema([
  { type: "password", name: "password", label: "Password" },
  {
    type: "password",
    name: "confirmPassword",
    label: "Confirm Password",
    validate: (value, { data }) => {
      if (value !== data.password) {
        return "Passwords do not match";
      }
      return true;
    },
  },
]);

Async Validation

Return a Promise for async validation (e.g., checking username availability):

{
  type: "text",
  name: "username",
  label: "Username",
  validate: async (value) => {
    const available = await checkUsernameAvailable(value);
    if (!available) {
      return "Username is already taken";
    }
    return true;
  },
}

Validation Modes

Configure when validation runs via the provider or form:

<BuzzFormProvider mode="onBlur">
  {/* Validates when fields lose focus */}
</BuzzFormProvider>
ModeDescription
onSubmitValidate only on form submission
onBlurValidate when field loses focus (default)
onChangeValidate on every keystroke
allValidate on all events

Server-Side Errors

Set errors programmatically after server validation:

const handleSubmit = async (data) => {
  const result = await submitToServer(data);
  
  if (result.errors) {
    // Set field-level errors from server
    form.setError("email", { message: "Email already registered" });
  }
};

On this page