Adds pattern as an attribute and adds number as a rule for validation (#11)
This commit is contained in:
parent
8bc3ff3dae
commit
9785e5fb26
@ -159,6 +159,7 @@ Rule | Arguments
|
|||||||
required | *none*
|
required | *none*
|
||||||
email | *none*
|
email | *none*
|
||||||
confirmed | confirmation field
|
confirmed | confirmation field
|
||||||
|
number | *none*
|
||||||
|
|
||||||
You can add as many validation rules as you want to each `formulate-element`,
|
You can add as many validation rules as you want to each `formulate-element`,
|
||||||
simply chain your rules with pipes `|'. Additional arguments can be passed to
|
simply chain your rules with pipes `|'. Additional arguments can be passed to
|
||||||
|
6
dist/index.js
vendored
6
dist/index.js
vendored
File diff suppressed because one or more lines are too long
2706
package-lock.json
generated
2706
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -192,6 +192,10 @@ export default {
|
|||||||
type: [String, Number, Boolean],
|
type: [String, Number, Boolean],
|
||||||
default: () => false
|
default: () => false
|
||||||
},
|
},
|
||||||
|
pattern: {
|
||||||
|
type: [String, Number, Boolean],
|
||||||
|
default: () => false
|
||||||
|
},
|
||||||
minlength: {
|
minlength: {
|
||||||
type: [String, Number, Boolean],
|
type: [String, Number, Boolean],
|
||||||
default: () => false
|
default: () => false
|
||||||
@ -333,7 +337,7 @@ export default {
|
|||||||
return show
|
return show
|
||||||
},
|
},
|
||||||
attributes () {
|
attributes () {
|
||||||
return ['min', 'max', 'minlength', 'maxlength', 'placeholder', 'id', 'multiple']
|
return ['min', 'max', 'minlength', 'maxlength', 'placeholder', 'id', 'multiple', 'pattern']
|
||||||
.filter(prop => this[prop] !== false)
|
.filter(prop => this[prop] !== false)
|
||||||
.reduce((attributes, attr) => {
|
.reduce((attributes, attr) => {
|
||||||
attributes[attr] = this[attr]
|
attributes[attr] = this[attr]
|
||||||
|
@ -2,5 +2,6 @@ export default {
|
|||||||
required: ({label, value}) => `${label} is required`,
|
required: ({label, value}) => `${label} is required`,
|
||||||
email: ({label, value}) => `${label} is invalid.`,
|
email: ({label, value}) => `${label} is invalid.`,
|
||||||
confirmed: ({label, value}) => `${label} does not match the confirmation field.`,
|
confirmed: ({label, value}) => `${label} does not match the confirmation field.`,
|
||||||
|
number: ({label, value}) => `${label} is not a number`,
|
||||||
default: ({label, value}) => `This field is invalid.`
|
default: ({label, value}) => `This field is invalid.`
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,15 @@ export default {
|
|||||||
return (!value || (Array.isArray(value) && !value.length)) ? error(...arguments) : false
|
return (!value || (Array.isArray(value) && !value.length)) ? error(...arguments) : false
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the field contains only numbers
|
||||||
|
* @param {Object} field
|
||||||
|
* @param {string} label
|
||||||
|
*/
|
||||||
|
async number ({value, error}) {
|
||||||
|
return isNaN(value) ? error(...arguments) : false
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate email addresses
|
* Validate email addresses
|
||||||
* @param {Object} field
|
* @param {Object} field
|
||||||
|
@ -12,6 +12,22 @@ test('test required rule failure', async t => {
|
|||||||
t.is('namexyz', v)
|
t.is('namexyz', v)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('test number no failure on empty', async t => {
|
||||||
|
let v = await rules.number({field: 'name', value: '', error, label: 'xyz'})
|
||||||
|
t.is(false, v)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test number failure not number', async t => {
|
||||||
|
let v = await rules.number({field: 'name', value: 't', error, label: 'xyz'})
|
||||||
|
t.is('string', typeof v)
|
||||||
|
t.is('namexyz', v)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test number is typeof number', async t => {
|
||||||
|
let v = await rules.number({field: 'name', value: '3', error, label: 'xyz'})
|
||||||
|
t.is(false, v)
|
||||||
|
})
|
||||||
|
|
||||||
test('test required rule empty array failure', async t => {
|
test('test required rule empty array failure', async t => {
|
||||||
let v = await rules.required({field: 'name', value: [], error, label: 'xyz'})
|
let v = await rules.required({field: 'name', value: [], error, label: 'xyz'})
|
||||||
t.is('namexyz', v)
|
t.is('namexyz', v)
|
||||||
|
Loading…
Reference in New Issue
Block a user