1
0
mirror of synced 2024-11-22 05:16:05 +03:00

Adds pattern as an attribute and adds number as a rule for validation (#11)

This commit is contained in:
Chris Ellinger 2018-11-20 11:51:32 -05:00 committed by Justin Schroeder
parent 8bc3ff3dae
commit 9785e5fb26
7 changed files with 1391 additions and 1360 deletions

View File

@ -159,6 +159,7 @@ Rule | Arguments
required | *none*
email | *none*
confirmed | confirmation field
number | *none*
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

6
dist/index.js vendored

File diff suppressed because one or more lines are too long

2706
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -192,6 +192,10 @@ export default {
type: [String, Number, Boolean],
default: () => false
},
pattern: {
type: [String, Number, Boolean],
default: () => false
},
minlength: {
type: [String, Number, Boolean],
default: () => false
@ -333,7 +337,7 @@ export default {
return show
},
attributes () {
return ['min', 'max', 'minlength', 'maxlength', 'placeholder', 'id', 'multiple']
return ['min', 'max', 'minlength', 'maxlength', 'placeholder', 'id', 'multiple', 'pattern']
.filter(prop => this[prop] !== false)
.reduce((attributes, attr) => {
attributes[attr] = this[attr]

View File

@ -2,5 +2,6 @@ export default {
required: ({label, value}) => `${label} is required`,
email: ({label, value}) => `${label} is invalid.`,
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.`
}

View File

@ -8,6 +8,15 @@ export default {
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
* @param {Object} field

View File

@ -12,6 +12,22 @@ test('test required rule failure', async t => {
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 => {
let v = await rules.required({field: 'name', value: [], error, label: 'xyz'})
t.is('namexyz', v)