2020-06-03 15:49:04 +00:00
|
|
|
/* 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()
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(str.validate(undefined))
|
2020-06-03 15:49:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('test string type with required', async () => {
|
|
|
|
const str = new TypeString().required()
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(str.validate('123'))
|
2020-06-03 15:49:04 +00:00
|
|
|
|
|
|
|
expect(throwFunc(str.validate(undefined))).toThrow()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('test string type with empty', async () => {
|
|
|
|
const str = new TypeString().empty().required()
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(str.validate(''))
|
2020-06-03 15:49:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('test string type without empty', async () => {
|
|
|
|
const str = new TypeString().required()
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(str.validate('asd'))
|
2020-06-03 15:49:04 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(str.validate('asd'))
|
2020-06-03 15:49:04 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2020-06-10 13:32:50 +00:00
|
|
|
throwFunc(str.validate('a'))
|
2020-06-03 15:49:04 +00:00
|
|
|
|
|
|
|
expect(throwFunc(str.validate('asd'))).toThrow()
|
|
|
|
})
|
2020-06-10 13:32:50 +00:00
|
|
|
|
|
|
|
it('test string type with pattern', async () => {
|
|
|
|
const str = new TypeString().required().pattern(/^asd$/)
|
|
|
|
|
|
|
|
expect(throwFunc(str.validate('dsa'))).toThrow()
|
|
|
|
|
|
|
|
throwFunc(str.validate('asd'))
|
|
|
|
})
|
2020-06-03 15:49:04 +00:00
|
|
|
})
|