/
test.ts
133 lines (127 loc) · 3.31 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import test from "ava";
import { tojsonschema } from "./";
import * as rt from "runtypes";
import { validate } from "jsonschema";
const tjs = tojsonschema;
test("basic", (t) => {
// readme example - start
// const myRtSchema = rt.Record({ foo: rt.Literal("bar") });
// const myjsonschema = tojsonschema(myRtSchema);
// console.log(JSON.stringify(myjsonschema));
// const out = { type: "object", properties: { foo: { const: "bar" } } };
// readme example - end
const fooSchema = rt.Literal("foo");
fooSchema.meta = { description: "foo_description", defaultValue: "__FOO__" };
t.deepEqual(tjs(fooSchema), {
description: "foo_description",
const: "foo",
default: "__FOO__",
});
t.deepEqual(tjs(rt.Union(rt.Literal("foo"), rt.Literal("bar"))), {
anyOf: [{ const: "foo" }, { const: "bar" }],
});
});
test("complex", (t) => {
const rtSchema = rt.Union(
rt.Record({
arrayField: rt.Array(
rt.Partial({
partialField1: rt.String,
})
),
foo: rt.Dictionary(rt.Number.withConstraint(() => true)),
testBool: rt.Boolean,
testNull: rt.Null,
}),
rt.Record({
testBigInt: rt.BigInt,
testBrand: rt.Brand("brandy", rt.Literal("test-branded-literal")),
})
);
const actualJsonSchema = tjs(rtSchema);
const expected = {
anyOf: [
{
type: "object",
properties: {
arrayField: {
type: "array",
items: {
type: "object",
properties: { partialField1: { type: "string" } },
},
},
foo: { type: "object", properties: { builtin: { type: "number" } } },
testBool: { type: "boolean" },
testNull: { const: null },
},
required: ["arrayField", "foo", "testBool", "testNull"],
},
{
type: "object",
properties: {
testBigInt: { type: "integer" },
testBrand: { const: "test-branded-literal" },
},
required: ["testBigInt", "testBrand"],
},
],
};
t.deepEqual(actualJsonSchema, expected);
const demoData: rt.Static<typeof rtSchema> = {
testBool: true,
testNull: null,
foo: {
bar: 1,
},
arrayField: [
{
partialField1: "ok",
},
{},
],
};
try {
const checkedDemoData = rtSchema.check(demoData);
const result = validate(checkedDemoData, actualJsonSchema);
t.is(result.valid, true);
} catch (err) {
t.fail(String((err as { details?: unknown })?.details || err));
}
});
test("optional", (t) => {
const rtSchema = rt.Record({
bar: rt.Number,
baz: rt.String.withConstraint(() => true).optional(),
foo: rt.Boolean.optional().withGuard((_v): _v is boolean => true),
});
const actualJsonSchema = tjs(rtSchema);
const expected = {
properties: {
bar: {
type: "number",
},
baz: {
type: "string",
},
foo: {
type: "boolean",
},
},
required: ["bar"],
type: "object",
};
t.deepEqual(actualJsonSchema, expected);
});
test("readme demo", (t) => {
const myRtSchema = rt.Record({ foo: rt.Literal("bar") });
const myjsonschema = tojsonschema(myRtSchema);
t.deepEqual(
{
type: "object",
properties: { foo: { const: "bar" } },
required: ["foo"],
},
myjsonschema
);
});