Skip to content

Commit 7d18bd3

Browse files
committed
refactor(validators): default validators
1 parent a8f826f commit 7d18bd3

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/mixins/abstractField.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function getValidator (validator) {
88
if (validators[validator] === undefined) throw new Error('Invalid validator: ' + validator)
99
return validators[validator]
1010
}
11-
return null
11+
throw new Error('Couldn\'t get validator for: ' + validator)
1212
}
1313

1414
export default {
@@ -53,7 +53,7 @@ export default {
5353
let results = []
5454

5555
if (!this.isDisabled && this.field.validator && !this.isReadOnly) {
56-
const fieldValidators = []
56+
const fieldValidators = [ ...this.defaultValidators ]
5757

5858
if (Array.isArray(this.field.validator)) {
5959
/** Retrieve actual validators for every given validator in Array */
@@ -64,11 +64,6 @@ export default {
6464
fieldValidators.push(getValidator(this.field.validator))
6565
}
6666

67-
/** Always include 'required' validator, whenever the field is required */
68-
if (this.isRequired && !fieldValidators.includes(validators.required)) {
69-
fieldValidators.push(validators.required)
70-
}
71-
7267
fieldValidators.forEach(validator => {
7368
const isValid = validator(this.currentModelValue, this.field, this.model, this)
7469
if (!isValid) results.push(getMessage(validator.name))
@@ -153,6 +148,28 @@ export default {
153148

154149
computed: {
155150

151+
/**
152+
* Compute all validators that should be present by default.
153+
*
154+
* @returns {*[]}
155+
*/
156+
defaultValidators () {
157+
const fieldValidators = []
158+
159+
if (this.isRequired && !fieldValidators.includes(validators.required)) {
160+
fieldValidators.push(validators.required)
161+
}
162+
163+
if (this.field.min) {
164+
fieldValidators.push(validators.minLength)
165+
}
166+
167+
if (this.field.max) {
168+
fieldValidators.push(validators.maxLength)
169+
}
170+
return fieldValidators
171+
},
172+
156173
/**
157174
* Compute the current value of this field by accessing the form's model object.
158175
*

src/validators/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ export default {
1515
return !!(fieldComponent.isRequired && isNotEmpty(value))
1616
},
1717

18+
/**
19+
* Check if field value is the minimum provided length
20+
*
21+
* @param {any} value - The current value of the field.
22+
* @param {Object} field - The field as specified in the form's schema.
23+
* @param {Object} _model - The form model.
24+
* @param {Component} _fieldComponent - Field's Vue Component.
25+
* @returns {boolean} - Returns 'true' if the field is required and the value is not empty, otherwise false.
26+
*/
27+
minLength (value, field, _model, _fieldComponent) {
28+
return field.min && value.length >= field.min
29+
},
30+
31+
/**
32+
* Check if field value is the maximum provided length
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 required and the value is not empty, otherwise false.
39+
*/
40+
maxLength (value, field, _model, _fieldComponent) {
41+
return field.max && value.length <= field.max
42+
},
43+
1844
/**
1945
* Checks if the field's value is of type string.
2046
*

0 commit comments

Comments
 (0)