schematox 1.2.1 → 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.
Files changed (63) hide show
  1. package/dist/constants.d.ts +20 -0
  2. package/dist/constants.d.ts.map +1 -0
  3. package/dist/constants.js +22 -0
  4. package/dist/constants.js.map +1 -0
  5. package/dist/index.d.ts +10 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +9 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/parse.d.ts +5 -0
  10. package/dist/parse.d.ts.map +1 -0
  11. package/dist/parse.js +328 -0
  12. package/dist/parse.js.map +1 -0
  13. package/dist/struct.d.ts +48 -0
  14. package/dist/struct.d.ts.map +1 -0
  15. package/dist/struct.js +111 -0
  16. package/dist/struct.js.map +1 -0
  17. package/dist/types/extensions.d.ts +13 -0
  18. package/dist/types/extensions.d.ts.map +1 -0
  19. package/dist/types/extensions.js +2 -0
  20. package/dist/types/extensions.js.map +1 -0
  21. package/dist/types/infer.d.ts +35 -0
  22. package/dist/types/infer.d.ts.map +1 -0
  23. package/dist/types/infer.js +2 -0
  24. package/dist/types/infer.js.map +1 -0
  25. package/dist/types/schema.d.ts +93 -0
  26. package/dist/types/schema.d.ts.map +1 -0
  27. package/dist/types/schema.js +2 -0
  28. package/dist/types/schema.js.map +1 -0
  29. package/dist/types/standard-schema.d.ts +35 -0
  30. package/dist/types/standard-schema.d.ts.map +1 -0
  31. package/dist/types/standard-schema.js +2 -0
  32. package/dist/types/standard-schema.js.map +1 -0
  33. package/dist/types/struct.d.ts +52 -0
  34. package/dist/types/struct.d.ts.map +1 -0
  35. package/dist/types/struct.js +2 -0
  36. package/dist/types/struct.js.map +1 -0
  37. package/dist/types/utils.d.ts +41 -0
  38. package/dist/types/utils.d.ts.map +1 -0
  39. package/dist/types/utils.js +2 -0
  40. package/dist/types/utils.js.map +1 -0
  41. package/dist/utils.d.ts +9 -0
  42. package/dist/utils.d.ts.map +1 -0
  43. package/dist/utils.js +14 -0
  44. package/dist/utils.js.map +1 -0
  45. package/package.json +15 -4
  46. package/src/tests/README.md +390 -0
  47. package/src/tests/by-struct/array.test.ts +1684 -0
  48. package/src/tests/by-struct/boolean.test.ts +741 -0
  49. package/src/tests/by-struct/literal.test.ts +755 -0
  50. package/src/tests/by-struct/number.test.ts +1234 -0
  51. package/src/tests/by-struct/object.test.ts +1484 -0
  52. package/src/tests/by-struct/record.test.ts +1802 -0
  53. package/src/tests/by-struct/string.test.ts +1252 -0
  54. package/src/tests/by-struct/tuple.test.ts +1341 -0
  55. package/src/tests/by-struct/union.test.ts +1284 -0
  56. package/src/tests/fixtures.ts +52 -0
  57. package/src/tests/fold-constants.ts +247 -0
  58. package/src/tests/fold-morph.ts +49 -0
  59. package/src/tests/type.ts +1 -0
  60. package/src/tests/types/extensions.test.ts +117 -0
  61. package/src/tests/types/infer.test.ts +1410 -0
  62. package/src/tests/utils.test.ts +191 -0
  63. package/CHANGELOG.md +0 -52
@@ -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,52 +0,0 @@
1
- # Changelog
2
-
3
- ## [1.2.1](https://github.com/incerta/schematox/compare/v1.2.0...v1.2.1)
4
-
5
- - [Add type "module" to package.json #51](https://github.com/incerta/schematox/pull/51)
6
-
7
- ## [1.2.0](https://github.com/incerta/schematox/compare/v1.1.0...v1.2.0)
8
-
9
- - [Record schema range limiters support #48](https://github.com/incerta/schematox/pull/48)
10
-
11
- ## [1.1.0](https://github.com/incerta/schematox/compare/v1.0.1...v1.1.0)
12
-
13
- - [Standard schema support #46](https://github.com/incerta/schematox/pull/46)
14
-
15
- ## [1.0.1](https://github.com/incerta/schematox/compare/v1.0.0...v1.0.1)
16
-
17
- - [FIX: struct brand assignment second argument type restriction #44](https://github.com/incerta/schematox/pull/44)
18
-
19
- ## [1.0.0](https://github.com/incerta/schematox/compare/v0.4.0...v1.0.0)
20
-
21
- The module went through major refactoring so it could be ready for production usage:
22
-
23
- - [RecordSchema support #34](https://github.com/incerta/schematox/pull/34)
24
- - [Drop validate/guard feature support #36](https://github.com/incerta/schematox/pull/36)
25
- - [Pre major release testing architecture and file structure refactoring #38](https://github.com/incerta/schematox/pull/38)
26
- - [Break down parse logic into smaller functions #39](https://github.com/incerta/schematox/pull/39)
27
- - [Support unrestricted object schema depth #42](https://github.com/incerta/schematox/pull/42)
28
- - [Support tuple schema #43](https://github.com/incerta/schematox/pull/43)
29
-
30
- ## [0.4.0](https://github.com/incerta/schematox/compare/v0.3.1...v0.4.0)
31
-
32
- - [`aa0d95e`](https://github.com/incerta/schematox/commit/aa0d95e30b7784c0ce29317ae808e4ba7950abab) Extend compound structure nesting limit to 12 layers of depth
33
-
34
- ## [0.3.1](https://github.com/incerta/schematox/compare/v0.3.0...v0.3.1)
35
-
36
- ### Bugfix
37
-
38
- - [`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
39
-
40
- ### Chore
41
-
42
- - [`7d98a81`](https://github.com/incerta/schematox/commit/7d98a81c2bc18280299365da32f8346d5b145560) Use "es2015" tsconfig target instead of outdated "es5"
43
-
44
- ### Features
45
-
46
- - [`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)
47
-
48
- ## [0.3.0](https://github.com/incerta/schematox/compare/v0.2.0...v0.3.0)
49
-
50
- ### Features
51
-
52
- - [`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)