-
Notifications
You must be signed in to change notification settings - Fork 327
/
JsonSchema.ts
208 lines (180 loc) · 5.67 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
import * as C from 'fp-ts/lib/Const'
import { identity } from 'fp-ts/lib/function'
import { pipe } from 'fp-ts/lib/pipeable'
import * as R from 'fp-ts/lib/ReadonlyRecord'
import { JSONSchema7 } from 'json-schema'
import * as S from '../src/Schemable'
// -------------------------------------------------------------------------------------
// model
// -------------------------------------------------------------------------------------
export interface JsonSchema<A> {
readonly compile: (definitions?: Record<string, JSONSchema7 | undefined>) => C.Const<JSONSchema7, A>
}
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
export function literal<A extends readonly [S.Literal, ...ReadonlyArray<S.Literal>]>(
...values: A
): JsonSchema<A[number]> {
return {
compile: () => C.make({ enum: [...values] })
}
}
// -------------------------------------------------------------------------------------
// primitives
// -------------------------------------------------------------------------------------
export const string: JsonSchema<string> = {
compile: () => C.make({ type: 'string' })
}
export const number: JsonSchema<number> = {
compile: () => C.make({ type: 'number' })
}
export const boolean: JsonSchema<boolean> = {
compile: () => C.make({ type: 'boolean' })
}
// tslint:disable-next-line: readonly-array
export const UnknownArray: JsonSchema<Array<unknown>> = {
compile: () => C.make({ type: 'array' })
}
export const UnknownRecord: JsonSchema<Record<string, unknown>> = {
compile: () => C.make({ type: 'object' })
}
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
const nullJsonSchema: JsonSchema<null> = {
compile: () => C.make({ enum: [null] })
}
export function nullable<A>(or: JsonSchema<A>): JsonSchema<null | A> {
return union(nullJsonSchema, or)
}
export function struct<A>(properties: { [K in keyof A]: JsonSchema<A[K]> }): JsonSchema<A> {
return {
compile: (lazy) =>
C.make({
type: 'object',
properties: pipe(
properties,
R.map<JsonSchema<unknown>, JSONSchema7>((p) => p.compile(lazy))
),
required: Object.keys(properties)
})
}
}
export function partial<A>(properties: { [K in keyof A]: JsonSchema<A[K]> }): JsonSchema<Partial<A>> {
return {
compile: (lazy) =>
C.make({
type: 'object',
properties: pipe(
properties,
R.map<JsonSchema<unknown>, JSONSchema7>((p) => p.compile(lazy))
)
})
}
}
export function record<A>(codomain: JsonSchema<A>): JsonSchema<Record<string, A>> {
return {
compile: (lazy) =>
C.make({
type: 'object',
additionalProperties: codomain.compile(lazy)
})
}
}
// tslint:disable-next-line: readonly-array
export function array<A>(items: JsonSchema<A>): JsonSchema<Array<A>> {
return {
compile: (lazy) =>
C.make({
type: 'array',
items: items.compile(lazy)
})
}
}
export function tuple<A extends ReadonlyArray<unknown>>(
...components: { [K in keyof A]: JsonSchema<A[K]> }
): JsonSchema<A> {
const len = components.length
return {
compile: (lazy) =>
C.make({
type: 'array',
items: len > 0 ? components.map((c) => c.compile(lazy)) : undefined,
minItems: len,
maxItems: len
})
}
}
export const intersect = <B>(right: JsonSchema<B>) => <A>(left: JsonSchema<A>): JsonSchema<A & B> => ({
compile: (lazy) => C.make({ allOf: [left.compile(lazy), right.compile(lazy)] })
})
export function sum<T extends string>(
_tag: T
): <A>(members: { [K in keyof A]: JsonSchema<A[K] & Record<T, K>> }) => JsonSchema<A[keyof A]> {
return (members: Record<string, JsonSchema<unknown>>) => {
return {
compile: (lazy) => C.make({ anyOf: Object.keys(members).map((k) => members[k].compile(lazy)) })
}
}
}
export function lazy<A>(id: string, f: () => JsonSchema<A>): JsonSchema<A> {
const $ref = `#/definitions/${id}`
return {
compile: (definitions) => {
if (definitions !== undefined) {
if (definitions.hasOwnProperty(id)) {
return C.make({ $ref })
}
definitions[id] = undefined
return (definitions[id] = f().compile(definitions))
} else {
definitions = { [id]: undefined }
definitions[id] = f().compile(definitions)
return C.make({
definitions,
$ref
})
}
}
}
}
export const readonly: <A>(arb: JsonSchema<A>) => JsonSchema<Readonly<A>> = identity
export function union<A extends readonly [unknown, ...ReadonlyArray<unknown>]>(
...members: { [K in keyof A]: JsonSchema<A[K]> }
): JsonSchema<A[number]> {
return {
compile: (lazy) => C.make({ anyOf: members.map((m) => m.compile(lazy)) })
}
}
// -------------------------------------------------------------------------------------
// instances
// -------------------------------------------------------------------------------------
export const URI = 'io-ts/JsonSchema'
export type URI = typeof URI
declare module 'fp-ts/lib/HKT' {
interface URItoKind<A> {
readonly [URI]: JsonSchema<A>
}
}
export const Schemable: S.Schemable1<URI> & S.WithUnknownContainers1<URI> & S.WithUnion1<URI> = {
URI,
literal,
string,
number,
boolean,
UnknownArray,
UnknownRecord,
nullable,
type: struct,
struct,
partial,
record,
array,
tuple: tuple as S.Schemable1<URI>['tuple'],
intersect,
sum,
lazy,
readonly,
union: union as S.WithUnion1<URI>['union']
}