Skip to content

Instantly share code, notes, and snippets.

@SouravDas25
Last active July 4, 2020 18:37
Show Gist options
  • Select an option

  • Save SouravDas25/03f2304778a6a77b8b1c9d78fb33dfa0 to your computer and use it in GitHub Desktop.

Select an option

Save SouravDas25/03f2304778a6a77b8b1c9d78fb33dfa0 to your computer and use it in GitHub Desktop.
function (college) {
const rules = {
collegeName: {
required: true,
notBlank: true
},
collegeEmail: {
required: true,
notBlank: true,
email: true
},
collegePhone: {
notBlank: true
},
city: {
required: true,
notBlank: true
},
state: {
required: true,
notBlank: true
},
tpoEmail: {
required: true,
notBlank: true,
email: true
},
tpoName: {
required: true,
notBlank: true
},
tpoPhone: {
notBlank: true
},
}
return ObjectValidator.validate(college, rules);
}
function () {
const mailRegex = /^\w+[\w-+.]*\@\w+([-.]\w+)*\.[a-zA-Z]{2,}$/;
return {
required: function (value) {
return value !== null;
},
email: function (value) {
return value.match(mailRegex);
},
notBlank: function (value) {
if (value == null)
return false;
return value.trim().length > 0;
},
getRuleValidator: function (rule) {
if (this.hasOwnProperty(rule))
return this[rule];
throw new Error("rule not found.");
},
validateValue: function (value, rules) {
for (const rule in rules) {
const ruleValidator = this.getRuleValidator(rule);
const isValid = ruleValidator(value);
if (!isValid)
return isValid;
}
return true;
},
validate: function (object, ruleCollection) {
for (const key in ruleCollection) {
if (object.hasOwnProperty(key)) {
const value = object[key];
const rules = ruleCollection[key];
const isValid = this.validateValue(value, rules);
if (!isValid)
return isValid;
}
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment