Last active
June 14, 2017 13:08
-
-
Save johanmendezb/4fdb6e094dd7092f361d05e5a598cf73 to your computer and use it in GitHub Desktop.
validator library
This file contains hidden or 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 expect = require('chai').expect | |
var Validator = require('../lib/validator') | |
var is = require('is_js') | |
describe('Validator validate :)', function() { | |
it('should return the object if passes', function() { | |
var values = {email: '[email protected]'} | |
var schema = { | |
email: [ | |
{valid: (v) => (v.indexOf('@') != -1), error: 'esto no es un correo'}, | |
] | |
} | |
var validator = new Validator(values, schema) | |
var result = validator.validate() | |
expect(result).to.be.empty | |
}) | |
it('should work with a library', function() { | |
var values = {email: ''} | |
var schema = { | |
email: [ | |
{valid: is.not.empty, error: 'esto es requerido'} | |
] | |
} | |
var validator = new Validator(values, schema) | |
var result = validator.validate() | |
expect(result).to.have.property('email', 'esto es requerido') | |
}) | |
it('should work with multiple values', function() { | |
var values = { | |
terms_of_service: false, | |
user: { | |
"name": 12342 | |
}, | |
company: { | |
"email": "hola", | |
"name": 12342 | |
} | |
} | |
var schema = { | |
terms_of_service: [{valid: is.not.truthy, error: 'debes aceptar los terminos ya tu sa`'}], | |
email: [{valid: is.not.empty, error: 'esto es requerido'}], | |
name: [{valid: is.not.string, error: 'debe ser un string'}] | |
} | |
var validator = new Validator(values, schema) | |
var result = validator.validate() | |
expect(result)// to match the same keys from the values object | |
}) | |
}) |
This file contains hidden or 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('lodash') | |
errorObj = function(values, errors) { | |
_.forEach(values, function(value, key) { | |
if (typeof(value) === 'object') { | |
_.forEach(value, function() { | |
errors[key] = errorObj(value, {}) | |
}) | |
} else { | |
_.setWith(errors, key, null) | |
} | |
}) | |
return errors | |
} | |
var Validator = function(values, schema) { | |
this.values = values | |
this.schema = schema | |
this.errors = errorObj(values, {}) | |
console.log('the object values formated', this.errors) | |
}; | |
var getValue = function(values, schemaKey) { | |
console.log('values', values) | |
_.forEach(values, function(value, key) { | |
if (typeof(value) === 'object') { | |
_.forEach(value , function(n_value, n_key) { | |
var obj = {} | |
console.log('---------value: ', value) | |
obj[n_key] = n_value | |
getValue(obj, schemaKey) | |
}) | |
} else { | |
return values[schemaKey] | |
} | |
}) | |
} | |
Validator.prototype.validate = function() { | |
var errors = this.errors | |
var schema = this.schema | |
for (var key in schema) { | |
var value = getValue(this.values, key) | |
var validations = schema[key] | |
for (var schemaValidationValue of validations) { | |
if (!value) { | |
errors[key] = 'undefined, this shouldnt be here' | |
} | |
if (!schemaValidationValue.valid(value) ){ | |
errors[key] = schemaValidationValue.error | |
} | |
} | |
} | |
return errors; | |
}; | |
module.exports = Validator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment