Yup Schema Validation
Build expressive validation schemas with Yup for form validation and data parsing in JavaScript applications.
Overview
The library supports conditional validation through the `.when()` method, allowing validation rules to depend on other field values. This is particularly useful for complex forms where field requirements change based on user selections. Yup also provides `.test()` for custom async validation, enabling checks against APIs like uniqueness validation.
Yup's error messages are customizable at both the schema level and globally. The library supports localization through custom locale objects, making it easy to provide error messages in multiple languages. While similar to Zod in purpose, Yup focuses on runtime validation with automatic coercion, making it particularly well-suited for form validation where input values start as strings.
Who Is This For?
- Validate form inputs with Formik and Yup schemas
- Define conditional validation rules for complex forms
- Validate API request data with custom error messages
- Set up multilingual validation error messages
Installation
npm install yup Configuration
import * as yup from "yup"
const schema = yup.object({
name: yup.string().required("Name is required").min(2),
email: yup.string().email("Invalid email").required(),
age: yup.number().positive().integer().nullable(),
website: yup.string().url().optional(),
password: yup.string().min(8).required(),
confirmPassword: yup.string()
.oneOf([yup.ref("password")], "Passwords must match")
.required(),
})
await schema.validate(formData, { abortEarly: false }) Explore AI Tools
Discover the best AI tools that complement your skills
Read AI & Design Articles
Tips and trends in the world of design and AI
Related Servers
Snyk Security Scan
Detect vulnerabilities in your dependencies and application code. Get actionable remediation advice and automatic fix pull requests.
SonarQube Code Quality
Run continuous code quality and security analysis to catch bugs, code smells, and vulnerabilities before they reach production.
OWASP ZAP Security Testing
Perform automated web application security testing to find common vulnerabilities like XSS, injection flaws, and misconfigurations.