Registry / testing / yup
library1.7.1jsnpmunverified

Yup is a schema builder for runtime value parsing and validation, now at v1.7.1 with active development. It provides a concise, chainable API for defining schemas that can parse, coerce, and validate complex nested objects, with built-in async support and rich error details. Key differentiators include powerful TypeScript inference via InferType, seamless integration with Standard Schema, and extensibility through custom methods. It remains a top choice for form validation in React ecosystems (Formik) and general data validation, competing with Zod (more lightweight) and Joi (Node-only).

npm install yup
INSTALL
IMPORT
SIG · YUP
Y
yup
testingjavascriptv1.7.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

object
import { object } from 'yup'
import yup, { object } from 'yup'
Yup exports all top-level schemas as named exports; there is no default export in v1.
string
import { string } from 'yup'
import yup from 'yup'; yup.string()
In v1, you must import named exports. The old require('yup').string() still works in CommonJS but is not recommended for tree-shaking.
InferType
import { InferType } from 'yup'
import { Infertype } from 'yup' (case-sensitive)
InferType is a utility type that derives the TypeScript type from a schema. It is case-sensitive.
ValidationError
import { ValidationError } from 'yup'
import { ValidationException } from 'yup'
ValidationError is the class for validation errors, not ValidationException.

Defines a user schema with required name, age, email, website, and createdOn fields, then validates an input object asynchronously.

import { object, string, number, date, InferType } from 'yup'; let userSchema = object({ name: string().required(), age: number().required().positive().integer(), email: string().email(), website: string().url().nullable(), createdOn: date().default(() => new Date()), }); async function validateUser(input: any) { try { const user = await userSchema.validate(input); console.log('Valid user:', user); } catch (err) { console.error('Validation failed:', err.errors); } } // Example usage: validateUser({ name: 'Alice', age: '30', email: 'alice@example.com' });
Debug
Known issues
breakingYup v1.0.0 removed the default export. Use named imports instead.
fix
Replace 'import yup from "yup"' with 'import { object, string, ... } from "yup"'
affects: >=1.0.0
breakingYup v1.0.0 changed .validate() to return a Promise. It no longer supports synchronous validation without a callback.
fix
Use await or .then() when calling .validate(). For synchronous casting, use .cast() instead.
affects: >=1.0.0
breakingYup v1.0.0 removed the .validateSync() method. Use .validate() with async/await or .cast() for synchronous coercion.
fix
Replace .validateSync() with await .validate() or use .cast() if you only need coercion.
affects: >=1.0.0
deprecatedUsing yup.mixed() is deprecated in v1 in favor of using specific schema types like object(), string(), etc.
fix
Replace yup.mixed() with a concrete schema type (e.g., string(), number()) or use lazy() for dynamic schemas.
affects: >=1.0.0
gotchaYup does not support circular references in schemas by default. Attempting to define a recursive schema without lazy() will cause a stack overflow.
fix
Use yup.lazy() to define recursive schemas. Example: const nodeSchema = object({ children: array().of(lazy(() => nodeSchema)) })
affects: all
Errors
Common errors & fixes
TypeError: yup.string is not a function
Using default import in v1, but yup v1 has no default export.
fix
Change import to named imports: import { string } from 'yup'
ValidationError: age must be a `number` type, but the final value was: `NaN` (cast from the value `"abc"`).
Attempting to cast or validate a string that cannot be parsed as a number.
fix
Ensure input values are of the correct type, or use .transform() to handle coercion gracefully.
Cannot read properties of undefined (reading 'validate')
Trying to call .validate() on a schema that hasn't been defined (e.g., variable is undefined).
fix
Ensure the schema variable is properly initialized before calling methods on it.
Upgrade
Version history
1.7.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
39 hits · last 30 days
node
34
OpenAI (training)
1
Resources
packageyup
yup — npm install yup · libregistry