UNPKG

2.59 kBPlain TextView Raw
1import {SchemaObject} from "../types"
2
3type MetaSchema = (root: boolean) => SchemaObject
4
5const shared: MetaSchema = (root) => {
6 const sch: SchemaObject = {
7 nullable: {type: "boolean"},
8 metadata: {
9 optionalProperties: {
10 union: {elements: {ref: "schema"}},
11 },
12 additionalProperties: true,
13 },
14 }
15 if (root) sch.definitions = {values: {ref: "schema"}}
16 return sch
17}
18
19const emptyForm: MetaSchema = (root) => ({
20 optionalProperties: shared(root),
21})
22
23const refForm: MetaSchema = (root) => ({
24 properties: {
25 ref: {type: "string"},
26 },
27 optionalProperties: shared(root),
28})
29
30const typeForm: MetaSchema = (root) => ({
31 properties: {
32 type: {
33 enum: [
34 "boolean",
35 "timestamp",
36 "string",
37 "float32",
38 "float64",
39 "int8",
40 "uint8",
41 "int16",
42 "uint16",
43 "int32",
44 "uint32",
45 ],
46 },
47 },
48 optionalProperties: shared(root),
49})
50
51const enumForm: MetaSchema = (root) => ({
52 properties: {
53 enum: {elements: {type: "string"}},
54 },
55 optionalProperties: shared(root),
56})
57
58const elementsForm: MetaSchema = (root) => ({
59 properties: {
60 elements: {ref: "schema"},
61 },
62 optionalProperties: shared(root),
63})
64
65const propertiesForm: MetaSchema = (root) => ({
66 properties: {
67 properties: {values: {ref: "schema"}},
68 },
69 optionalProperties: {
70 optionalProperties: {values: {ref: "schema"}},
71 additionalProperties: {type: "boolean"},
72 ...shared(root),
73 },
74})
75
76const optionalPropertiesForm: MetaSchema = (root) => ({
77 properties: {
78 optionalProperties: {values: {ref: "schema"}},
79 },
80 optionalProperties: {
81 additionalProperties: {type: "boolean"},
82 ...shared(root),
83 },
84})
85
86const discriminatorForm: MetaSchema = (root) => ({
87 properties: {
88 discriminator: {type: "string"},
89 mapping: {
90 values: {
91 metadata: {
92 union: [propertiesForm(false), optionalPropertiesForm(false)],
93 },
94 },
95 },
96 },
97 optionalProperties: shared(root),
98})
99
100const valuesForm: MetaSchema = (root) => ({
101 properties: {
102 values: {ref: "schema"},
103 },
104 optionalProperties: shared(root),
105})
106
107const schema: MetaSchema = (root) => ({
108 metadata: {
109 union: [
110 emptyForm,
111 refForm,
112 typeForm,
113 enumForm,
114 elementsForm,
115 propertiesForm,
116 optionalPropertiesForm,
117 discriminatorForm,
118 valuesForm,
119 ].map((s) => s(root)),
120 },
121})
122
123const jtdMetaSchema: SchemaObject = {
124 definitions: {
125 schema: schema(false),
126 },
127 ...schema(true),
128}
129
130export default jtdMetaSchema