1
0
mirror of synced 2024-11-29 08:36:12 +03:00
vue-formulario/src~v1/rules.js

42 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-01-31 01:21:21 +03:00
export default {
/**
* Validate a required field.
* @param {Object} field
* @param {string} label
*/
async required ({value, error}) {
2018-02-01 01:20:29 +03:00
return (!value || (Array.isArray(value) && !value.length)) ? error(...arguments) : false
2018-01-31 01:21:21 +03:00
},
/**
* Validates the field contains only numbers
* @param {Object} field
* @param {string} label
*/
async number ({value, error}) {
return isNaN(value) ? error(...arguments) : false
},
2018-01-31 01:21:21 +03:00
/**
* Validate email addresses
* @param {Object} field
* @param {string} label
*/
async email ({value, error}) {
2018-01-31 01:21:21 +03:00
// eslint-disable-next-line
var re = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;
return (value && !re.test(value.toLowerCase())) ? error(...arguments) : false
},
/**
* Check if a particular field is matches another field in the form.
* @param {Object} field
* @param {string} label
* @param {string} confirmField (uses `${field}_confirmation` by default)
*/
async confirmed ({field, value, error, values}, confirmField) {
2018-01-31 01:21:21 +03:00
confirmField = confirmField || `${field}_confirmation`
return (value && value !== values[confirmField]) ? error(...arguments) : false
}
}