184 lines
4.4 KiB
TypeScript
184 lines
4.4 KiB
TypeScript
import { expect, test } from 'bun:test'
|
|
import s from '../Schema'
|
|
|
|
test('number enum', () => {
|
|
enum Test {
|
|
A,
|
|
B
|
|
}
|
|
// test by enum call
|
|
expect(s.enum(Test).parse(Test.A).valid).toBe(true)
|
|
// test by value call
|
|
expect(s.enum(Test).parse(0).valid).toBe(true)
|
|
// test by key call
|
|
expect(s.enum(Test).parse('A').valid).toBe(false)
|
|
})
|
|
|
|
test('string enum', () => {
|
|
enum Test {
|
|
A = 'B',
|
|
B = 'A',
|
|
C = 'D'
|
|
}
|
|
// test by enumn call
|
|
expect(s.enum(Test).parse(Test.A).valid).toBe(true)
|
|
// test by raw call
|
|
expect(s.enum(Test).parse('A').valid).toBe(true)
|
|
// test by key call
|
|
expect(s.enum(Test).parse('C').valid).toBe(false)
|
|
})
|
|
|
|
test('literal', () => {
|
|
const schema = s.literal('a')
|
|
expect(schema.parse('a').valid).toBe(true)
|
|
expect(schema.parse('b').valid).toBe(false)
|
|
})
|
|
|
|
test('nullable', () => {
|
|
const schema = s.nullable(s.string())
|
|
expect(schema.parse('a').valid).toBe(true)
|
|
expect(schema.parse(undefined).valid).toBe(true)
|
|
})
|
|
|
|
test('number', () => {
|
|
const schema = s.number()
|
|
expect(schema.parse(1).valid).toBe(true)
|
|
expect(schema.parse('1').valid).toBe(false)
|
|
|
|
let c = s.number().gt(2)
|
|
expect(c.parse(3).valid).toBe(true)
|
|
expect(c.parse(2).valid).toBe(false)
|
|
c = s.number().lt(2)
|
|
expect(c.parse(1).valid).toBe(true)
|
|
expect(c.parse(2).valid).toBe(false)
|
|
c = s.number().max(2)
|
|
expect(c.parse(1).valid).toBe(true)
|
|
expect(c.parse(2).valid).toBe(true)
|
|
expect(c.parse(3).valid).toBe(false)
|
|
c = s.number().min(2)
|
|
expect(c.parse(1).valid).toBe(false)
|
|
expect(c.parse(2).valid).toBe(true)
|
|
expect(c.parse(3).valid).toBe(true)
|
|
c = s.number().parseString()
|
|
expect(c.parse(1).valid).toBe(true)
|
|
expect(c.parse('2').valid).toBe(true)
|
|
expect(c.parse(true).valid).toBe(false)
|
|
})
|
|
|
|
test('object', () => {
|
|
const schema = s.object({
|
|
a: s.string(),
|
|
b: s.number()
|
|
})
|
|
expect(schema.parse({ a: 'a', b: 1 }).valid).toBe(true)
|
|
expect(schema.parse({ a: 'a', b: '1' }).valid).toBe(false)
|
|
})
|
|
|
|
test('string', () => {
|
|
const schema = s.string()
|
|
expect(schema.parse('1').valid).toBe(true)
|
|
expect(schema.parse(1).valid).toBe(false)
|
|
|
|
// min length
|
|
let c = s.string()
|
|
.minLength(8)
|
|
expect(c.parse('12345678').valid).toBe(true)
|
|
expect(c.parse('1234567').valid).toBe(false)
|
|
|
|
// max length
|
|
c = s.string()
|
|
.maxLength(8)
|
|
expect(c.parse('12345678').valid).toBe(true)
|
|
expect(c.parse('123456789').valid).toBe(false)
|
|
|
|
// to lower case
|
|
c = s.string()
|
|
.toCasing('lower')
|
|
expect(c.parse('HELLO WORLD').valid).toBe(true)
|
|
expect(c.parse('HELLO WORLD').object).toBe('hello world')
|
|
|
|
// to upper case
|
|
c = s.string()
|
|
.toCasing('upper')
|
|
expect(c.parse('hello world').valid).toBe(true)
|
|
expect(c.parse('hello world').object).toBe('HELLO WORLD')
|
|
|
|
// to not empty
|
|
c = s.string()
|
|
.notEmpty()
|
|
expect(c.parse('hello world').valid).toBe(true)
|
|
expect(c.parse('').valid).toBe(false)
|
|
|
|
// regex validation
|
|
c = s.string()
|
|
.regex(/llo/g)
|
|
expect(c.parse('hello world').valid).toBe(true)
|
|
expect(c.parse('hell world').valid).toBe(false)
|
|
|
|
})
|
|
|
|
test('union', () => {
|
|
const schema = s.union(s.string(), s.number())
|
|
expect(schema.parse('1').valid).toBe(true)
|
|
expect(schema.parse(1).valid).toBe(true)
|
|
expect(schema.parse(true).valid).toBe(false)
|
|
})
|
|
|
|
test('union advanced', () => {
|
|
const schema = s.union(s.object({
|
|
type: s.literal('pokemon' as const),
|
|
value: s.string()
|
|
}), s.object({
|
|
type: s.literal('pouet' as const),
|
|
value: s.number()
|
|
}))
|
|
|
|
expect(schema.parse({
|
|
type: 'pokemon',
|
|
value: 'a'
|
|
}).valid).toBe(true)
|
|
|
|
expect(schema.parse({
|
|
type: 'pouet',
|
|
value: 'a'
|
|
}).valid).toBe(false)
|
|
|
|
expect(schema.parse({
|
|
type: 'pouet',
|
|
value: 123
|
|
}).valid).toBe(true)
|
|
})
|
|
|
|
test('array', () => {
|
|
const schema = s.array(s.string())
|
|
expect(schema.parse(['1']).valid).toBe(true)
|
|
expect(schema.parse([]).valid).toBe(true)
|
|
|
|
expect(schema.parse([1]).valid).toBe(false)
|
|
expect(schema.parse(['1', 1]).valid).toBe(false)
|
|
|
|
const a = s.array(s.string())
|
|
.unique()
|
|
|
|
expect(a.parse(['1', '2', '1']).valid).toBe(true)
|
|
expect(a.parse(['1', '2', '1']).object).toEqual(['1', '2'])
|
|
|
|
})
|
|
|
|
test('boolean', () => {
|
|
const schema = s.boolean()
|
|
expect(schema.parse(true).valid).toBe(true)
|
|
expect(schema.parse(false).valid).toBe(true)
|
|
|
|
const p = s.boolean().parseString('oui', 'non')
|
|
expect(p.parse('oui').valid).toBe(true)
|
|
expect(p.parse('pokemon').valid).toBe(false)
|
|
expect(p.parse(1).valid).toBe(false)
|
|
expect(p.parse('oui').object).toBe(true)
|
|
expect(p.parse('non').valid).toBe(true)
|
|
expect(p.parse('non').object).toBe(false)
|
|
|
|
expect(schema.parse('true').valid).toBe(false)
|
|
expect(schema.parse('false').valid).toBe(false)
|
|
})
|