This repository has been archived by the owner on Oct 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonschema.ts
255 lines (203 loc) · 6.72 KB
/
jsonschema.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import type * as HKT from "https://deno.land/x/fun@v1.0.0/hkt.ts";
import type * as TC from "https://deno.land/x/fun@v1.0.0/type_classes.ts";
import type * as SC from "https://deno.land/x/fun@v1.0.0/schemable/schemable.ts";
import type { State } from "https://deno.land/x/fun@v1.0.0/state.ts";
import type { NonEmptyRecord } from "https://deno.land/x/fun@v1.0.0/types.ts";
import * as S from "https://deno.land/x/fun@v1.0.0/state.ts";
import {
createSequenceStruct,
createSequenceTuple,
} from "https://deno.land/x/fun@v1.0.0/sequence.ts";
import { constant, pipe } from "https://deno.land/x/fun@v1.0.0/fns.ts";
import type * as Json from "./types.ts";
/*******************************************************************************
* Types
******************************************************************************/
export type JsonSchema<A> = State<Json.Definitions, Json.Type>;
export type TypeOf<T> = T extends JsonSchema<infer A> ? A : never;
type NonEmptyArray<T> = readonly [T, ...T[]];
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "JsonSchema";
export type URI = typeof URI;
declare module "https://deno.land/x/fun@v1.0.0/hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: JsonSchema<_[0]>;
}
}
/*******************************************************************************
* Utilities
******************************************************************************/
const { concat }: TC.Semigroup<Json.Definitions> = {
concat: (a) => (b) => Object.assign({}, a, b),
};
const sequenceStruct = createSequenceStruct(S.Apply);
const sequenceTuple = createSequenceTuple(S.Apply);
/*******************************************************************************
* Constructors
******************************************************************************/
const of = <A = never>(t: Json.Type): JsonSchema<A> => S.of(t);
/*******************************************************************************
* Schemables
******************************************************************************/
export const UnknownSchemable: SC.UnknownSchemable<URI> = {
unknown: constant(of({})),
};
export const StringSchemable: SC.StringSchemable<URI> = {
string: constant(of({ type: "string" })),
};
export const NumberSchemable: SC.NumberSchemable<URI> = {
number: constant(of({ type: "number" })),
};
export const BooleanSchemable: SC.BooleanSchemable<URI> = {
boolean: constant(of({ type: "boolean" })),
};
export const LiteralSchemable: SC.LiteralSchemable<URI> = {
literal: (...schemas) => of({ enum: schemas }),
};
export const NullableSchemable: SC.NullableSchemable<URI> = {
nullable: (or) =>
pipe(
sequenceTuple(or, of({ type: "null" })),
S.map((anyOf) => ({ anyOf })),
),
};
// TODO expand on this to be more specific
export const UndefinableSchemable: SC.UndefinableSchemable<URI> = {
undefinable: (or) =>
pipe(
sequenceTuple(or, of({})),
S.map((anyOf) => ({ anyOf })),
),
};
export const RecordSchemable: SC.RecordSchemable<URI> = {
record: (codomain) =>
pipe(
codomain,
S.map((additionalProperties) => ({
type: "object",
properties: {},
additionalProperties,
})),
),
};
export const ArraySchemable: SC.ArraySchemable<URI> = {
array: (item) =>
pipe(
item,
S.map((items) => ({ type: "array", items })),
),
};
export const TupleSchemable: SC.TupleSchemable<URI> = {
tuple: (...components) =>
pipe(
sequenceTuple(
...(components as unknown as NonEmptyArray<
JsonSchema<keyof typeof components>
>),
),
S.map((items) => ({ type: "array", items })),
),
};
export const StructSchemable: SC.StructSchemable<URI> = {
struct: (props) =>
pipe(
sequenceStruct(props as Record<string, JsonSchema<unknown>>),
S.map((properties) => ({
type: "object",
properties,
required: Object.keys(properties).sort() as unknown as NonEmptyArray<
string
>,
})),
),
};
export const PartialSchemable: SC.PartialSchemable<URI> = {
partial: (props) =>
pipe(
sequenceStruct(props as Record<string, JsonSchema<unknown>>),
S.map((properties) => ({
type: "object",
properties,
})),
),
};
export const IntersectionSchemable: SC.IntersectSchemable<URI> = {
intersect: (and) =>
(ta) =>
pipe(
sequenceTuple(and, ta),
S.map((allOf) => ({ allOf })),
),
};
export const UnionSchemable: SC.UnionSchemable<URI> = {
union: (or) =>
(ta) =>
pipe(
sequenceTuple(or, ta),
S.map((anyOf) => ({ anyOf })),
),
};
export const LazySchemable: SC.LazySchemable<URI> = {
lazy: (id, f) => {
let returnRef = false;
const ref: Json.Ref = { $ref: `#/definitions/${id}` };
return (s) => {
if (returnRef) {
return [ref, s];
}
returnRef = true;
const [schema, defs] = f()(s);
let definitions = concat({ [id]: schema })(defs);
definitions = concat(definitions)(s);
return [ref, definitions];
};
},
};
/*******************************************************************************
* Utilities
******************************************************************************/
export const print = <A>(jsonschema: JsonSchema<A>): Json.Type => {
const [schema, definitions] = jsonschema({});
return {
...schema,
definitions,
};
};
/*******************************************************************************
* Modules
******************************************************************************/
export const Schemable: SC.Schemable<URI> = {
...UnknownSchemable,
...StringSchemable,
...NumberSchemable,
...BooleanSchemable,
...LiteralSchemable,
...NullableSchemable,
...UndefinableSchemable,
...RecordSchemable,
...ArraySchemable,
...TupleSchemable,
...StructSchemable,
...PartialSchemable,
...IntersectionSchemable,
...UnionSchemable,
...LazySchemable,
};
export const unknown = Schemable.unknown();
export const string = Schemable.string();
export const number = Schemable.number();
export const boolean = Schemable.boolean();
export const literal = Schemable.literal;
export const nullable = Schemable.nullable;
export const undefinable = Schemable.undefinable;
export const record = Schemable.record;
export const array = Schemable.array;
export const tuple = Schemable.tuple;
export const struct = Schemable.struct;
export const partial = Schemable.partial;
export const intersect = Schemable.intersect;
export const union = Schemable.union;
export const lazy = Schemable.lazy;