A simple and flexible validation plugin for vue 3.
npm install vue-jvalidate
// main.js
import jvalidate from 'vue-jvalidate';
app.use(jvalidate);
// MyForm.vue
const jvalidate = inject('jvalidate');
The return value will be a Map object with key and value pairs equal to the field name and error message of any failed validations. If everything passes, it will be an empty map.
// MyForm.vue
const errors = ref(new Map());
const fields = ref({
name: '',
email: '',
});
const validation = {
name: ['required'],
email: ['required', 'email']
}
function onSubmit() {
const errors.value = jvalidate(fields.value, validation)
if (errors.value.size) {
// Do something with the errors
return;
}
}
The return value will be a Map object with key and value pairs equal to the field name and error message of any failed validations. If everything passes, it will be an empty map.
Nested objects keys will joined with a colon.
// Top level error
<div v-if="errors.has('email')">
{{ errors.get('email') }}
</div>
// Nested error
<div v-if="errors.has('address:city')">
{{ errors.get('address:city') }}
</div>
This is an object of the data you wish to validate
const fields = ref({
name: '',
email: '',
message: '',
})
The validation object contains the validation rules for the values in the field object. It should mirror the schema of your fields. You can omit fields that don't require any validation.
The value of each field should be a array of validators.
A validator is an object with two properties:
- rule: a function that takes a value and returns true or false
- message: a string to return if the field fails the rule (i.e. it returns false)
# Require 20 characters
{
rule: (value) => value.length >= 20,
message: 'A minimum of 20 characters is required'
}
const validation = {
name: [{
rule: (value) => value ? true : false,
message: 'This field is required'
}],
email: [{
rule: (value) => value ? true : false,
message: 'This field is required'
}, {
rule: (value) => value.match(/^(.+)@(.+)$/),
message: 'Please enter a valid email'
}]
}
The example above will:
- Check that name field is populated
- Check that email field is populated
- If email is populated, check that email field matches the regex pattern
The validation for each field will stop at the first failure.
Instead of an object, you can also pass a string that corresponds with the name of a pre-defined validator.
const validation = {
name: ['required'],
email: ['required', 'email']
}
The plugin contains a handful of pre-defined validators:
required: {
rule: (value) => value ? true : false,
message: 'This field is required'
},
email: {
rule: (value) => value.match(/^(.+)@(.+)$/),
message: 'Invalid email'
},
phone: {
rule: (value) => value.match(/^\(\d{3}\)\s\d{3}-\d{4}$/),
message: 'Invalid phone number'
},
zipCode: {
rule: (value) => value.match(/^\d{5}$/),
message: 'Invalid zip code'
}
You can merge your own list of validators with the pre-packaged ones in the options object when registering the plugin.
// main.js
import { myValidators } from '@/helpers/validators';
import { jvalidate } from 'vue-jvalidate';
app.use(jvalidate, { validators: myValidators });