Last active
July 16, 2018 14:53
-
-
Save ilearnio/91d7b043111ffdec105930f48189c408 to your computer and use it in GitHub Desktop.
Simple schema validation
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
/** | |
* Simple object validation | |
*/ | |
const typeOf = require('just-typeof') | |
const validateEmail = require('./utils/helpers/strings/validateEmail') | |
const RULES = { | |
/** | |
* Type of the value | |
* @param {mixed} value | |
* @param {String} type One of the following: | |
* string, number, date, email, array, object, boolean | |
* @returns {Boolean} | |
*/ | |
type (value, type) { | |
if (type === 'email') { | |
return validateEmail(value) | |
} | |
return typeOf(value) === type | |
}, | |
/** | |
* Minimum string length | |
*/ | |
min (value, minNum) { | |
return value.length >= minNum | |
}, | |
/** | |
* Maximal string length | |
*/ | |
max (value, maxNum) { | |
return value.length <= maxNum | |
}, | |
/** | |
* Test against regular expression | |
*/ | |
pattern (value, regexp) { | |
return regexp.test(value) | |
}, | |
/** | |
* Test against custom function handler | |
*/ | |
test (value, handler) { | |
return handler(value) | |
} | |
} | |
const validator = { | |
/** | |
* Validate a single value agains rules | |
*/ | |
validateValue (value, rules, fieldName = null) { | |
const results = Object.keys(rules).map(ruleName => { | |
const rule = rules[ruleName] | |
const ruleTestHendler = RULES[ruleName] | |
let isValid | |
if (ruleName === 'type') { | |
isValid = ruleTestHendler(value, rule.value || rule) | |
} else if (ruleName === 'test') { | |
isValid = ruleTestHendler(value, rule.handler) | |
} else { | |
isValid = ruleTestHendler(value, rule.value) | |
} | |
if (isValid) { | |
return true | |
} else { | |
let { message, code } = rule | |
if (!message) { | |
message = 'Invalid field' | |
if (fieldName) message += ` "${fieldName}"` | |
} | |
return { message, code } | |
} | |
}) | |
const errors = results.filter(val => val !== true) | |
return errors.length > 0 ? errors : true | |
}, | |
/** | |
* Validate object agains schema | |
* @return {mixed} | |
*/ | |
validate (object, rules) { | |
let isValid = true | |
const errors = {} | |
Object.keys(object).forEach(fieldName => { | |
const value = object[fieldName] | |
const fieldRules = rules[fieldName] | |
const result = validator.validateValue(value, fieldRules, fieldName) | |
if (result !== true) { | |
isValid = false | |
errors[fieldName] = result | |
} | |
}) | |
return isValid ? true : errors | |
} | |
} | |
module.exports = validator |
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
import validator from './validator' | |
// | |
// Example Usage | |
// | |
const userSchema = { | |
name: { | |
type: 'string', | |
pattern: { | |
value: /^[^\d\s]+ [^\d\s]+/, | |
message: 'Invalid full name.', | |
code: exports.AUTH_CODE_INVALID_NAME | |
} | |
}, | |
username: { | |
type: 'string', | |
pattern: { | |
value: /^[a-zA-Z][\w-]+[a-zA-Z0-9]$/, | |
message: 'Invalid username.', | |
code: exports.AUTH_CODE_INVALID_USERNAME | |
} | |
}, | |
email: { | |
type: { | |
value: 'email', | |
message: 'Invalid email format.', | |
code: exports.AUTH_CODE_INVALID_EMAIL_FORMAT | |
} | |
}, | |
password: { | |
type: 'string', | |
min: { | |
value: 8, | |
message: 'Password should contain at least 8 characters.', | |
code: exports.AUTH_CODE_PASSWORD_TOO_SHORT | |
}, | |
max: { | |
value: 50, | |
message: 'Password shouldn\'t contain more than 50 characters.', | |
code: exports.AUTH_CODE_PASSWORD_TOO_LONG | |
}, | |
pattern: { | |
value: /^[A-Za-z0-9!@#$%^&*()\-_=+[{\]};:'"\\|~,<.>/?]+$/, | |
message: 'Password contains invalid characters.', | |
code: exports.AUTH_CODE_BAD_PASSWORD | |
} | |
} | |
} | |
const validateUserData = (data) => { | |
return validator.validate(data, userSchema) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment