How to use the common-errors.ValidationError function in common-errors

To help you get started, we’ve selected a few common-errors examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mlrawlings / schema / lib / schema.js View on Github external
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 {
github mlrawlings / schema / lib / schema.js View on Github external
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'))
		})
	}
github mlrawlings / schema / lib / schema.js View on Github external
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)
			}
		})
github mlrawlings / schema / lib / schema.js View on Github external
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' 
github mlrawlings / schema / lib / schema.js View on Github external
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)
github mlrawlings / schema / lib / schema.js View on Github external
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)
	}
github mlrawlings / schema / test / schema.js View on Github external
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() {
github mlrawlings / schema / test / schema.js View on Github external
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() {
github mlrawlings / schema / test / schema.js View on Github external
return function(value) {
					if(value < min)
						throw new ValidationError('must be '+min+' or greater')
				}
			}
github mlrawlings / schema / lib / schema.js View on Github external
definition.required && definition.required.forEach(function(key) {
			if(!util.isExistent(data[key]))
				errors.addError(new ValidationError('required'))
		})
	}