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 install yup
yarn add yup
pnpm add yup
Import
import * as yup from 'yup';
Quick Example
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
| Package | yup |
| Category | Validation |
| Weekly Downloads | 6M+ |
| License | MIT |
| Install | npm install yup |
Related Packages
Zod is a TypeScript-first schema validation library that provides a concise, chainable API for defin…
Joi is the most powerful schema description language and data validator for JavaScript, originally c…
Ajv (Another JSON Schema Validator) is the fastest JSON Schema validator for JavaScript and TypeScri…
Valibot is a modular schema validation library for TypeScript that achieves exceptionally small bund…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.