@zhin.js/schema 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/package.json +3 -3
- package/tests/schema.test.ts +165 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/schema",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Configuration validation and schema system for Zhin.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"typescript": "^5.3.0",
|
|
16
16
|
"@types/node": "^24.3.0",
|
|
17
|
-
"zhin.js": "1.0.
|
|
17
|
+
"zhin.js": "1.0.22"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"registry": "https://registry.npmjs.org"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"zhin.js": "1.0.
|
|
29
|
+
"zhin.js": "1.0.22"
|
|
30
30
|
},
|
|
31
31
|
"keywords": [
|
|
32
32
|
"zhin",
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { Schema } from '../src/index'
|
|
3
|
+
|
|
4
|
+
describe('Schema', () => {
|
|
5
|
+
describe('Basic types', () => {
|
|
6
|
+
it('should create string schema', () => {
|
|
7
|
+
const schema = Schema.string()
|
|
8
|
+
expect(schema).toBeDefined()
|
|
9
|
+
expect(schema.meta.type).toBe('string')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('should create number schema', () => {
|
|
13
|
+
const schema = Schema.number()
|
|
14
|
+
expect(schema).toBeDefined()
|
|
15
|
+
expect(schema.meta.type).toBe('number')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('should create boolean schema', () => {
|
|
19
|
+
const schema = Schema.boolean()
|
|
20
|
+
expect(schema).toBeDefined()
|
|
21
|
+
expect(schema.meta.type).toBe('boolean')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should create date schema', () => {
|
|
25
|
+
const schema = Schema.date()
|
|
26
|
+
expect(schema).toBeDefined()
|
|
27
|
+
expect(schema.meta.type).toBe('date')
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should create regexp schema', () => {
|
|
31
|
+
const schema = Schema.regexp()
|
|
32
|
+
expect(schema).toBeDefined()
|
|
33
|
+
expect(schema.meta.type).toBe('regexp')
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe('Complex types', () => {
|
|
38
|
+
it('should create object schema', () => {
|
|
39
|
+
const schema = Schema.object({
|
|
40
|
+
name: Schema.string(),
|
|
41
|
+
age: Schema.number()
|
|
42
|
+
})
|
|
43
|
+
expect(schema).toBeDefined()
|
|
44
|
+
expect(schema.meta.type).toBe('object')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('should create list schema', () => {
|
|
48
|
+
const schema = Schema.list(Schema.string())
|
|
49
|
+
expect(schema).toBeDefined()
|
|
50
|
+
expect(schema.meta.type).toBe('list')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('should create dict schema', () => {
|
|
54
|
+
const schema = Schema.dict(Schema.number())
|
|
55
|
+
expect(schema).toBeDefined()
|
|
56
|
+
expect(schema.meta.type).toBe('dict')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('should create union schema', () => {
|
|
60
|
+
const schema = Schema.union(['option1', 'option2', 'option3'])
|
|
61
|
+
expect(schema).toBeDefined()
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
describe('Schema methods', () => {
|
|
66
|
+
it('should set description', () => {
|
|
67
|
+
const schema = Schema.string().description('Test description')
|
|
68
|
+
expect(schema.meta.description).toBe('Test description')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('should set default value', () => {
|
|
72
|
+
const schema = Schema.string().default('default value')
|
|
73
|
+
expect(schema.meta.default).toBe('default value')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('should set required', () => {
|
|
77
|
+
const schema = Schema.string().required()
|
|
78
|
+
expect(schema.meta.required).toBe(true)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('should have meta property', () => {
|
|
82
|
+
const schema = Schema.string()
|
|
83
|
+
expect(schema.meta).toBeDefined()
|
|
84
|
+
expect(schema.meta.type).toBe('string')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('should chain multiple methods', () => {
|
|
88
|
+
const schema = Schema.string()
|
|
89
|
+
.description('Name')
|
|
90
|
+
.default('John')
|
|
91
|
+
.required()
|
|
92
|
+
|
|
93
|
+
expect(schema.meta.description).toBe('Name')
|
|
94
|
+
expect(schema.meta.default).toBe('John')
|
|
95
|
+
expect(schema.meta.required).toBe(true)
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('Const schema', () => {
|
|
100
|
+
it('should create const schema with string', () => {
|
|
101
|
+
const schema = Schema.const('constant')
|
|
102
|
+
expect(schema.meta.type).toBe('const')
|
|
103
|
+
expect(schema.meta.default).toBe('constant')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('should create const schema with number', () => {
|
|
107
|
+
const schema = Schema.const(42)
|
|
108
|
+
expect(schema.meta.type).toBe('const')
|
|
109
|
+
expect(schema.meta.default).toBe(42)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('should create const schema with object', () => {
|
|
113
|
+
const obj = { key: 'value' }
|
|
114
|
+
const schema = Schema.const(obj)
|
|
115
|
+
expect(schema.meta.type).toBe('const')
|
|
116
|
+
expect(schema.meta.default).toBe(obj)
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
describe('Schema properties', () => {
|
|
121
|
+
it('should have meta for string schema', () => {
|
|
122
|
+
const schema = Schema.string()
|
|
123
|
+
expect(schema.meta).toBeDefined()
|
|
124
|
+
expect(schema.meta.type).toBe('string')
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('should have meta for number schema', () => {
|
|
128
|
+
const schema = Schema.number()
|
|
129
|
+
expect(schema.meta).toBeDefined()
|
|
130
|
+
expect(schema.meta.type).toBe('number')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('should have meta for boolean schema', () => {
|
|
134
|
+
const schema = Schema.boolean()
|
|
135
|
+
expect(schema.meta).toBeDefined()
|
|
136
|
+
expect(schema.meta.type).toBe('boolean')
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('should have options for object schema', () => {
|
|
140
|
+
const schema = Schema.object({
|
|
141
|
+
name: Schema.string(),
|
|
142
|
+
age: Schema.number()
|
|
143
|
+
})
|
|
144
|
+
expect(schema.options).toBeDefined()
|
|
145
|
+
expect(schema.options.object).toBeDefined()
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('should have options for list schema', () => {
|
|
149
|
+
const schema = Schema.list(Schema.string())
|
|
150
|
+
expect(schema.options).toBeDefined()
|
|
151
|
+
expect(schema.options.inner).toBeDefined()
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
describe('Schema intersect', () => {
|
|
156
|
+
it('should create intersect schema', () => {
|
|
157
|
+
const schema1 = Schema.object({ name: Schema.string() })
|
|
158
|
+
const schema2 = Schema.object({ age: Schema.number() })
|
|
159
|
+
const intersect = Schema.intersect([schema1, schema2])
|
|
160
|
+
|
|
161
|
+
expect(intersect).toBeDefined()
|
|
162
|
+
expect(intersect.meta.type).toBe('intersect')
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
})
|