1.0.3 • Published 6 years ago
ns-validate v1.0.3
Validate
Simple validation
const validate = require('ns-validate').validate
describe('validate', () => {
it('should validate values', () => {
expect(validate.isString('hello')).toEqual(true)
expect(validate.isStringNotEmpty('hello')).toEqual(true)
expect(validate.isDate(new Date())).toEqual(true)
expect(validate.isObject({})).toEqual(true)
expect(validate.isInteger(123)).toEqual(true)
expect(validate.isArray([1, 2, 3])).toEqual(true)
})
})
Accumulating validator
const Validator = require('ns-validate').Validator
describe('Validator', function () {
it('should accumulate errors', () => {
const validator = new Validator()
validator.string(123, 'f1')
validator.stringNotEmpty('', 'f2')
validator.date('today', 'f3')
validator.object(undefined, 'f4')
validator.integer('one', 'f5')
validator.array(5, 'f6')
expect(validator.errors).toEqual([
{error: 'Not a string', field: 'f1', value: 123},
{error: 'Not a non-empty string', field: 'f2', value: ''},
{error: 'Not a date', field: 'f3', value: 'today'},
{error: 'Not an object', field: 'f4', value: undefined},
{error: 'Not an integer', field: 'f5', value: 'one'},
{error: 'Not an array', field: 'f6', value: 5}])
})
})
})