schematox 1.2.0 → 1.2.2-alpha
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/dist/constants.d.ts +20 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +22 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/parse.d.ts +5 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +328 -0
- package/dist/parse.js.map +1 -0
- package/dist/struct.d.ts +48 -0
- package/dist/struct.d.ts.map +1 -0
- package/dist/struct.js +111 -0
- package/dist/struct.js.map +1 -0
- package/dist/types/extensions.d.ts +13 -0
- package/dist/types/extensions.d.ts.map +1 -0
- package/dist/types/extensions.js +2 -0
- package/dist/types/extensions.js.map +1 -0
- package/dist/types/infer.d.ts +35 -0
- package/dist/types/infer.d.ts.map +1 -0
- package/dist/types/infer.js +2 -0
- package/dist/types/infer.js.map +1 -0
- package/dist/types/schema.d.ts +93 -0
- package/dist/types/schema.d.ts.map +1 -0
- package/dist/types/schema.js +2 -0
- package/dist/types/schema.js.map +1 -0
- package/dist/types/standard-schema.d.ts +35 -0
- package/dist/types/standard-schema.d.ts.map +1 -0
- package/dist/types/standard-schema.js +2 -0
- package/dist/types/standard-schema.js.map +1 -0
- package/dist/types/struct.d.ts +52 -0
- package/dist/types/struct.d.ts.map +1 -0
- package/dist/types/struct.js +2 -0
- package/dist/types/struct.js.map +1 -0
- package/dist/types/utils.d.ts +41 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utils.js +2 -0
- package/dist/types/utils.js.map +1 -0
- package/dist/utils.d.ts +9 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +14 -0
- package/dist/utils.js.map +1 -0
- package/package.json +16 -4
- package/src/tests/README.md +390 -0
- package/src/tests/by-struct/array.test.ts +1684 -0
- package/src/tests/by-struct/boolean.test.ts +741 -0
- package/src/tests/by-struct/literal.test.ts +755 -0
- package/src/tests/by-struct/number.test.ts +1234 -0
- package/src/tests/by-struct/object.test.ts +1484 -0
- package/src/tests/by-struct/record.test.ts +1802 -0
- package/src/tests/by-struct/string.test.ts +1252 -0
- package/src/tests/by-struct/tuple.test.ts +1341 -0
- package/src/tests/by-struct/union.test.ts +1284 -0
- package/src/tests/fixtures.ts +52 -0
- package/src/tests/fold-constants.ts +247 -0
- package/src/tests/fold-morph.ts +49 -0
- package/src/tests/type.ts +1 -0
- package/src/tests/types/extensions.test.ts +117 -0
- package/src/tests/types/infer.test.ts +1410 -0
- package/src/tests/utils.test.ts +191 -0
- package/CHANGELOG.md +0 -48
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import * as x from '../'
|
|
2
|
+
|
|
3
|
+
describe('Type equivalence check by `tCh` utility', () => {
|
|
4
|
+
describe('Union of literals', () => {
|
|
5
|
+
it('valid', () => {
|
|
6
|
+
type Expected = 'x' | 'y'
|
|
7
|
+
type Actual = 'x' | 'y'
|
|
8
|
+
|
|
9
|
+
x.tCh<Expected, Actual>()
|
|
10
|
+
x.tCh<Actual, Expected>()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('union has extra member', () => {
|
|
14
|
+
type Expected = 'x' | 'y'
|
|
15
|
+
type Actual = 'x' | 'y' | 'z'
|
|
16
|
+
|
|
17
|
+
// @ts-expect-error: '"z"' is not assignable to type 'Expected'
|
|
18
|
+
x.tCh<Expected, Actual>()
|
|
19
|
+
x.tCh<Actual, Expected>()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('union has `never` instead of expected member', () => {
|
|
23
|
+
type Expected = 'x' | 'y'
|
|
24
|
+
type Actual = 'x' | never
|
|
25
|
+
|
|
26
|
+
x.tCh<Expected, Actual>()
|
|
27
|
+
// @ts-expect-error: 'Expected' does not satisfy the constraint '"x"'
|
|
28
|
+
x.tCh<Actual, Expected>()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('union is `never`', () => {
|
|
32
|
+
type Expected = 'x' | 'y'
|
|
33
|
+
type Actual = never
|
|
34
|
+
|
|
35
|
+
x.tCh<Expected, Actual>()
|
|
36
|
+
// @ts-expect-error: Type 'string' does not satisfy the constraint 'never'.
|
|
37
|
+
x.tCh<Actual, Expected>()
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
describe('Array of primitives', () => {
|
|
42
|
+
it('valid', () => {
|
|
43
|
+
type Expected = Array<string | number>
|
|
44
|
+
type Actual = Array<string | number>
|
|
45
|
+
|
|
46
|
+
x.tCh<Expected, Actual>()
|
|
47
|
+
x.tCh<Actual, Expected>()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('array union has one extra member', () => {
|
|
51
|
+
type Expected = Array<string | number>
|
|
52
|
+
type Actual = Array<string | number | boolean>
|
|
53
|
+
|
|
54
|
+
// @ts-expect-error: 'string | number | boolean' is not assignable to type 'string | number'
|
|
55
|
+
x.tCh<Expected, Actual>()
|
|
56
|
+
x.tCh<Actual, Expected>()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('array union has `never` instead of expected memeber', () => {
|
|
60
|
+
type Expected = Array<string | number>
|
|
61
|
+
type Actual = Array<string | never>
|
|
62
|
+
|
|
63
|
+
x.tCh<Expected, Actual>()
|
|
64
|
+
// @ts-expect-error: 'Expected' does not satisfy the constraint 'string[]'
|
|
65
|
+
x.tCh<Actual, Expected>()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('array of `never`', () => {
|
|
69
|
+
type Expected = Array<string | number>
|
|
70
|
+
type Actual = Array<never>
|
|
71
|
+
|
|
72
|
+
x.tCh<Expected, Actual>()
|
|
73
|
+
// @ts-expect-error: 'Expected' does not satisfy the constraint 'never[]'
|
|
74
|
+
x.tCh<Actual, Expected>()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('array is `never`', () => {
|
|
78
|
+
type Expected = Array<string | number>
|
|
79
|
+
type Actual = never
|
|
80
|
+
|
|
81
|
+
x.tCh<Expected, Actual>()
|
|
82
|
+
// @ts-expect-error: Type 'Expected' does not satisfy the constraint 'never'
|
|
83
|
+
x.tCh<Actual, Expected>()
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
describe('Object of primitives', () => {
|
|
88
|
+
it('valid', () => {
|
|
89
|
+
type Expected = { x: string; y: number }
|
|
90
|
+
type Actual = { x: string; y: number }
|
|
91
|
+
|
|
92
|
+
x.tCh<Expected, Actual>()
|
|
93
|
+
x.tCh<Actual, Expected>()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('has extra property', () => {
|
|
97
|
+
type Expected = { x: string; y: number }
|
|
98
|
+
type Actual = { x: string; y: number; z: boolean }
|
|
99
|
+
|
|
100
|
+
x.tCh<Expected, Actual>()
|
|
101
|
+
// @ts-expect-error: Property 'z' is missing in type 'Expected' but required in type 'Actual'
|
|
102
|
+
x.tCh<Actual, Expected>()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('one property has `never` value', () => {
|
|
106
|
+
type Expected = { x: string; y: number }
|
|
107
|
+
type Actual = { x: string; y: never }
|
|
108
|
+
|
|
109
|
+
x.tCh<Expected, Actual>()
|
|
110
|
+
// @ts-expect-error Types of property 'y' are incompatible. Type 'number' is not assignable to type 'never'.
|
|
111
|
+
x.tCh<Actual, Expected>()
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('all properties have `never` value', () => {
|
|
115
|
+
type Expected = { x: string; y: number }
|
|
116
|
+
type Actual = { x: never; y: never }
|
|
117
|
+
|
|
118
|
+
x.tCh<Expected, Actual>()
|
|
119
|
+
// @ts-expect-error 'Expected' does not satisfy the constraint 'Actual'
|
|
120
|
+
x.tCh<Actual, Expected>()
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
describe('Check ParseResult utilities', () => {
|
|
126
|
+
it('ParseResult typguards are functional', () => {
|
|
127
|
+
const parsed = x.boolean().parse(undefined)
|
|
128
|
+
|
|
129
|
+
if (parsed.success) {
|
|
130
|
+
x.tCh<typeof parsed.data, boolean>()
|
|
131
|
+
x.tCh<boolean, typeof parsed.data>()
|
|
132
|
+
|
|
133
|
+
x.tCh<typeof parsed.error, undefined>()
|
|
134
|
+
x.tCh<undefined, typeof parsed.error>()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (parsed.data !== undefined) {
|
|
138
|
+
x.tCh<typeof parsed.data, boolean>()
|
|
139
|
+
x.tCh<boolean, typeof parsed.data>()
|
|
140
|
+
|
|
141
|
+
x.tCh<typeof parsed.error, undefined>()
|
|
142
|
+
x.tCh<undefined, typeof parsed.error>()
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (parsed.success === false) {
|
|
146
|
+
x.tCh<typeof parsed.error, x.InvalidSubject[]>()
|
|
147
|
+
x.tCh<x.InvalidSubject[], typeof parsed.error>()
|
|
148
|
+
|
|
149
|
+
x.tCh<typeof parsed.data, undefined>()
|
|
150
|
+
x.tCh<undefined, typeof parsed.data>()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (parsed.error) {
|
|
154
|
+
x.tCh<typeof parsed.error, x.InvalidSubject[]>()
|
|
155
|
+
x.tCh<x.InvalidSubject[], typeof parsed.error>()
|
|
156
|
+
|
|
157
|
+
x.tCh<typeof parsed.data, undefined>()
|
|
158
|
+
x.tCh<undefined, typeof parsed.data>()
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('ParseError result', () => {
|
|
163
|
+
const invalidSubjects: x.InvalidSubject[] = [
|
|
164
|
+
{
|
|
165
|
+
code: x.ERROR_CODE.invalidType,
|
|
166
|
+
path: [],
|
|
167
|
+
schema: { type: 'string' },
|
|
168
|
+
subject: undefined,
|
|
169
|
+
},
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
const parseError = x.error(invalidSubjects)
|
|
173
|
+
|
|
174
|
+
expect('data' in parseError).toBe(false)
|
|
175
|
+
expect(parseError.success).toBe(false)
|
|
176
|
+
expect(parseError.error === invalidSubjects).toBe(true)
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('ParseSuccess result', () => {
|
|
180
|
+
const validSubj = 'sample'
|
|
181
|
+
const parsedSuccess = x.success(validSubj)
|
|
182
|
+
|
|
183
|
+
expect('error' in parsedSuccess).toBe(false)
|
|
184
|
+
expect(parsedSuccess.success).toBe(true)
|
|
185
|
+
expect(parsedSuccess.data).toBe(validSubj)
|
|
186
|
+
})
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it('PARAMS_BY_SCHEMA_TYPE is exported', () => {
|
|
190
|
+
expect(x.PARAMS_BY_SCHEMA_TYPE).toStrictEqual(x.PARAMS_BY_SCHEMA_TYPE)
|
|
191
|
+
})
|
package/CHANGELOG.md
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [1.2.0](https://github.com/incerta/schematox/compare/v1.1.0...v1.2.0)
|
|
4
|
-
|
|
5
|
-
- [Record schema range limiters support #48](https://github.com/incerta/schematox/pull/48)
|
|
6
|
-
|
|
7
|
-
## [1.1.0](https://github.com/incerta/schematox/compare/v1.0.1...v1.1.0)
|
|
8
|
-
|
|
9
|
-
- [Standard schema support #46](https://github.com/incerta/schematox/pull/46)
|
|
10
|
-
|
|
11
|
-
## [1.0.1](https://github.com/incerta/schematox/compare/v1.0.0...v1.0.1)
|
|
12
|
-
|
|
13
|
-
- [FIX: struct brand assignment second argument type restriction #44](https://github.com/incerta/schematox/pull/44)
|
|
14
|
-
|
|
15
|
-
## [1.0.0](https://github.com/incerta/schematox/compare/v0.4.0...v1.0.0)
|
|
16
|
-
|
|
17
|
-
The module went through major refactoring so it could be ready for production usage:
|
|
18
|
-
|
|
19
|
-
- [RecordSchema support #34](https://github.com/incerta/schematox/pull/34)
|
|
20
|
-
- [Drop validate/guard feature support #36](https://github.com/incerta/schematox/pull/36)
|
|
21
|
-
- [Pre major release testing architecture and file structure refactoring #38](https://github.com/incerta/schematox/pull/38)
|
|
22
|
-
- [Break down parse logic into smaller functions #39](https://github.com/incerta/schematox/pull/39)
|
|
23
|
-
- [Support unrestricted object schema depth #42](https://github.com/incerta/schematox/pull/42)
|
|
24
|
-
- [Support tuple schema #43](https://github.com/incerta/schematox/pull/43)
|
|
25
|
-
|
|
26
|
-
## [0.4.0](https://github.com/incerta/schematox/compare/v0.3.1...v0.4.0)
|
|
27
|
-
|
|
28
|
-
- [`aa0d95e`](https://github.com/incerta/schematox/commit/aa0d95e30b7784c0ce29317ae808e4ba7950abab) Extend compound structure nesting limit to 12 layers of depth
|
|
29
|
-
|
|
30
|
-
## [0.3.1](https://github.com/incerta/schematox/compare/v0.3.0...v0.3.1)
|
|
31
|
-
|
|
32
|
-
### Bugfix
|
|
33
|
-
|
|
34
|
-
- [`e3527df`](https://github.com/incerta/schematox/commit/e3527dfb46b73a4b6579e3d2aea58f3301aded9a) [#24](https://github.com/incerta/schematox/pull/25) Parser should preserve object optional key only if it is specified in source
|
|
35
|
-
|
|
36
|
-
### Chore
|
|
37
|
-
|
|
38
|
-
- [`7d98a81`](https://github.com/incerta/schematox/commit/7d98a81c2bc18280299365da32f8346d5b145560) Use "es2015" tsconfig target instead of outdated "es5"
|
|
39
|
-
|
|
40
|
-
### Features
|
|
41
|
-
|
|
42
|
-
- [`8bc2082`](https://github.com/incerta/schematox/commit/8bc208211457901f4f7246f00694f112d56f8d56) [#22](https://github.com/incerta/schematox/issues/22) Ensure optional properties in schemas reflect as optional in object context (@incerta)
|
|
43
|
-
|
|
44
|
-
## [0.3.0](https://github.com/incerta/schematox/compare/v0.2.0...v0.3.0)
|
|
45
|
-
|
|
46
|
-
### Features
|
|
47
|
-
|
|
48
|
-
- [`8bc2082`](https://github.com/incerta/schematox/commit/8bc208211457901f4f7246f00694f112d56f8d56) [#22](https://github.com/incerta/schematox/issues/22) Ensure optional properties in schemas reflect as optional in object context (@incerta)
|