Gathering detailed insights and metrics for vuelidate
Gathering detailed insights and metrics for vuelidate
Gathering detailed insights and metrics for vuelidate
Gathering detailed insights and metrics for vuelidate
Simple, lightweight model-based validation for Vue.js
npm install vuelidate
@vuelidate/core@2.0.1
Published on 26 Mar 2023
@vuelidate/core@2.0.0
Published on 25 Sept 2022
@vuelidate/validators@2.0.0-alpha.31
Published on 23 Jul 2022
@vuelidate/core@2.0.0-alpha.43
Published on 23 Jul 2022
@vuelidate/core@2.0.0-alpha.41
Published on 01 May 2022
@vuelidate/validators@2.0.0-alpha.29
Published on 01 May 2022
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6,898 Stars
670 Commits
497 Forks
79 Watching
6 Branches
85 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
JavaScript (64.94%)
Vue (12.52%)
Sass (10.92%)
Pug (9.6%)
SCSS (1.35%)
HTML (0.66%)
Cumulative downloads
Total Downloads
Last day
3.9%
35,244
Compared to previous day
Last week
3.2%
184,518
Compared to previous week
Last month
12.9%
800,122
Compared to previous month
Last year
-24.7%
9,306,208
Compared to previous year
59
Simple, lightweight model-based validation for Vue.js 2.x & 3.0
Visit Vuelidate Docs for detailed instructions.
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
1npm install @vuelidate/core @vuelidate/validators 2# or 3yarn add @vuelidate/core @vuelidate/validators
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.
1import { email, required } from '@vuelidate/validators' 2import { useVuelidate } from '@vuelidate/core' 3 4export default { 5 name: 'UsersPage', 6 data: () => ({ 7 form: { 8 name: '', 9 email: '' 10 } 11 }), 12 setup: () => ({ v$: useVuelidate() }), 13 validations () { 14 return { 15 form: { 16 name: { required }, 17 email: { required, email } 18 } 19 } 20 } 21}
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
.
1import { reactive } from 'vue' // or '@vue/composition-api' in Vue 2.x 2import { useVuelidate } from '@vuelidate/core' 3import { email, required } from '@vuelidate/validators' 4 5export default { 6 setup () { 7 const state = reactive({ 8 name: '', 9 emailAddress: '' 10 }) 11 const rules = { 12 name: { required }, 13 emailAddress: { required, email } 14 } 15 16 const v$ = useVuelidate(rules, state) 17 18 return { state, v$ } 19 } 20}
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.
1<script> 2import { useVuelidate } from '@vuelidate/core' 3 4export default { 5 data () { 6 return { ...state } 7 }, 8 validations () { 9 return { ...validations } 10 }, 11 setup: () => ({ v$: useVuelidate() }), 12 validationConfig: { 13 $lazy: true, 14 } 15} 16</script>
1import { reactive } from 'vue' // or '@vue/composition-api' in Vue 2.x 2import { useVuelidate } from '@vuelidate/core' 3import { email, required } from '@vuelidate/validators' 4 5export default { 6 setup () { 7 const state = reactive({}) 8 const rules = {} 9 const v$ = useVuelidate(rules, state, { $lazy: true }) 10 11 return { state, v$ } 12 } 13}
v$
object1interface ValidationState { 2 $dirty: false, // validations will only run when $dirty is true 3 $touch: Function, // call to turn the $dirty state to true 4 $reset: Function, // call to turn the $dirty state to false 5 $errors: [], // contains all the current errors { $message, $params, $pending, $invalid } 6 $error: false, // true if validations have not passed 7 $invalid: false, // as above for compatibility reasons 8 // there are some other properties here, read the docs for more info 9}
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.
1const v = useVuelidate(rules, state, { $lazy: true })
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.
1 2<app-modal @closed="v$.$reset()"> 3<!-- some inputs --> 4</app-modal>
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.
1 2<p 3 v-for="(error, index) of v$.$errors" 4 :key="index" 5> 6<strong>{{ error.$validator }}</strong> 7<small> on property</small> 8<strong>{{ error.$property }}</strong> 9<small> says:</small> 10<strong>{{ error.$message }}</strong> 11</p>
You can also check for errors on each form property:
1<p 2 v-for="(error, index) of v$.name.$errors" 3 :key="index" 4> 5<!-- Same as above --> 6</p>
For more info, visit the Vuelidate Docs.
To test the package run
1# install dependencies 2yarn install 3 4# create bundles. 5yarn build 6 7# Create docs inside /docs package 8yarn dev 9 10# run unit tests for entire monorepo 11yarn test:unit 12 13# You can also run for same command per package
Here we honor past contributors who have been a major part on this project.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 12/28 approved changesets -- score normalized to 4
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
48 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More