All Projects → vuelidate → Vuelidate

vuelidate / Vuelidate

Licence: mit
Simple, lightweight model-based validation for Vue.js

Programming Languages

javascript
184084 projects - #8 most used programming language
Vue
7211 projects
Sass
350 projects
Pug
443 projects
SCSS
7915 projects
HTML
75241 projects

Projects that are alternatives of or similar to Vuelidate

Superstruct
A simple and composable way to validate data in JavaScript (and TypeScript).
Stars: ✭ 5,604 (-9.41%)
Mutual labels:  validation
Valid.js
📝 A library for data validation.
Stars: ✭ 604 (-90.24%)
Mutual labels:  validation
Intl Tel Input
A JavaScript plugin for entering and validating international telephone numbers
Stars: ✭ 5,963 (-3.6%)
Mutual labels:  validation
Store Receipt Validator
PHP receipt validator for Apple iTunes, Google Play and Amazon App Store
Stars: ✭ 547 (-91.16%)
Mutual labels:  validation
Vue Mc
Models and Collections for Vue
Stars: ✭ 588 (-90.49%)
Mutual labels:  validation
Vue Form
Form validation for Vue.js 2.2+
Stars: ✭ 618 (-90.01%)
Mutual labels:  validation
Validation
PHP Standalone Validation Library
Stars: ✭ 520 (-91.59%)
Mutual labels:  validation
Malli
Data-Driven Schemas for Clojure/Script.
Stars: ✭ 667 (-89.22%)
Mutual labels:  validation
Vvalidator
🤖 An easy to use form validator for Kotlin & Android.
Stars: ✭ 592 (-90.43%)
Mutual labels:  validation
Validation
The most awesome validation engine ever created for PHP
Stars: ✭ 5,484 (-11.35%)
Mutual labels:  validation
Checkmail
Golang package for email validation
Stars: ✭ 554 (-91.04%)
Mutual labels:  validation
Nice Validator
Simple, smart and pleasant validation solution.
Stars: ✭ 587 (-90.51%)
Mutual labels:  validation
Ng2 Validation
angular2 validation
Stars: ✭ 622 (-89.95%)
Mutual labels:  validation
Express Validator
An express.js middleware for validator.js.
Stars: ✭ 5,236 (-15.36%)
Mutual labels:  validation
Vue Composable
Vue composition-api composable components. i18n, validation, pagination, fetch, etc. +50 different composables
Stars: ✭ 638 (-89.69%)
Mutual labels:  validation
Swiftcop
SwiftCop is a validation library fully written in Swift and inspired by the clarity of Ruby On Rails Active Record validations.
Stars: ✭ 544 (-91.21%)
Mutual labels:  validation
Meteor Astronomy
Model layer for Meteor
Stars: ✭ 608 (-90.17%)
Mutual labels:  validation
Pydantic
Data parsing and validation using Python type hints
Stars: ✭ 8,362 (+35.18%)
Mutual labels:  validation
Class Validator
Decorator-based property validation for classes.
Stars: ✭ 6,941 (+12.2%)
Mutual labels:  validation
Marshmallow
A lightweight library for converting complex objects to and from simple Python datatypes.
Stars: ✭ 5,857 (-5.32%)
Mutual labels:  validation

vuelidate

Simple, lightweight model-based validation for Vue.js 2.x & 3.0

Visit Vuelidate Docs for detailed instructions.

Sponsors

Silver

Storyblok

Bronze

Vue Mastery logo

Installation

You can use Vuelidate just by itself, but we suggest you use it along @vuelidate/validators, as it gives a nice collection of commonly used validators.

Vuelidate supports both Vue 3.0 and Vue 2.x

npm install @vuelidate/core @vuelidate/validators
# or
yarn add @vuelidate/core @vuelidate/validators

Usage with Options API

To use Vuelidate with the Options API, you just need to return an empty Vuelidate instance from setup.

Your validation state lives in the data and the rules are in validations function.

import { email, required } from '@vuelidate/validators'
import { useVuelidate } from '@vuelidate/core'

