Skip to content

Instantly share code, notes, and snippets.

@ericat
Created March 22, 2017 10:25
Show Gist options
  • Save ericat/70473461aed38b95aeb3804329388102 to your computer and use it in GitHub Desktop.
Save ericat/70473461aed38b95aeb3804329388102 to your computer and use it in GitHub Desktop.
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