-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.test.ts
73 lines (72 loc) · 2.47 KB
/
object.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Tsu from '../src'
test('object parser', () => {
expect(Tsu.Object({}).check([])).toBe(false)
expect(Tsu.Object({}).check({})).toBe(true)
expect(Tsu.Object({}).check(1)).toBe(false)
expect(Tsu.Object({}).check('2')).toBe(false)
expect(Tsu.Object({}).check({ k: 1 })).toBe(true)
expect(Tsu.Object({}).strict().check({ k: 1 })).toBe(false)
expect(
Tsu.Object({ k: Tsu.Number(), v: Tsu.String().default('hi') })
.strict()
.parse({ k: 1, g: 'h' })
).toStrictEqual({
k: 1,
v: 'hi'
})
expect(Tsu.Object({ k: Tsu.Number(), v: Tsu.String().default('hi') }).parse({ k: 1, g: 'test' })).toStrictEqual({
k: 1,
v: 'hi',
g: 'test'
})
expect(Tsu.Object({ k: Tsu.Number(), v: Tsu.String() }).check({ k: 1 })).toBe(false)
expect(
Tsu.Object({ k: Tsu.Number(), v: Tsu.String().default('hi') })
.default({ k: 233, v: 'hello' })
.parse(undefined)
).toStrictEqual({ k: 233, v: 'hello' })
expect(Tsu.Object({ k: Tsu.Number(), v: Tsu.String().default('hi') }).check({ v: 'hey' })).toBe(false)
expect(
Tsu.Object({})
.index(Tsu.String().regexp(/[0-9]+\.[0-9]+\.[0-9]+/), Tsu.String().regexp(/kotori-plugin-(.*)/))
.check({
'kotori-plugin-adapter-qq': '1.5.0',
'kotori-plugin-wiki': '1.0.0'
})
).toBe(true)
expect(
Tsu.Object({})
.index(Tsu.String().regexp(/[0-9]+\.[0-9]+\.[0-9]+/), Tsu.String().regexp(/kotori-plugin-(.*)/))
.check({
'kotori-plugin-adapter-qq': '1.5.0',
'kotori-plin-wiki': '1.0.0'
})
).toBe(false)
expect(
Tsu.Object({})
.index(Tsu.String().regexp(/[0-9]+\.[0-9]+\.[0-9]+/), Tsu.String().regexp(/kotori-plugin-(.*)/))
.check({
'kotori-plugin-adapter-qq': '1.5.0',
'kotori-plugin-wiki': 'hi'
})
).toBe(false)
expect(
Tsu.Object({
mode: Tsu.Enum(Tsu.Literal('mode1'), Tsu.Literal('mode2')),
port: Tsu.Number().port(),
address: Tsu.String().describe('server address')
}).schema()
).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
mode: { enum: ['mode1', 'mode2'] },
port: { type: 'integer', minimum: 1, maximum: 65535 },
address: { type: 'string', description: 'server address' }
},
required: ['mode', 'port', 'address']
})
expect(Tsu.Object({}).min(1).check({})).toBe(false)
expect(Tsu.Object({}).max(1).check({ a: 1, b: 2 })).toBe(false)
expect(Tsu.Object({}).range(1, 1).check({ a: 1 })).toBe(true)
})