validator/types/string.test.js
Jay 46b19757bd
All checks were successful
continuous-integration/drone/push Build is passing
[feat] add string type pattern check, add object child check
2020-06-10 13:32:50 +00:00

67 lines
1.8 KiB
JavaScript

/* eslint-disable no-undef */
const TypeString = require('./string.js')
describe('test validate type schema string', () => {
function throwFunc (val) {
return () => {
if (val) throw new Error(val)
}
}
it('test string type without required', async () => {
const str = new TypeString()
throwFunc(str.validate(undefined))
})
it('test string type with required', async () => {
const str = new TypeString().required()
throwFunc(str.validate('123'))
expect(throwFunc(str.validate(undefined))).toThrow()
})
it('test string type with empty', async () => {
const str = new TypeString().empty().required()
throwFunc(str.validate(''))
})
it('test string type without empty', async () => {
const str = new TypeString().required()
throwFunc(str.validate('asd'))
expect(throwFunc(str.validate(''))).toThrow()
})
it('test string type with min length', async () => {
const str = new TypeString().required().min(2)
expect(() => new TypeString().min()).toThrow()
expect(() => new TypeString().min(false)).toThrow()
expect(() => new TypeString().max(2).min(3)).toThrow()
throwFunc(str.validate('asd'))
expect(throwFunc(str.validate('a'))).toThrow()
})
it('test string type with max length', async () => {
const str = new TypeString().required().max(2)
expect(() => new TypeString().max()).toThrow()
expect(() => new TypeString().max(false)).toThrow()
expect(() => new TypeString().min(2).max(1)).toThrow()
throwFunc(str.validate('a'))
expect(throwFunc(str.validate('asd'))).toThrow()
})
it('test string type with pattern', async () => {
const str = new TypeString().required().pattern(/^asd$/)
expect(throwFunc(str.validate('dsa'))).toThrow()
throwFunc(str.validate('asd'))
})
})