vue-jvalidate
v1.0.8
Published
Vue 3 validation plugin
Downloads
5
Maintainers
Readme
A simple and flexible validation plugin for vue 3.
Installation
npm install vue-jvalidate
Usage
1. Import and register plugin
// main.js
import jvalidate from 'vue-jvalidate';
app.use(jvalidate);
2. Inject into component
// MyForm.vue
const jvalidate = inject('jvalidate');
3. Call jvalidate() with field and validation objects
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;
}
}
Handling errors
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.
Displaying error messages in your template
// 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>
Field Object
This is an object of the data you wish to validate
const fields = ref({
name: '',
email: '',
message: '',
})
Validation Object
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.
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)
Example validator
# Require 20 characters
{
rule: (value) => value.length >= 20,
message: 'A minimum of 20 characters is required'
}
Validator usage
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.
Named Validators
Instead of an object, you can also pass a string that corresponds with the name of a pre-defined validator.
Using named validators
const validation = {
name: ['required'],
email: ['required', 'email']
}
The plugin contains a handful of pre-defined validators:
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'
}
Adding your own named validators
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 });