export default {
  name: 'UsersPage',
  data: () => ({
    form: {
      name: '',
      email: ''
    }
  }),
  setup: () => ({ v$: useVuelidate() }),
  validations () {
    return {
      form: {
        name: { required },
        email: { required, email }
      }
    }
  }
}

Usage with Composition API

To use Vuelidate with the Composition API, you need to provide it a state and set of validation rules, for that state.

The state can be a reactive object or a collection of refs.

import { reactive } from 'vue' // or '@vue/composition-api' in Vue 2.x
import { useVuelidate } from '@vuelidate/core'
import { email, required } from '@vuelidate/validators'

export default {
  setup () {
    const state = reactive({
      name: '',
      emailAddress: ''
    })
    const rules = {
      name: { required },
      emailAddress: { required, email }
    }

    const v$ = useVuelidate(rules, state)

    return { state, v$ }
  }
}

Providing global config to your Vuelidate instance

You can provide global configs to your Vuelidate instance using the third parameter of useVuelidate or by using the validationsConfig. These config options are used to change some core Vuelidate functionality, like $autoDirty, $lazy, $scope and more. Learn all about them in Validation Configuration.

Config with Options API

<script>
import { useVuelidate } from '@vuelidate/core'

export default {
  data () {
    return { ...state }
  },
  validations () {
    return { ...validations }
  },
  setup: () => ({ v$: useVuelidate() }),
  validationConfig: {
    $lazy: true,
  }
}
</script>

Config with Composition API

import { reactive } from 'vue' // or '@vue/composition-api' in Vue 2.x
import { useVuelidate } from '@vuelidate/core'
import { email, required } from '@vuelidate/validators'

export default {
  setup () {
    const state = reactive({})
    const rules = {}
    const v$ = useVuelidate(rules, state, { $lazy: true })

    return { state, v$ }
  }
}

The validation object, aka v$ object

interface ValidationState {
  $dirty: false, // validations will only run when $dirty is true
  $touch: Function, // call to turn the $dirty state to true
  $reset: Function, // call to turn the $dirty state to false
  $errors: [], // contains all the current errors { $message, $params, $pending, $invalid }
  $error: false, // true if validations have not passed
  $invalid: false, // as above for compatibility reasons
  // there are some other properties here, read the docs for more info
}

Validations rules are on by default

Validation in Vuelidate 2 is by default on, meaning validators are called on initialisation, but an error is considered active, only after a field is dirty, so after $touch() is called or by using $model.

If you wish to make a validation lazy, meaning it only runs validations once it a field is dirty, you can pass a { $lazy: true } property to Vuelidate. This saves extra invocations for async validators as well as makes the initial validation setup a bit more performant.

const v = useVuelidate(rules, state, { $lazy: true })

Resetting dirty state

If you wish to reset a form's $dirty state, you can do so by using the appropriately named $reset method. For example when closing a create/edit modal, you dont want the validation state to persist.

<app-modal @closed="v$.$reset()">
<!-- some inputs  -->
</app-modal>

Displaying error messages

The validation state holds useful data, like the invalid state of each property validator, along with extra properties, like an error message or extra parameters.

Error messages come out of the box with the bundled validators in @vuelidate/validators package. You can check how change those them over at the Custom Validators page

The easiest way to display errors is to use the form's top level $errors property. It is an array of validation objects, that you can iterate over.

<p
  v-for="(error, index) of $v.$errors"
  :key="index"
>
<strong>{{ error.$validator }}</strong>
<small> on property</small>
<strong>{{ error.$property }}</strong>
<small> says:</small>
<strong>{{ error.$message }}</strong>
</p>

You can also check for errors on each form property:

<p
  v-for="(error, index) of $v.name.$errors"
  :key="index"
>
<!-- Same as above -->
</p>

For more info, visit the Vuelidate Docs.

Development

To test the package run

# install dependencies
yarn install

# create bundles.
yarn build

# Create docs inside /docs package
yarn dev

# run unit tests for entire monorepo
yarn test:unit

# You can also run for same command per package

Contributors

Current

Emeriti

Here we honor past contributors who have been a major part on this project.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].