Created
March 22, 2017 10:25
-
-
Save ericat/70473461aed38b95aeb3804329388102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('underscore'); // es6 can import what we need | |
// will return a function | |
function required(field, value) { | |
return value || 'Value is required'; | |
} | |
function minLen(value) { | |
return value.length >= 6 ? value : 'Value is too short'; | |
} | |
function maxLen(value) { | |
return value.length <= 25 ? value : 'Value is too long'; | |
} | |
function isBool(value) { | |
return typeof value === 'boolean'; | |
} | |
var validate = _.compose( | |
minLen, | |
maxLen, | |
required | |
); | |
console.log(validate('firstName', 'ericat')); | |
var validationRules = { | |
firstName: [required, minLen, MaxLen], | |
billingAddress: [required, isBool], | |
password: [required, minLen, MaxLen], | |
confirmPass: [required, mustMatch] | |
}; | |
// Apply rules, pseudo code | |
export const validateFields = (field, value, ...validations) => { | |
for (let v of validations) { | |
let errMess = v(field, value); | |
if (errMess) { | |
return { | |
[field]: errMessage(field) | |
}; | |
} | |
} | |
return field; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment