npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nuxt-yup

v2.4.4

Published

yup implementation for nuxt

Readme

Nuxt Yup

npm version npm downloads License Nuxt

A Nuxt module that brings Yup into your app with first-class Nuxt integration, global access, and typed extension support.

Features

  • 🔌  Use Yup anywhere with the auto-imported useYup() composable
  • 🌍  Configure global validation messages through app.config.ts
  • 📦  Add custom methods from yup.methods.ts
  • 🏷️  Automatically generate TypeScript types for custom Yup methods

Why use nuxt-yup?

nuxt-yup gives you a Nuxt-native Yup workflow with minimal setup.

  • Auto-imported useYup() composable
  • Global configuration through app.config.ts
  • Typed custom Yup extensions
  • Seamless Nuxt runtime integration
  • Better DX with less boilerplate

Quick Example

const yup = useYup()

const userSchema = yup.object({
  name: yup.string().required(),
  email: yup.string().email().required(),
  document: yup.string().cnpj(), // custom method from yup.methods.ts
})

const sampleUser = {
  name: 'Ada Lovelace',
  email: '[email protected]',
  document: '12345678901234',
}

await userSchema.validate(sampleUser)

Quick Setup

Install the module in your Nuxt project:

npx nuxi@latest module add nuxt-yup

Yup is now available globally in your app.

Usage

Access Yup anywhere with useYup() or the $yup plugin.

<template>
  <div>
    <input v-model="value" placeholder="Enter a value" />
    <p>Is valid: {{ isValid }}</p>
  </div>
</template>

<script setup>
const yup = useYup()
// or: const { $yup } = useNuxtApp()

const value = ref('')
const isValid = ref(false)

const schema = yup.string().min(3).required()

watch(value, async (newValue) => {
  isValid.value = await schema.isValid(newValue)
})
</script>

Comparison

| Feature | Yup | nuxt-yup | |---|---|---| | Auto-import composable | Manual import and wiring | useYup() available automatically | | Global config | Manual setup in app bootstrap | Configure with app.config.ts | | Typed extensions | Manual declaration merging | Typed descriptors + generated augmentation | | Nuxt developer experience | Generic library usage | Nuxt-first integration and DX |


Configuration

Customizing error messages (setLocale)

Override Yup default messages globally with yup.setLocale in app.config.ts.

// app.config.ts
export default defineAppConfig({
  yup: {
    setLocale: {
      mixed: {
        required: 'This field is required',
      },
      string: {
        min: 'Must be at least ${min} characters',
        email: 'Must be a valid email address',
      },
      number: {
        min: 'Must be at least ${min}',
        max: 'Must be at most ${max}',
      },
    },
  },
})

See the full list of keys in the Yup locale documentation.


Module options (nuxt.config.ts)

Use methodsDir when your methods file is outside the Nuxt root.

// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-yup'],
  yup: {
    methodsDir: './shared/validation',
  },
})
  • The module searches this directory for yup.methods.*.
  • If no file is found, it logs an error and continues without custom methods.

yup.methods.ts

Create a yup.methods.ts file in your project root to register custom methods. The module auto-detects it and supports two modes.

Typed Mode — defineYupExtension (recommended)

Export an array of descriptors with defineYupExtension. Type augmentations are generated automatically.

// yup.methods.ts
import { defineYupExtension } from 'nuxt-yup'

export default defineYupExtension([
  {
    name: 'cnpj',
    type: 'string',
    message: 'Invalid CNPJ',
    validate(value: string) {
      return /^\d{14}$/.test(value)
    },
  },
  {
    name: 'isEven',
    type: 'number',
    message: 'Must be an even number',
    async validate(value: number) {
      return value % 2 === 0
    },
  },
])

Each descriptor accepts:

| Property | Type | Description | |---|---|---| | name | string | Method name to add to the schema | | type | YupExtensionType | Yup schema type to extend | | message | string (optional) | Default error message | | validate | (value) => boolean \| Promise<boolean> | Validation logic (sync or async) |

Yup types are automatically augmented, so you get full autocomplete:

// ✅ TypeScript knows about .cnpj() and .isEven()
useYup().string().cnpj()
useYup().number().isEven()

Compat Mode — function export

For full control (for example, migrations from an existing setup), export a function that receives the yup instance directly. Type augmentations are not generated in this mode.

// yup.methods.ts
import type * as Yup from 'yup'

export default function extendYup(yup: typeof Yup) {
  yup.addMethod(yup.StringSchema, 'documentId', function (message = 'Invalid document') {
    return this.test('document-id', message, (value) => {
      if (value == null) return true
      return typeof value === 'string' && value.length >= 6
    })
  })
}

Supported file extensions

The yup.methods file is resolved in this order from your project root (or from methodsDir):

.ts.mts.cts.js.mjs.cjs


Works well with

  • Nuxt 3 / Nuxt 4
  • TypeScript
  • vee-validate
  • Nuxt server routes (server/api/*)

Contributing

Contributions are welcome. Open an issue for bugs or feature ideas, and submit a PR when you're ready.

For local development and test commands, see package.json.


License

MIT — Made with ❤️ by Darlan Prado