validator/validator.test.js
2020-06-10 14:13:34 +00:00

44 lines
952 B
JavaScript

/* eslint-disable no-undef */
const validator = require('./index.js')
describe('test validator validate', () => {
test('validate test 1', () => {
const data = {
field: 'string'
}
const schema = {
field: validator.string().required()
}
validator.validate(data, schema)
})
test('validate test 2', () => {
const data = {
wrong: 1
}
const schema = {
wrong: validator.string().required()
}
expect(() => validator.validate(data, schema)).toThrow()
})
test('validate test 3', () => {
const data = {
str: '123',
strWithPattern: 'asd',
objWithChild: {
name: 'asd'
}
}
const schema = {
str: validator.string().required(),
strWithPattern: validator.string().pattern(/^asd$/).required(),
objWithChild: validator.object({ name: validator.string().required() }).required()
}
validator.validate(data, schema)
})
})