@forklaunch/validator 0.3.0 → 0.3.2
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/LICENSE +21 -0
- package/lib/eslint.config.d.mts +3 -0
- package/lib/eslint.config.d.mts.map +1 -0
- package/lib/eslint.config.mjs +10 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +1 -0
- package/lib/jest.config.d.ts +4 -0
- package/lib/jest.config.d.ts.map +1 -0
- package/lib/jest.config.js +21 -0
- package/{dist → lib/src/shared}/types/schema.types.d.ts +47 -7
- package/lib/src/shared/types/schema.types.d.ts.map +1 -0
- package/lib/src/typebox/index.d.ts +3 -0
- package/lib/src/typebox/index.d.ts.map +1 -0
- package/lib/src/typebox/index.js +1 -0
- package/lib/src/typebox/staticSchemaValidator.d.ts +93 -0
- package/lib/src/typebox/staticSchemaValidator.d.ts.map +1 -0
- package/lib/src/typebox/staticSchemaValidator.js +91 -0
- package/lib/src/typebox/typeboxSchemaValidator.d.ts +110 -0
- package/lib/src/typebox/typeboxSchemaValidator.d.ts.map +1 -0
- package/lib/src/typebox/typeboxSchemaValidator.js +343 -0
- package/{dist/typebox/types/typebox.schema.types.d.ts → lib/src/typebox/types/schema.types.d.ts} +9 -13
- package/lib/src/typebox/types/schema.types.d.ts.map +1 -0
- package/lib/src/zod/index.d.ts +3 -0
- package/lib/src/zod/index.d.ts.map +1 -0
- package/lib/src/zod/index.js +1 -0
- package/lib/src/zod/staticSchemaValidator.d.ts +93 -0
- package/lib/src/zod/staticSchemaValidator.d.ts.map +1 -0
- package/lib/src/zod/staticSchemaValidator.js +91 -0
- package/{dist/zod/types/zod.schema.types.d.ts → lib/src/zod/types/schema.types.d.ts} +3 -2
- package/lib/src/zod/types/schema.types.d.ts.map +1 -0
- package/lib/src/zod/types/schema.types.js +1 -0
- package/lib/src/zod/zodSchemaValidator.d.ts +98 -0
- package/lib/src/zod/zodSchemaValidator.d.ts.map +1 -0
- package/lib/src/zod/zodSchemaValidator.js +179 -0
- package/lib/tests/typebox/advancedParse.test.d.ts +2 -0
- package/lib/tests/typebox/advancedParse.test.d.ts.map +1 -0
- package/lib/tests/typebox/advancedParse.test.js +514 -0
- package/lib/tests/typebox/largeSchema.test.d.ts +2 -0
- package/lib/tests/typebox/largeSchema.test.d.ts.map +1 -0
- package/{dist → lib}/tests/typebox/largeSchema.test.js +46 -49
- package/lib/tests/typebox/schemaValidator.test.d.ts +2 -0
- package/lib/tests/typebox/schemaValidator.test.d.ts.map +1 -0
- package/lib/tests/typebox/schemaValidator.test.js +219 -0
- package/{dist/tests/typebox/equality.test.d.ts → lib/tests/typebox/typeEquality.test.d.ts} +1 -0
- package/lib/tests/typebox/typeEquality.test.d.ts.map +1 -0
- package/lib/tests/typebox/typeEquality.test.js +100 -0
- package/lib/tests/utils/compare.d.ts +2 -0
- package/lib/tests/utils/compare.d.ts.map +1 -0
- package/lib/tests/utils/compare.js +8 -0
- package/{dist/tests → lib/tests/utils}/mockSchemaValidator.d.ts +14 -8
- package/lib/tests/utils/mockSchemaValidator.d.ts.map +1 -0
- package/lib/tests/utils/mockSchemaValidator.js +72 -0
- package/lib/tests/zod/advancedParse.test.d.ts +2 -0
- package/lib/tests/zod/advancedParse.test.d.ts.map +1 -0
- package/lib/tests/zod/advancedParse.test.js +514 -0
- package/lib/tests/zod/largeSchema.test.d.ts +2 -0
- package/lib/tests/zod/largeSchema.test.d.ts.map +1 -0
- package/{dist → lib}/tests/zod/largeSchema.test.js +43 -46
- package/lib/tests/zod/schemaValidator.test.d.ts +2 -0
- package/lib/tests/zod/schemaValidator.test.d.ts.map +1 -0
- package/lib/tests/zod/schemaValidator.test.js +179 -0
- package/{dist/tests/zod/equality.test.d.ts → lib/tests/zod/typeEquality.test.d.ts} +1 -0
- package/lib/tests/zod/typeEquality.test.d.ts.map +1 -0
- package/lib/tests/zod/typeEquality.test.js +100 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/vitest.config.d.ts +3 -0
- package/lib/vitest.config.d.ts.map +1 -0
- package/lib/vitest.config.js +7 -0
- package/package.json +35 -39
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -18
- package/dist/index.js.map +0 -1
- package/dist/jest.config.d.ts +0 -3
- package/dist/jest.config.js +0 -10
- package/dist/jest.config.js.map +0 -1
- package/dist/tests/mockSchemaValidator.js +0 -60
- package/dist/tests/mockSchemaValidator.js.map +0 -1
- package/dist/tests/typebox/equality.test.js +0 -234
- package/dist/tests/typebox/equality.test.js.map +0 -1
- package/dist/tests/typebox/largeSchema.test.js.map +0 -1
- package/dist/tests/zod/equality.test.js +0 -238
- package/dist/tests/zod/equality.test.js.map +0 -1
- package/dist/tests/zod/largeSchema.test.js.map +0 -1
- package/dist/typebox/index.d.ts +0 -1
- package/dist/typebox/index.js +0 -18
- package/dist/typebox/index.js.map +0 -1
- package/dist/typebox/typebox.schemaValidator.d.ts +0 -150
- package/dist/typebox/typebox.schemaValidator.js +0 -203
- package/dist/typebox/typebox.schemaValidator.js.map +0 -1
- package/dist/typebox/types/typebox.schema.types.js +0 -3
- package/dist/typebox/types/typebox.schema.types.js.map +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +0 -18
- package/dist/types/index.js.map +0 -1
- package/dist/types/schema.types.js +0 -3
- package/dist/types/schema.types.js.map +0 -1
- package/dist/zod/index.d.ts +0 -1
- package/dist/zod/index.js +0 -18
- package/dist/zod/index.js.map +0 -1
- package/dist/zod/types/zod.schema.types.js +0 -3
- package/dist/zod/types/zod.schema.types.js.map +0 -1
- package/dist/zod/zod.schemaValidator.d.ts +0 -147
- package/dist/zod/zod.schemaValidator.js +0 -196
- package/dist/zod/zod.schemaValidator.js.map +0 -1
- /package/{dist/tests/typebox/largeSchema.test.d.ts → lib/src/shared/types/schema.types.js} +0 -0
- /package/{dist/tests/zod/largeSchema.test.d.ts → lib/src/typebox/types/schema.types.js} +0 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { generateSchema } from '@anatine/zod-openapi';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { array, number, openapi, optional, parse, schemify, string, union, validate } from '../../src/zod';
|
|
4
|
+
import { compare } from '../utils/compare';
|
|
5
|
+
describe('zod schema validator tests', () => {
|
|
6
|
+
let schema;
|
|
7
|
+
let schemified;
|
|
8
|
+
let expectedSchema;
|
|
9
|
+
beforeAll(() => {
|
|
10
|
+
schema = {
|
|
11
|
+
hello: {
|
|
12
|
+
world: string
|
|
13
|
+
},
|
|
14
|
+
foo: {
|
|
15
|
+
bar: number
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
schemified = schemify(schema);
|
|
19
|
+
expectedSchema = z.object({
|
|
20
|
+
hello: z.object({
|
|
21
|
+
world: z.string()
|
|
22
|
+
}),
|
|
23
|
+
foo: z.object({
|
|
24
|
+
bar: z.number()
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
test('schemify', async () => {
|
|
29
|
+
compare(schemified, expectedSchema);
|
|
30
|
+
compare(schemified, schemify({
|
|
31
|
+
hello: {
|
|
32
|
+
world: string
|
|
33
|
+
},
|
|
34
|
+
foo: {
|
|
35
|
+
bar: number
|
|
36
|
+
}
|
|
37
|
+
}));
|
|
38
|
+
compare(schemified, schemify({
|
|
39
|
+
hello: schemify({
|
|
40
|
+
world: string
|
|
41
|
+
}),
|
|
42
|
+
foo: {
|
|
43
|
+
bar: number
|
|
44
|
+
}
|
|
45
|
+
}));
|
|
46
|
+
compare(schemified, schemify({
|
|
47
|
+
hello: {
|
|
48
|
+
world: string
|
|
49
|
+
},
|
|
50
|
+
foo: schemify({
|
|
51
|
+
bar: number
|
|
52
|
+
})
|
|
53
|
+
}));
|
|
54
|
+
compare(schemified, schemify({
|
|
55
|
+
hello: schemify({
|
|
56
|
+
world: string
|
|
57
|
+
}),
|
|
58
|
+
foo: schemify({
|
|
59
|
+
bar: number
|
|
60
|
+
})
|
|
61
|
+
}));
|
|
62
|
+
});
|
|
63
|
+
test('optional', async () => {
|
|
64
|
+
const unboxSchemified = optional(schema);
|
|
65
|
+
const boxSchemified = optional(schemified);
|
|
66
|
+
const schemifiedExpected = z.optional(expectedSchema);
|
|
67
|
+
compare(unboxSchemified, schemifiedExpected);
|
|
68
|
+
compare(boxSchemified, schemifiedExpected);
|
|
69
|
+
});
|
|
70
|
+
test('array', async () => {
|
|
71
|
+
const unboxSchemified = array(schema);
|
|
72
|
+
const boxSchemified = array(schemified);
|
|
73
|
+
const schemifiedExpected = z.array(expectedSchema);
|
|
74
|
+
compare(unboxSchemified, schemifiedExpected);
|
|
75
|
+
compare(boxSchemified, schemifiedExpected);
|
|
76
|
+
});
|
|
77
|
+
test('union', async () => {
|
|
78
|
+
const unboxSchemified = union([
|
|
79
|
+
schema,
|
|
80
|
+
{
|
|
81
|
+
test: string
|
|
82
|
+
}
|
|
83
|
+
]);
|
|
84
|
+
const unboxSchemified2 = union([
|
|
85
|
+
schema,
|
|
86
|
+
schemify({
|
|
87
|
+
test: string
|
|
88
|
+
})
|
|
89
|
+
]);
|
|
90
|
+
const boxSchemified = union([
|
|
91
|
+
schemified,
|
|
92
|
+
schemify({
|
|
93
|
+
test: string
|
|
94
|
+
})
|
|
95
|
+
]);
|
|
96
|
+
const boxSchemified2 = union([
|
|
97
|
+
schemified,
|
|
98
|
+
{
|
|
99
|
+
test: string
|
|
100
|
+
}
|
|
101
|
+
]);
|
|
102
|
+
const schemifiedExpected = z.union([
|
|
103
|
+
expectedSchema,
|
|
104
|
+
z.object({
|
|
105
|
+
test: z.string()
|
|
106
|
+
})
|
|
107
|
+
]);
|
|
108
|
+
compare(unboxSchemified, schemifiedExpected);
|
|
109
|
+
compare(unboxSchemified2, schemifiedExpected);
|
|
110
|
+
compare(boxSchemified, schemifiedExpected);
|
|
111
|
+
compare(boxSchemified2, schemifiedExpected);
|
|
112
|
+
});
|
|
113
|
+
test('literal', async () => {
|
|
114
|
+
const schemified = schemify({
|
|
115
|
+
hello: 'world'
|
|
116
|
+
});
|
|
117
|
+
compare(schemified, z.object({
|
|
118
|
+
hello: z.literal('world')
|
|
119
|
+
}));
|
|
120
|
+
});
|
|
121
|
+
test('validate', async () => {
|
|
122
|
+
validate(schemified, {
|
|
123
|
+
hello: {
|
|
124
|
+
world: 'world'
|
|
125
|
+
},
|
|
126
|
+
foo: {
|
|
127
|
+
bar: 42
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
expect(validate(schemified, {
|
|
131
|
+
hello: {
|
|
132
|
+
world: 55
|
|
133
|
+
},
|
|
134
|
+
foo: {}
|
|
135
|
+
})).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
test('parse', () => {
|
|
138
|
+
const validParse = parse(schemified, {
|
|
139
|
+
hello: {
|
|
140
|
+
world: 'world'
|
|
141
|
+
},
|
|
142
|
+
foo: {
|
|
143
|
+
bar: 42
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
expect(validParse.ok).toBe(true);
|
|
147
|
+
if (validParse.ok) {
|
|
148
|
+
compare(validParse.value, {
|
|
149
|
+
hello: {
|
|
150
|
+
world: 'world'
|
|
151
|
+
},
|
|
152
|
+
foo: {
|
|
153
|
+
bar: 42
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const failedParse = parse(schemified, {
|
|
158
|
+
hello: {
|
|
159
|
+
world: 55
|
|
160
|
+
},
|
|
161
|
+
foo: {}
|
|
162
|
+
});
|
|
163
|
+
expect(failedParse.ok).toBe(false);
|
|
164
|
+
if (!failedParse.ok) {
|
|
165
|
+
expect(failedParse.error)
|
|
166
|
+
.toBe(`Validation failed with the following errors:
|
|
167
|
+
1. Path: hello > world
|
|
168
|
+
Message: Expected string, received number
|
|
169
|
+
|
|
170
|
+
2. Path: foo > bar
|
|
171
|
+
Message: Expected number, received nan`);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
test('openapi', async () => {
|
|
175
|
+
const schemified = schemify(schema);
|
|
176
|
+
const openApi = openapi(schemified);
|
|
177
|
+
expect(openApi).toEqual(generateSchema(schemified));
|
|
178
|
+
});
|
|
179
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typeEquality.test.d.ts","sourceRoot":"","sources":["../../../tests/zod/typeEquality.test.ts"],"names":[],"mappings":"AAgHA,wBAAgB,MAAM,CAAC,CAAC,SAAS,KAAK,UAAM"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { array, bigint, boolean, date, never, nullish, number, optional, schemify, string, symbol, union } from '../../src/zod';
|
|
2
|
+
const one = array({
|
|
3
|
+
name: {
|
|
4
|
+
j: union([string, number, date, boolean, bigint, nullish, symbol, never]),
|
|
5
|
+
t: optional(union([
|
|
6
|
+
array({
|
|
7
|
+
y: array(number)
|
|
8
|
+
}),
|
|
9
|
+
string
|
|
10
|
+
])),
|
|
11
|
+
m: {
|
|
12
|
+
a: optional(string),
|
|
13
|
+
b: number,
|
|
14
|
+
c: {
|
|
15
|
+
d: string,
|
|
16
|
+
e: number
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
200: {
|
|
21
|
+
j: string
|
|
22
|
+
},
|
|
23
|
+
m: {
|
|
24
|
+
a: true
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
const two = array({
|
|
28
|
+
name: {
|
|
29
|
+
j: union([string, number, date, boolean, bigint, nullish, symbol, never]),
|
|
30
|
+
t: optional(union([
|
|
31
|
+
array({
|
|
32
|
+
y: array(number)
|
|
33
|
+
}),
|
|
34
|
+
string
|
|
35
|
+
])),
|
|
36
|
+
m: schemify({
|
|
37
|
+
a: optional(string),
|
|
38
|
+
b: number,
|
|
39
|
+
c: {
|
|
40
|
+
d: string,
|
|
41
|
+
e: number
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
},
|
|
45
|
+
200: {
|
|
46
|
+
j: string
|
|
47
|
+
},
|
|
48
|
+
m: {
|
|
49
|
+
a: true
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const three = schemify(array(schemify({
|
|
53
|
+
name: schemify({
|
|
54
|
+
j: union([
|
|
55
|
+
string,
|
|
56
|
+
number,
|
|
57
|
+
date,
|
|
58
|
+
boolean,
|
|
59
|
+
bigint,
|
|
60
|
+
nullish,
|
|
61
|
+
symbol,
|
|
62
|
+
never
|
|
63
|
+
]),
|
|
64
|
+
t: optional(union([
|
|
65
|
+
array({
|
|
66
|
+
y: array(number)
|
|
67
|
+
}),
|
|
68
|
+
string
|
|
69
|
+
])),
|
|
70
|
+
m: schemify({
|
|
71
|
+
a: optional(string),
|
|
72
|
+
b: number,
|
|
73
|
+
c: {
|
|
74
|
+
d: string,
|
|
75
|
+
e: number
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
}),
|
|
79
|
+
200: schemify({
|
|
80
|
+
j: string
|
|
81
|
+
}),
|
|
82
|
+
m: schemify({
|
|
83
|
+
a: true
|
|
84
|
+
})
|
|
85
|
+
})));
|
|
86
|
+
export function assert() { }
|
|
87
|
+
assert();
|
|
88
|
+
assert();
|
|
89
|
+
assert();
|
|
90
|
+
assert();
|
|
91
|
+
const shortOne = {
|
|
92
|
+
s: string,
|
|
93
|
+
non: number
|
|
94
|
+
};
|
|
95
|
+
const shortTwo = schemify({
|
|
96
|
+
s: string,
|
|
97
|
+
non: number
|
|
98
|
+
});
|
|
99
|
+
assert();
|
|
100
|
+
assert();
|