Validation6M+/wkMIT

yup

Yup is a schema validation library for JavaScript that provides a declarative, chainable API for defining object schemas and validating data. Yup schemas define

Installation

npm
npm install yup
yarn
yarn add yup
pnpm
pnpm add yup

Import

ESM
import * as yup from 'yup';

Quick Example

usage
import * as yup from 'yup';

const schema = yup.object({
  name: yup.string().required('Name is required'),
  email: yup.string().email('Invalid email').required(),
  age: yup.number().positive().integer().optional(),
});

try {
  const valid = await schema.validate({ name: 'Alice', email: '[email protected]' });
  console.log(valid);
} catch (err) {
  console.log(err.errors);
}

About yup

Yup is a schema validation library for JavaScript that provides a declarative, chainable API for defining object schemas and validating data. Yup schemas define the shape of data with type coercion, default values, and validation rules that produce detailed error messages. The API is expressive: yup.string().required().email() defines a required email field, yup.number().min(0).max(100).integer() defines a bounded integer, and yup.object({ name: yup.string() }) defines an object shape. Yup supports conditional validation through .when() (validate field B differently based on field A's value), dependent validation with .test() for custom logic, type casting/coercion, nullable and optional fields, and array/tuple schemas. Yup provides both synchronous (.validateSync()) and asynchronous (.validate()) validation for schemas with async custom tests. The library integrates particularly well with Formik, the popular React form library, which was designed around Yup's schema system. Yup also works with React Hook Form through the @hookform/resolvers package. While Zod has gained significant popularity for its TypeScript-first approach, Yup remains widely used, especially in existing React projects using Formik for form management.

Quick Facts

Packageyup
CategoryValidation
Weekly Downloads6M+
LicenseMIT
Installnpm install yup

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.