Skip to content

Commit 660aa13

Browse files
committed
Update length validator, now it supports arrays
1 parent f910436 commit 660aa13

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

VueForm/validators/length.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,31 @@ export default function length(options, customMessage = {}) {
1111
const { equals, min, max } = options
1212
invariant([equals, min, max].some(x => !isNil(x)), 'equals, min or max should be provided')
1313

14-
return (value, name) => {
14+
return (rawValue, name) => {
15+
const cleanValue = isNil(rawValue) ? '' : rawValue
16+
const value = Array.isArray(cleanValue) ? cleanValue : String().trim()
17+
1518
if (equals) {
16-
return String(value).trim().length !== equals
17-
? customMessage.equals || `${startCase(name)} should have ${equals} characters`
19+
return value.length !== equals
20+
? customMessage.equals ||
21+
customMessage ||
22+
`${startCase(name)} should have ${equals} characters`
1823
: undefined
1924
}
2025

2126
if (min) {
22-
return String(value).trim().length < min
23-
? customMessage.min || `${startCase(name)} should have at least ${min} characters`
27+
return value.length < min
28+
? customMessage.min ||
29+
customMessage ||
30+
`${startCase(name)} should have at least ${min} characters`
2431
: undefined
2532
}
2633

2734
if (max) {
28-
return String(value).trim().length > max
29-
? customMessage.max || `${startCase(name)} may have maximum ${max} characters`
35+
return value.length > max
36+
? customMessage.max ||
37+
customMessage ||
38+
`${startCase(name)} may have maximum ${max} characters`
3039
: undefined
3140
}
3241

0 commit comments

Comments
 (0)