# @vuelidate/validators

> Validators for Vuelidate

Latest version **2.0.4** (published 2023-08-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install @vuelidate/validators
pnpm add @vuelidate/validators
yarn add @vuelidate/validators
bun add @vuelidate/validators
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2023-08-25 |
| First published | 2019-11-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 102 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6865 |
| Maintainers | shentao, marinamosti, dobromir-hristov |

## Links

- npm: https://www.npmjs.com/package/@vuelidate/validators
- Repository: https://github.com/vuelidate/vuelidate
- Homepage: https://github.com/vuelidate/vuelidate#readme
- Issues: https://github.com/vuelidate/vuelidate/issues
- npm.io page: https://npm.io/package/@vuelidate/validators

## Dependencies (1)

- [vue-demi](https://npm.io/package/vue-demi.md) ^0.13.11

## Recent versions

- 2.0.4 (latest) — 2023-08-25
- 1.0.0-alpha.2 (next) — 2019-11-05
- 2.0.3 — 2023-06-29
- 2.0.2 — 2023-03-29
- 2.0.1 — 2023-03-26
- 2.0.0 — 2022-09-25
- 2.0.0-alpha.32 — 2022-09-25
- 2.0.0-alpha.31 — 2022-07-23
- 2.0.0-alpha.30 — 2022-06-24
- 2.0.0-alpha.29 — 2022-05-01
- 2.0.0-alpha.28 — 2022-03-26
- 2.0.0-alpha.27 — 2022-03-13
- 2.0.0-alpha.26 — 2021-12-28
- 2.0.0-alpha.25 — 2021-11-01
- 2.0.0-alpha.24 — 2021-10-20
- … 24 more at https://npm.io/package/@vuelidate/validators/versions

## README

# Vuelidate Validators

This is the standalone validators package for [Vuelidate](https://github.com/vuelidate/vuelidate).

## Installation

You need to install both this package and Vuelidate.

```bash
npm install @vuelidate/core @vuelidate/validators
```
or with yarn
```bash
yarn add @vuelidate/core @vuelidate/validators
```

## Usage

After adding the Vuelidate plugin, you can use the validators by importing them directly.
These validators come with error messages out of the box.

```js
import { required, email } from '@vuelidate/validators'

export default {
  data: () => ({
    name: '',
    email: ''
  }),
  validations: {
    name: { required },
    email: { required, email }
  }
}
```

### Raw, no message validators

If you want to use validators without error messages, you can import the raw validators.

```js
import { required, email } from '@vuelidate/validators/dist/raw.esm'
```

### Extending a validator with custom message

You can attach a validation message to a validator via tha `withMessage` method.

```js
import { common, required } from '@vuelidate/validators'

const requiredWithMessage = common.withMessage(required, 'This field is required and must be filled in')

export default {
  ...,
  validations: {
    name: { requiredWithMessage }
  }
}
```

### Attaching extra data to a validator

If you want to attach extra data properties to validator, so you can use them in the messages and when validating, use the `withParams` helper.

```js
import { common } from '@vuelidate/validators'

const atLeast = (number) => common.withParams({ number }, (value) => value.length <= number) // just an example

export default {
  ...,
  validations: {
    name: { atLeast: atLeast(5) }
  }
}
```

### Combining params and messages

You can combine both helpers to build a validator.

```js
import { common } from '@vuelidate/validators'

const customMinLength = (number) => common.withMessage((value) => value.length <= number, ({ $params }) => `The field must be at least ${$params.number} chars long`)
const atLeast = (number) => common.withParams({ number }, customMinLength(number)) // just an example

export default {
  ...,
  validations: {
    name: { atLeast: atLeast(5) }
  }
}
```

## Development
To test the package run

```bash
yarn test:unit
```

To link the package run

```bash
yarn link
```

To build run the package, run: 

```bash
npm run build
```

---
_Source: https://npm.io/package/@vuelidate/validators · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
