mcpField
Y
yup

yup

github.com/claude-dev-suiteDevelopment4650 installsUpdated 2d ago
Repository

About yup

A quick-reference skill for Yup, aimed at developers building JavaScript or TypeScript validation schemas. It covers common schema types, validation options, and form-oriented guidance.

Key features

  1. 01String, number, date, array, and object schemas
  2. 02Synchronous and asynchronous validation
  3. 03Type inference with InferType
  4. 04Unknown-field stripping and type coercion

Use cases

  1. 01Validate user and form data
  2. 02Collect multiple validation errors
  3. 03Infer TypeScript types from schemas

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/claude-dev-suite/claude-dev-suite --skill yup

After installing, type/yupin Claude Code to confirm it loaded.

Bundled filesadvanced.md

Details

From the project README

Yup - Quick Reference

Full Reference: See advanced.md for conditional validation (.when), custom validation (.test), Formik/React Hook Form integration, transforms, and localization.

Deep Knowledge: Use mcp__documentation__fetch_docs with technology: yup for comprehensive documentation.

Setup

npm install yup

Basic Usage

import * as yup from 'yup';

const userSchema = yup.object({
  name: yup.string().required().min(2).max(50),
  email: yup.string().required().email(),
  age: yup.number().required().min(18).max(120).integer(),
  bio: yup.string().optional(),
});

// Type inference
type User = yup.InferType<typeof userSchema>;

// Validate
async function validateUser(data: unknown) {
  try {
    const user = await userSchema.validate(data, { abortEarly: false });
    return { success: true, data: user };
  } catch (error) {
    if (error instanceof yup.ValidationError) {
      return { success: false, errors: error.errors };
    }
    throw error;
  }
}

Schema Types

String
const stringSchema = yup
  .string()
  .required('Name is required')
  .min(2, 'Too short')
  .max(100, 'Too long')
  .trim()
  .email('Invalid email')
  .url('Invalid URL')
  .matches(/^[a-zA-Z]+$/, 'Only letters allowed')
  .oneOf(['option1', 'option2'], 'Must be one of the options');
Number
const numberSchema = yup
  .number()
  .required()
  .min(0)
  .max(100)
  .positive()
  .integer()
  .typeError('Must be a number');
Date
const dateSchema = yup
  .date()
  .required()
  .min(new Date('2020-01-01'), 'Too old')
  .max(new Date(), 'Cannot be in future');
Array
const arraySchema = yup
  .array()
  .of(yup.string().required())
  .required()
  .min(1, 'At least one item')
  .max(10, 'Maximum 10 items')
  .ensure();  // Transform null/undefined to []
Object
const objectSchema = yup.object({
  name: yup.string().required(),
  address: yup.object({
    street: yup.string().required(),
    city: yup.string().required(),
  }),
}).noUnknown(true);  // Strip unknown keys

// Partial object (all fields optional)
const partialSchema = objectSchema.partial();

Validation Options

const result = await schema.validate(data, {
  abortEarly: false,    // Return all errors, not just first
  stripUnknown: true,   // Remove unknown keys
  strict: false,        // Allow type coercion
});

// Sync validation
schema.validateSync(data);
schema.isValidSync(data);  // Returns boolean

Comparison: Yup vs Zod

FeatureYupZod
Type inferenceGoodExcellent
Bundle size~12KB~8KB
Async validationBuilt-in.parseAsync()
FormikNativeVia resolver
Conditional.when().refine()

When NOT to Use This Skill

  • New projects - Prefer Zod for better TypeScript integration
  • NestJS applications - Use class-validator with decorators
  • Non-form validation - Zod has better API for general validation

Anti-Patterns

Anti-PatternWhy It's BadCorrect Approach
Schema in componentRe-created on renderDefine at module level
Forgetting abortEarly: falseOnly shows first errorSet abortEarly: false
No custom messagesGeneric errorsAdd custom message to each
Not using InferTypeManual type definitionsUse yup.InferType

Quick Troubleshooting

IssueCauseSolution
Type errors with InferTypeMissing importImport type from 'yup'
Async validation not workingUsing validateSyncUse .validate() (async)
"Value is required" unexpectedlyUndefined vs nullUse .nullable() or .optional()
Error messages not showingabortEarly: trueSet abortEarly: false

Production Checklist

  • Schema defined at module level
  • Type inference with InferType
  • Custom error messages
  • abortEarly: false for forms
  • Resolver for form library
  • Localization if multilingual

Reference Documentation

Comments

Markdown supported