Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var errors = new ValidationError()
, schema = this
definition = definition || schema.getActiveDefinition(data)
options = options || {}
if(!util.isExistent(data)) {
if(!definition.required) return true
if(util.isNull(data)) {
throw new ValidationError('Required but got \'NULL\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isNaN(data)) {
throw new ValidationError('Required but got \'NaN\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isUndefined(data)) {
throw new ValidationError('Required but got \'undefined\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isEmptyStr(data)) {
throw new ValidationError('Required but got \'empty string\'', Schema.ErrorCodes.REQUIRED)
}
throw new ValidationError('Required failed test for existence', Schema.ErrorCodes.REQUIRED)
}
if(!util.typeMatch(data, definition.type)) {
// build up the error text and throw at end
// 'expected'
throw new ValidationError('expected '+definition.type.name)
}
if(util.isObject(data)) {
Object.keys(data).forEach(function(key) {
try {
if(util.isNaN(data)) {
throw new ValidationError('Required but got \'NaN\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isUndefined(data)) {
throw new ValidationError('Required but got \'undefined\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isEmptyStr(data)) {
throw new ValidationError('Required but got \'empty string\'', Schema.ErrorCodes.REQUIRED)
}
throw new ValidationError('Required failed test for existence', Schema.ErrorCodes.REQUIRED)
}
if(!util.typeMatch(data, definition.type)) {
// build up the error text and throw at end
// 'expected'
throw new ValidationError('expected '+definition.type.name)
}
if(util.isObject(data)) {
Object.keys(data).forEach(function(key) {
try {
schema.validate(data[key], options, definition.properties[key])
} catch(e) {
util.addNestedError(errors, e, key)
}
})
definition.required && definition.required.forEach(function(key) {
if(!util.isExistent(data[key]))
errors.addError(new ValidationError('required'))
})
}
if(!util.isExistent(data)) {
if(!definition.required) return true
if(util.isNull(data)) {
throw new ValidationError('Required but got \'NULL\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isNaN(data)) {
throw new ValidationError('Required but got \'NaN\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isUndefined(data)) {
throw new ValidationError('Required but got \'undefined\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isEmptyStr(data)) {
throw new ValidationError('Required but got \'empty string\'', Schema.ErrorCodes.REQUIRED)
}
throw new ValidationError('Required failed test for existence', Schema.ErrorCodes.REQUIRED)
}
if(!util.typeMatch(data, definition.type)) {
// build up the error text and throw at end
// 'expected'
throw new ValidationError('expected '+definition.type.name)
}
if(util.isObject(data)) {
Object.keys(data).forEach(function(key) {
try {
schema.validate(data[key], options, definition.properties[key])
} catch(e) {
util.addNestedError(errors, e, key)
}
})
Schema.prototype.validate = function(data, options, definition) {
var errors = new ValidationError()
, schema = this
definition = definition || schema.getActiveDefinition(data)
options = options || {}
if(!util.isExistent(data)) {
if(!definition.required) return true
if(util.isNull(data)) {
throw new ValidationError('Required but got \'NULL\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isNaN(data)) {
throw new ValidationError('Required but got \'NaN\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isUndefined(data)) {
throw new ValidationError('Required but got \'undefined\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isEmptyStr(data)) {
throw new ValidationError('Required but got \'empty string\'', Schema.ErrorCodes.REQUIRED)
}
throw new ValidationError('Required failed test for existence', Schema.ErrorCodes.REQUIRED)
}
if(!util.typeMatch(data, definition.type)) {
// build up the error text and throw at end
// 'expected'
definition = definition || schema.getActiveDefinition(data)
options = options || {}
if(!util.isExistent(data)) {
if(!definition.required) return true
if(util.isNull(data)) {
throw new ValidationError('Required but got \'NULL\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isNaN(data)) {
throw new ValidationError('Required but got \'NaN\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isUndefined(data)) {
throw new ValidationError('Required but got \'undefined\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isEmptyStr(data)) {
throw new ValidationError('Required but got \'empty string\'', Schema.ErrorCodes.REQUIRED)
}
throw new ValidationError('Required failed test for existence', Schema.ErrorCodes.REQUIRED)
}
if(!util.typeMatch(data, definition.type)) {
// build up the error text and throw at end
// 'expected'
throw new ValidationError('expected '+definition.type.name)
}
if(util.isObject(data)) {
Object.keys(data).forEach(function(key) {
try {
schema.validate(data[key], options, definition.properties[key])
} catch(e) {
util.addNestedError(errors, e, key)
Schema.prototype.validate = function(data, options, definition) {
var errors = new ValidationError()
, schema = this
definition = definition || schema.getActiveDefinition(data)
options = options || {}
if(!util.isExistent(data)) {
if(!definition.required) return true
if(util.isNull(data)) {
throw new ValidationError('Required but got \'NULL\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isNaN(data)) {
throw new ValidationError('Required but got \'NaN\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isUndefined(data)) {
throw new ValidationError('Required but got \'undefined\'', Schema.ErrorCodes.REQUIRED)
}
if(util.isEmptyStr(data)) {
throw new ValidationError('Required but got \'empty string\'', Schema.ErrorCodes.REQUIRED)
}
throw new ValidationError('Required failed test for existence', Schema.ErrorCodes.REQUIRED)
}
if(!util.typeMatch(data, definition.type)) {
// build up the error text and throw at end
// 'expected'
throw new ValidationError('expected '+definition.type.name)
}
it('should validate arrays', function() {
var schema = new Schema([String])
try {
schema.validate(['hi', 'there', 1])
throw new Error('Did not throw')
} catch(e) {
e.should.be.instanceof(ValidationError)
}
schema.validate(['a', 'b', 'c']).should.be.true
})
it('should not allow keys not defined in the schema', function() {
it('should check values against validators', function() {
var schema = new Schema({ type:Number, validators:[min(18)] })
try {
schema.validate(16)
throw new Error('Did not throw')
} catch(e) {
e.should.be.instanceof(ValidationError)
}
schema.validate(21).should.be.true
function min(min) {
return function(value) {
if(value < min)
throw new ValidationError('must be '+min+' or greater')
}
}
})
it('should validate objects', function() {
return function(value) {
if(value < min)
throw new ValidationError('must be '+min+' or greater')
}
}
definition.required && definition.required.forEach(function(key) {
if(!util.isExistent(data[key]))
errors.addError(new ValidationError('required'))
})
}