Course contentsShow
← React
Lesson 1232 of 2,39631. Forms in ReactFree lesson

Validation with Yup Integration

How Formik integrated with Yup for schema-based validation before Zod was common.

What You'll Learn

You'll learn how Formik leveraged Yup as its primary schema-based validation solution, establishing a pattern that dominated React form validation before Zod gained popularity.

The Formik + Yup Partnership

Before Zod existed, Yup was the de facto standard for schema-based form validation in React. Formik was designed with Yup integration as a first-class feature, providing seamless validation through a simple validationSchema prop.

How It Worked

Instead of writing validation functions manually, you'd define a Yup schema that describes what valid data looks like. Formik would automatically run your Yup schema against form values and populate the errors object:

import { useFormik } from 'formik';
import * as Yup from 'yup';

const validationSchema = Yup.object({
  email: Yup.string()
    .email('Invalid email address')
    .required('Email is required'),
  age: Yup.number()
    .min(18, 'Must be at least 18')
    .required('Age is required')
});

function MyForm() {
  const formik = useFormik({
    initialValues: { email: '', age: '' },
    validationSchema, // Pass the schema here
    onSubmit: values => console.log(values)
  });

  return (
    <form onSubmit={formik.handleSubmit}>
      <input name="email" {...formik.getFieldProps('email')} />
      {formik.errors.email && <span>{formik.errors.email}</span>}
      {/* Yup error messages appear automatically */}
    </form>
  );
}

Why This Was Powerful

Think of Yup as a contract for your data: you declare what's acceptable once, and Formik enforces it everywhere. No need to write if-statements checking every field. Yup provided built-in validators (.email(), .min(), .required()) with customizable error messages, and Formik translated validation failures directly into the errors object structure.

Key Takeaway: Formik's validationSchema prop with Yup provided declarative, schema-based validation that kept validation logic separate from component code, establishing the pattern that modern form libraries still follow today.