Skip to content

Commit e866bc1

Browse files
committed
feat(validators): add more validators
- Added email validator; - Added phone nr. E164 and E123; - Added number validator; - Added messages; - Added mobile phone NL.
1 parent be6c7d4 commit e866bc1

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/validators/index.js

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ export default {
66
* Checks if a field meets the 'required' validation criteria.
77
*
88
* @param {any} value - The current value of the field.
9-
* @param {Object} field - The field as specified in the form's schema.
10-
* @param {Object} model - The form model.
9+
* @param {Object} _field - The field as specified in the form's schema.
10+
* @param {Object} _model - The form model.
1111
* @param {Component} fieldComponent - Field's Vue Component.
1212
* @returns {boolean} - Returns 'true' if the field is required and the value is not empty, otherwise false.
1313
*/
14-
required(value, field, model, fieldComponent) {
14+
required (value, _field, _model, fieldComponent) {
1515
return !!(fieldComponent.isRequired && isNotEmpty(value))
1616
},
1717

@@ -24,8 +24,68 @@ export default {
2424
* @param {Component} _fieldComponent - Field's Vue Component.
2525
* @returns {boolean} - Returns 'true' if the field is required and the value is not empty, otherwise false.
2626
*/
27-
string(value, _field, _model, _fieldComponent) {
27+
string (value, _field, _model, _fieldComponent) {
2828
return isString(value)
29+
},
30+
31+
/**
32+
* Checks if the field's value is of type number.
33+
*
34+
* @param {any} value - The current value of the field.
35+
* @param {Object} _field - The field as specified in the form's schema.
36+
* @param {Object} _model - The form model.
37+
* @param {Component} _fieldComponent - Field's Vue Component.
38+
* @returns {boolean} - Returns 'true' if the field is a number.
39+
*/
40+
number (value, _field, _model, _fieldComponent) {
41+
return Number.isNaN(value)
42+
},
43+
44+
/**
45+
* Check if the field's value is of a valid e-mail address format.
46+
*
47+
* @param {any} value - The current value of the field.
48+
* @param {Object} _field - The field as specified in the form's schema.
49+
* @param {Object} _model - The form model.
50+
* @param {Component} _fieldComponent - Field's Vue Component.
51+
* @returns {boolean} - Returns 'true' if the field's format is a valid email format, otherwise false.
52+
*/
53+
email (value, _field, _model, _fieldComponent) {
54+
// eslint-disable-next-line max-len
55+
const regex = new RegExp('^([^<>()\\[\\]\\\\.,;:\\s@"]+(?:\\.[^<>()\\[\\]\\\\.,;:\\s@"]+)*|".+")@(\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}]|(?:[a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,})$', 'i')
56+
return Boolean(value.match(regex))
57+
},
58+
59+
/**
60+
* Check if a value is a phone number in E164 or E123 format.
61+
*
62+
* @param {String} value current value of the field
63+
* @param {Object} _field - The field as specified in the form's schema.
64+
* @param {Object} _model - The form model.
65+
* @param {Component} _fieldComponent - Field's Vue Component.
66+
* @returns {boolean} - Returns 'true' if value matches the format, otherwise false.
67+
*/
68+
phoneNumberE164andE123 (value, _field, _model, _fieldComponent) {
69+
const regex = new RegExp(
70+
// eslint-disable-next-line max-len
71+
'^\\+\\d{1,3}\\s\\d{2,3}\\s\\d{2,3}\\s\\d{4}|^\\+\\d{1,3}\\s\\d{1,14}(\\s\\d{1,13})?|^\\(\\d{3}\\)\\s\\d{3}\\s\\d{4}?',
72+
'i'
73+
)
74+
return Boolean(value.match(regex))
75+
},
76+
77+
/**
78+
* Check if value is a valid Dutch mobile phone number
79+
*
80+
* @param {String} value current value of the field
81+
* @param {Object} _field - The field as specified in the form's schema.
82+
* @param {Object} _model - The form model.
83+
* @param {Component} _fieldComponent - Field's Vue Component.
84+
* @returns {boolean} - Returns 'true' if value matches the format, otherwise false.
85+
*/
86+
mobilePhoneNL (value, _field, _model, _fieldComponent) {
87+
const regex = new RegExp('(\\+316[0-9]{8})|(06[0-9]{8})', 'i')
88+
return Boolean(value.match(regex))
2989
}
3090

3191

src/validators/messages.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
let messages = {
2-
required: 'Value is required'
2+
required: 'Field is required',
3+
string: 'Value must be a string',
4+
number: 'Value must be a number',
5+
email: 'E-mail is invalid',
6+
phoneNumberE164andE123: 'Phone number is invalid (must be valid E164 or E123 format, e.g. +31 612345678)',
7+
mobilePhoneNL: 'Phone number is invalid (must be a valid Dutch phone number, e.g. +31612345678)'
38
}
49

510
function getMessage (validatorName) {
6-
return messages[validatorName]
11+
return messages[validatorName] || 'Field is invalid'
712
}
813

914
function setMessages (_messages) {

0 commit comments

Comments
 (0)