-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
114 lines (97 loc) · 3.52 KB
/
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import Ajv from 'ajv';
import { expect } from 'chai';
import { cloneDeep } from 'lodash-es';
import Schema from 'schemastery';
// @ts-ignore
import convert from './index.ts';
const ajv = new Ajv({ useDefaults: true });
type AnySchema = Schema | Schema<never, never>;
const check = (schema: AnySchema, value: any) => ajv.validate(convert(schema, true), value);
const test = (schema: AnySchema, value: any) => {
const info = `
Schema: ${schema.toString()}
JSON: ${JSON.stringify(schema.toJSON())}
convert: ${JSON.stringify(convert(schema, true))}
`;
try {
schema(value as never);
} catch (e) {
expect(check(schema, value), info).to.equal(false);
return;
}
expect(check(schema, value), info).to.equal(true);
const extended = Schema.object({ foo: schema });
const data = cloneDeep({ foo: value });
ajv.validate(convert(extended, true), data);
const extendedinfo = `
Schema: ${extended.toString()}
JSON: ${JSON.stringify(extended.toJSON())}
convert: ${JSON.stringify(convert(extended, true))}
ajv: ${JSON.stringify(data)}
original: ${JSON.stringify({ foo: value })}
schemastery: ${JSON.stringify(extended({ foo: value }))}
`;
expect(data, extendedinfo).to.deep.equal(extended({ foo: value }));
};
describe('test', () => {
it('string', () => {
test(Schema.string(), 'foo');
test(Schema.string(), 0);
test(Schema.string(), true);
// FIXME this should pass validation
// test(Schema.string().default('foo'), undefined);
test(Schema.string().pattern(/fo+/), 'foo');
test(Schema.string().pattern(/fo+/), 'bar');
});
it('number', () => {
test(Schema.number(), 0);
test(Schema.number(), 'foo');
test(Schema.number(), true);
test(Schema.number().max(10).min(0), 0);
test(Schema.number().max(10).min(0), 5);
test(Schema.number().max(10).min(0), 10);
test(Schema.number().max(10).min(0), -1);
});
it('boolean', () => {
test(Schema.boolean(), false);
test(Schema.boolean(), 'foo');
test(Schema.boolean(), 0);
});
it('any', () => {
test(Schema.any(), 'foo');
test(Schema.any(), 0);
test(Schema.any(), true);
});
it('never', () => {
test(Schema.never(), false);
test(Schema.never(), 'foo');
test(Schema.never(), 0);
});
it('const', () => {
test(Schema.const('foo'), 'foo');
test(Schema.const('foo'), 'bar');
});
it('object', () => {
test(Schema.object({ foo: Schema.string() }), { foo: 'bar' });
test(Schema.object({ foo: Schema.string() }), { foo: 0 });
});
it('dict', () => {
test(Schema.dict(Schema.string()), { foo: 'bar' });
test(Schema.dict(Schema.string()), { foo: 0 });
});
it('array', () => {
test(Schema.array(Schema.string()), { foo: 'bar' });
test(Schema.array(Schema.string()), ['foo']);
test(Schema.array(Schema.string()), [0]);
test(Schema.array(Schema.string()), 'foo');
});
it('union', () => {
test(Schema.union([Schema.string(), Schema.number()]), 'foo');
test(Schema.union([Schema.string(), Schema.number()]), 0);
test(Schema.union([Schema.string(), Schema.number()]), true);
});
it('intersect', () => {
test(Schema.intersect([Schema.string(), Schema.const('foo')]), 'foo');
test(Schema.intersect([Schema.string(), Schema.number()]), 0);
});
});