@llm4ts/flow 2.54.1 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Activity.d.ts +1 -1
- package/dist/Activity.d.ts.map +1 -1
- package/dist/Activity.js +20 -0
- package/dist/Activity.js.map +1 -1
- package/dist/EstimatedUsage.d.ts.map +1 -1
- package/dist/EstimatedUsage.js +0 -1
- package/dist/EstimatedUsage.js.map +1 -1
- package/dist/Pack.d.ts +11 -0
- package/dist/Pack.d.ts.map +1 -1
- package/dist/Pack.js +2 -0
- package/dist/Pack.js.map +1 -1
- package/dist/Replay.d.ts.map +1 -1
- package/dist/Replay.js +1 -4
- package/dist/Replay.js.map +1 -1
- package/dist/RosterSeats.d.ts.map +1 -1
- package/dist/RosterSeats.js +0 -1
- package/dist/RosterSeats.js.map +1 -1
- package/dist/Timing.d.ts.map +1 -1
- package/dist/Timing.js +0 -1
- package/dist/Timing.js.map +1 -1
- package/dist/Transcript.d.ts.map +1 -1
- package/dist/Transcript.js +0 -1
- package/dist/Transcript.js.map +1 -1
- package/dist/TransientRetry.d.ts.map +1 -1
- package/dist/TransientRetry.js +0 -1
- package/dist/TransientRetry.js.map +1 -1
- package/dist/endpoint/Auth.d.ts +194 -0
- package/dist/endpoint/Auth.d.ts.map +1 -0
- package/dist/endpoint/Auth.js +302 -0
- package/dist/endpoint/Auth.js.map +1 -0
- package/dist/endpoint/Endpoints.d.ts +21 -0
- package/dist/endpoint/Endpoints.d.ts.map +1 -0
- package/dist/endpoint/Endpoints.js +34 -0
- package/dist/endpoint/Endpoints.js.map +1 -0
- package/dist/endpoint/Transport.d.ts +90 -0
- package/dist/endpoint/Transport.d.ts.map +1 -0
- package/dist/endpoint/Transport.js +306 -0
- package/dist/endpoint/Transport.js.map +1 -0
- package/dist/tools/Builtin.d.ts +5 -0
- package/dist/tools/Builtin.d.ts.map +1 -0
- package/dist/tools/Builtin.js +7 -0
- package/dist/tools/Builtin.js.map +1 -0
- package/dist/tools/Diff.d.ts +8 -0
- package/dist/tools/Diff.d.ts.map +1 -0
- package/dist/tools/Diff.js +45 -0
- package/dist/tools/Diff.js.map +1 -0
- package/dist/tools/HttpCall.d.ts +14 -0
- package/dist/tools/HttpCall.d.ts.map +1 -0
- package/dist/tools/HttpCall.js +101 -0
- package/dist/tools/HttpCall.js.map +1 -0
- package/dist/tools/JsonSchemaCheck.d.ts +36 -0
- package/dist/tools/JsonSchemaCheck.d.ts.map +1 -0
- package/dist/tools/JsonSchemaCheck.js +342 -0
- package/dist/tools/JsonSchemaCheck.js.map +1 -0
- package/dist/tools/JsonValidate.d.ts +11 -0
- package/dist/tools/JsonValidate.d.ts.map +1 -0
- package/dist/tools/JsonValidate.js +84 -0
- package/dist/tools/JsonValidate.js.map +1 -0
- package/dist/tools/Tool.d.ts +126 -0
- package/dist/tools/Tool.d.ts.map +1 -0
- package/dist/tools/Tool.js +218 -0
- package/dist/tools/Tool.js.map +1 -0
- package/dist/tools/TreeDiff.d.ts +42 -0
- package/dist/tools/TreeDiff.d.ts.map +1 -0
- package/dist/tools/TreeDiff.js +276 -0
- package/dist/tools/TreeDiff.js.map +1 -0
- package/package.json +12 -4
- package/src/Activity.ts +22 -0
- package/src/EstimatedUsage.ts +0 -1
- package/src/Pack.ts +16 -0
- package/src/Replay.ts +1 -7
- package/src/RosterSeats.ts +0 -2
- package/src/Timing.ts +0 -10
- package/src/Transcript.ts +0 -1
- package/src/TransientRetry.ts +0 -2
- package/src/endpoint/Auth.ts +446 -0
- package/src/endpoint/Endpoints.ts +54 -0
- package/src/endpoint/Transport.ts +456 -0
- package/src/tools/Builtin.ts +10 -0
- package/src/tools/Diff.ts +61 -0
- package/src/tools/HttpCall.ts +124 -0
- package/src/tools/JsonSchemaCheck.ts +457 -0
- package/src/tools/JsonValidate.ts +106 -0
- package/src/tools/Tool.ts +351 -0
- package/src/tools/TreeDiff.ts +386 -0
- package/dist/WorkspaceLayout.d.ts +0 -3
- package/dist/WorkspaceLayout.d.ts.map +0 -1
- package/dist/WorkspaceLayout.js +0 -19
- package/dist/WorkspaceLayout.js.map +0 -1
- package/dist/WorkspaceTools.d.ts +0 -4
- package/dist/WorkspaceTools.d.ts.map +0 -1
- package/dist/WorkspaceTools.js +0 -130
- package/dist/WorkspaceTools.js.map +0 -1
- package/src/WorkspaceLayout.ts +0 -22
- package/src/WorkspaceTools.ts +0 -188
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A JSON Schema subset validator for the schema objects a tool is likely to
|
|
3
|
+
* meet: draft-07, 2020-12 and OpenAPI 3.0/3.1 schema objects. It reports every
|
|
4
|
+
* issue it finds in one pass, with JSONPath-style locations, and never throws
|
|
5
|
+
* on a malformed schema: a bad `pattern`, a dangling `$ref` or a reference
|
|
6
|
+
* cycle become issues of their own.
|
|
7
|
+
*
|
|
8
|
+
* `format` and every keyword not listed in `validateJson`'s documentation are
|
|
9
|
+
* ignored, so a schema that uses them still validates the parts this module
|
|
10
|
+
* understands.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface JsonIssue {
|
|
14
|
+
readonly path: string
|
|
15
|
+
readonly detail: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface JsonSchemaCheckOptions {
|
|
19
|
+
/**
|
|
20
|
+
* The document that local `$ref` pointers (`#/components/schemas/X`,
|
|
21
|
+
* `#/definitions/X`, `#/$defs/X`) resolve against. Defaults to the schema
|
|
22
|
+
* itself.
|
|
23
|
+
*/
|
|
24
|
+
readonly root?: unknown
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Schema applications nested deeper than this are reported, not followed. */
|
|
28
|
+
const maxDepth = 64
|
|
29
|
+
|
|
30
|
+
const nestingTooDeep = "schema nesting too deep"
|
|
31
|
+
|
|
32
|
+
type JsonRecord = Record<string, unknown>
|
|
33
|
+
|
|
34
|
+
const isRecord = (value: unknown): value is JsonRecord =>
|
|
35
|
+
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
36
|
+
|
|
37
|
+
const jsonTypeOf = (value: unknown): string => {
|
|
38
|
+
if (value === null) return "null"
|
|
39
|
+
if (Array.isArray(value)) return "array"
|
|
40
|
+
return typeof value
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const identifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/
|
|
44
|
+
|
|
45
|
+
const childPath = (path: string, key: string): string =>
|
|
46
|
+
identifier.test(key) ? `${path}.${key}` : `${path}[${JSON.stringify(key)}]`
|
|
47
|
+
|
|
48
|
+
const indexPath = (path: string, index: number): string => `${path}[${index}]`
|
|
49
|
+
|
|
50
|
+
const renderLimit = 80
|
|
51
|
+
|
|
52
|
+
const render = (value: unknown): string => {
|
|
53
|
+
const text = value === undefined ? "undefined" : JSON.stringify(value)
|
|
54
|
+
return text.length > renderLimit ? `${text.slice(0, renderLimit - 1)}…` : text
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const jsonEquals = (left: unknown, right: unknown): boolean => {
|
|
58
|
+
if (left === right) return true
|
|
59
|
+
if (typeof left === "number" && typeof right === "number") return left === right
|
|
60
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
61
|
+
return (
|
|
62
|
+
left.length === right.length && left.every((item, index) => jsonEquals(item, right[index]))
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
if (isRecord(left) && isRecord(right)) {
|
|
66
|
+
const leftKeys = Object.keys(left).filter((key) => left[key] !== undefined)
|
|
67
|
+
const rightKeys = Object.keys(right).filter((key) => right[key] !== undefined)
|
|
68
|
+
return (
|
|
69
|
+
leftKeys.length === rightKeys.length &&
|
|
70
|
+
leftKeys.every((key) => key in right && jsonEquals(left[key], right[key]))
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
return false
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const hasValue = (record: JsonRecord, key: string): boolean =>
|
|
77
|
+
Object.prototype.hasOwnProperty.call(record, key) && record[key] !== undefined
|
|
78
|
+
|
|
79
|
+
const matchesType = (name: string, value: unknown): boolean => {
|
|
80
|
+
switch (name) {
|
|
81
|
+
case "integer":
|
|
82
|
+
return typeof value === "number" && Number.isInteger(value)
|
|
83
|
+
case "number":
|
|
84
|
+
return typeof value === "number"
|
|
85
|
+
case "string":
|
|
86
|
+
return typeof value === "string"
|
|
87
|
+
case "boolean":
|
|
88
|
+
return typeof value === "boolean"
|
|
89
|
+
case "null":
|
|
90
|
+
return value === null
|
|
91
|
+
case "array":
|
|
92
|
+
return Array.isArray(value)
|
|
93
|
+
case "object":
|
|
94
|
+
return isRecord(value)
|
|
95
|
+
default:
|
|
96
|
+
return true
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const typeNames = (type: unknown): ReadonlyArray<string> => {
|
|
101
|
+
if (typeof type === "string") return [type]
|
|
102
|
+
if (Array.isArray(type)) return type.filter((name): name is string => typeof name === "string")
|
|
103
|
+
return []
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Resolves a local JSON pointer (`#/a/b~1c`) against a root document. */
|
|
107
|
+
const resolvePointer = (
|
|
108
|
+
root: unknown,
|
|
109
|
+
ref: string
|
|
110
|
+
): { readonly found: boolean; readonly value: unknown } => {
|
|
111
|
+
if (!ref.startsWith("#")) return { found: false, value: undefined }
|
|
112
|
+
const fragment = ref.slice(1)
|
|
113
|
+
if (fragment === "") return { found: true, value: root }
|
|
114
|
+
if (!fragment.startsWith("/")) return { found: false, value: undefined }
|
|
115
|
+
let current: unknown = root
|
|
116
|
+
for (const rawToken of fragment.slice(1).split("/")) {
|
|
117
|
+
let token = rawToken
|
|
118
|
+
try {
|
|
119
|
+
token = decodeURIComponent(rawToken)
|
|
120
|
+
} catch {
|
|
121
|
+
token = rawToken
|
|
122
|
+
}
|
|
123
|
+
token = token.replace(/~1/g, "/").replace(/~0/g, "~")
|
|
124
|
+
if (Array.isArray(current)) {
|
|
125
|
+
const index = Number(token)
|
|
126
|
+
if (!Number.isInteger(index) || index < 0 || index >= current.length) {
|
|
127
|
+
return { found: false, value: undefined }
|
|
128
|
+
}
|
|
129
|
+
current = current[index]
|
|
130
|
+
} else if (isRecord(current) && Object.prototype.hasOwnProperty.call(current, token)) {
|
|
131
|
+
current = current[token]
|
|
132
|
+
} else {
|
|
133
|
+
return { found: false, value: undefined }
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return { found: true, value: current }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Validation state. Branches of `anyOf`/`oneOf`/`not` validate into a fresh
|
|
141
|
+
* collector so their internal issues never leak, while `once` notes (a cycle,
|
|
142
|
+
* an invalid pattern) always surface at the top level.
|
|
143
|
+
*/
|
|
144
|
+
interface Collector {
|
|
145
|
+
readonly root: unknown
|
|
146
|
+
readonly issues: Array<JsonIssue>
|
|
147
|
+
readonly seen: Set<string>
|
|
148
|
+
readonly once: Set<string>
|
|
149
|
+
readonly patterns: Map<string, RegExp | undefined>
|
|
150
|
+
readonly top: Collector | undefined
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const report = (collector: Collector, path: string, detail: string): void => {
|
|
154
|
+
const key = `${path}\u0000${detail}`
|
|
155
|
+
if (collector.seen.has(key)) return
|
|
156
|
+
collector.seen.add(key)
|
|
157
|
+
collector.issues.push({ path, detail })
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const reportOnce = (collector: Collector, path: string, detail: string): void => {
|
|
161
|
+
if (collector.once.has(detail)) return
|
|
162
|
+
collector.once.add(detail)
|
|
163
|
+
report(collector.top ?? collector, path, detail)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const branch = (collector: Collector): Collector => ({
|
|
167
|
+
root: collector.root,
|
|
168
|
+
issues: [],
|
|
169
|
+
seen: new Set(),
|
|
170
|
+
once: collector.once,
|
|
171
|
+
patterns: collector.patterns,
|
|
172
|
+
top: collector.top ?? collector
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
const passes = (
|
|
176
|
+
collector: Collector,
|
|
177
|
+
schema: unknown,
|
|
178
|
+
value: unknown,
|
|
179
|
+
path: string,
|
|
180
|
+
depth: number
|
|
181
|
+
) => {
|
|
182
|
+
const probe = branch(collector)
|
|
183
|
+
validate(probe, schema, value, path, depth)
|
|
184
|
+
return probe.issues.length === 0
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const compilePattern = (collector: Collector, source: string): RegExp | undefined => {
|
|
188
|
+
const cached = collector.patterns.get(source)
|
|
189
|
+
if (cached !== undefined) return cached
|
|
190
|
+
let compiled: RegExp | undefined
|
|
191
|
+
try {
|
|
192
|
+
compiled = new RegExp(source, "u")
|
|
193
|
+
} catch {
|
|
194
|
+
compiled = undefined
|
|
195
|
+
}
|
|
196
|
+
collector.patterns.set(source, compiled)
|
|
197
|
+
return compiled
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const validateString = (
|
|
201
|
+
collector: Collector,
|
|
202
|
+
schema: JsonRecord,
|
|
203
|
+
value: string,
|
|
204
|
+
path: string
|
|
205
|
+
): void => {
|
|
206
|
+
const length = Array.from(value).length
|
|
207
|
+
if (typeof schema.minLength === "number" && length < schema.minLength) {
|
|
208
|
+
report(collector, path, `expected at least ${schema.minLength} characters, found ${length}`)
|
|
209
|
+
}
|
|
210
|
+
if (typeof schema.maxLength === "number" && length > schema.maxLength) {
|
|
211
|
+
report(collector, path, `expected at most ${schema.maxLength} characters, found ${length}`)
|
|
212
|
+
}
|
|
213
|
+
if (typeof schema.pattern === "string") {
|
|
214
|
+
const pattern = compilePattern(collector, schema.pattern)
|
|
215
|
+
if (pattern === undefined) {
|
|
216
|
+
reportOnce(collector, path, `invalid pattern ${JSON.stringify(schema.pattern)}`)
|
|
217
|
+
} else if (!pattern.test(value)) {
|
|
218
|
+
report(
|
|
219
|
+
collector,
|
|
220
|
+
path,
|
|
221
|
+
`value ${render(value)} does not match pattern ${JSON.stringify(schema.pattern)}`
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const validateNumber = (
|
|
228
|
+
collector: Collector,
|
|
229
|
+
schema: JsonRecord,
|
|
230
|
+
value: number,
|
|
231
|
+
path: string
|
|
232
|
+
): void => {
|
|
233
|
+
const rendered = render(value)
|
|
234
|
+
if (typeof schema.minimum === "number") {
|
|
235
|
+
if (schema.exclusiveMinimum === true) {
|
|
236
|
+
if (value <= schema.minimum) {
|
|
237
|
+
report(
|
|
238
|
+
collector,
|
|
239
|
+
path,
|
|
240
|
+
`value ${rendered} is not greater than the exclusive minimum ${schema.minimum}`
|
|
241
|
+
)
|
|
242
|
+
}
|
|
243
|
+
} else if (value < schema.minimum) {
|
|
244
|
+
report(collector, path, `value ${rendered} is less than the minimum ${schema.minimum}`)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (typeof schema.exclusiveMinimum === "number" && value <= schema.exclusiveMinimum) {
|
|
248
|
+
report(
|
|
249
|
+
collector,
|
|
250
|
+
path,
|
|
251
|
+
`value ${rendered} is not greater than the exclusive minimum ${schema.exclusiveMinimum}`
|
|
252
|
+
)
|
|
253
|
+
}
|
|
254
|
+
if (typeof schema.maximum === "number") {
|
|
255
|
+
if (schema.exclusiveMaximum === true) {
|
|
256
|
+
if (value >= schema.maximum) {
|
|
257
|
+
report(
|
|
258
|
+
collector,
|
|
259
|
+
path,
|
|
260
|
+
`value ${rendered} is not less than the exclusive maximum ${schema.maximum}`
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
} else if (value > schema.maximum) {
|
|
264
|
+
report(collector, path, `value ${rendered} is greater than the maximum ${schema.maximum}`)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (typeof schema.exclusiveMaximum === "number" && value >= schema.exclusiveMaximum) {
|
|
268
|
+
report(
|
|
269
|
+
collector,
|
|
270
|
+
path,
|
|
271
|
+
`value ${rendered} is not less than the exclusive maximum ${schema.exclusiveMaximum}`
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
if (typeof schema.multipleOf === "number" && schema.multipleOf > 0) {
|
|
275
|
+
const quotient = value / schema.multipleOf
|
|
276
|
+
if (Math.abs(quotient - Math.round(quotient)) > 1e-9) {
|
|
277
|
+
report(collector, path, `value ${rendered} is not a multiple of ${schema.multipleOf}`)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const validateArray = (
|
|
283
|
+
collector: Collector,
|
|
284
|
+
schema: JsonRecord,
|
|
285
|
+
value: ReadonlyArray<unknown>,
|
|
286
|
+
path: string,
|
|
287
|
+
depth: number
|
|
288
|
+
): void => {
|
|
289
|
+
if (typeof schema.minItems === "number" && value.length < schema.minItems) {
|
|
290
|
+
report(collector, path, `expected at least ${schema.minItems} items, found ${value.length}`)
|
|
291
|
+
}
|
|
292
|
+
if (typeof schema.maxItems === "number" && value.length > schema.maxItems) {
|
|
293
|
+
report(collector, path, `expected at most ${schema.maxItems} items, found ${value.length}`)
|
|
294
|
+
}
|
|
295
|
+
if (schema.uniqueItems === true) {
|
|
296
|
+
for (let index = 1; index < value.length; index++) {
|
|
297
|
+
const twin = value.findIndex(
|
|
298
|
+
(item, earlier) => earlier < index && jsonEquals(item, value[index])
|
|
299
|
+
)
|
|
300
|
+
if (twin >= 0) {
|
|
301
|
+
report(collector, path, `items are not unique: item ${index} duplicates item ${twin}`)
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const items = schema.items
|
|
306
|
+
if (Array.isArray(items)) {
|
|
307
|
+
items.forEach((itemSchema, index) => {
|
|
308
|
+
if (index < value.length)
|
|
309
|
+
validate(collector, itemSchema, value[index], indexPath(path, index), depth)
|
|
310
|
+
})
|
|
311
|
+
} else if (items !== undefined) {
|
|
312
|
+
value.forEach((item, index) => validate(collector, items, item, indexPath(path, index), depth))
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const validateObject = (
|
|
317
|
+
collector: Collector,
|
|
318
|
+
schema: JsonRecord,
|
|
319
|
+
value: JsonRecord,
|
|
320
|
+
path: string,
|
|
321
|
+
depth: number
|
|
322
|
+
): void => {
|
|
323
|
+
const properties = isRecord(schema.properties) ? schema.properties : {}
|
|
324
|
+
if (Array.isArray(schema.required)) {
|
|
325
|
+
for (const name of schema.required) {
|
|
326
|
+
if (typeof name === "string" && !hasValue(value, name)) {
|
|
327
|
+
report(collector, path, `missing required property ${JSON.stringify(name)}`)
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
for (const key of Object.keys(value)) {
|
|
332
|
+
if (!hasValue(value, key)) continue
|
|
333
|
+
const child = childPath(path, key)
|
|
334
|
+
if (Object.prototype.hasOwnProperty.call(properties, key)) {
|
|
335
|
+
validate(collector, properties[key], value[key], child, depth)
|
|
336
|
+
} else if (schema.additionalProperties === false) {
|
|
337
|
+
report(collector, path, `unexpected property ${JSON.stringify(key)}`)
|
|
338
|
+
} else if (schema.additionalProperties !== undefined && schema.additionalProperties !== true) {
|
|
339
|
+
validate(collector, schema.additionalProperties, value[key], child, depth)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const validate = (
|
|
345
|
+
collector: Collector,
|
|
346
|
+
schema: unknown,
|
|
347
|
+
value: unknown,
|
|
348
|
+
path: string,
|
|
349
|
+
depth: number
|
|
350
|
+
): void => {
|
|
351
|
+
if (depth > maxDepth) {
|
|
352
|
+
reportOnce(collector, path, nestingTooDeep)
|
|
353
|
+
return
|
|
354
|
+
}
|
|
355
|
+
if (schema === true) return
|
|
356
|
+
if (schema === false) {
|
|
357
|
+
report(collector, path, "value is not allowed by a false schema")
|
|
358
|
+
return
|
|
359
|
+
}
|
|
360
|
+
if (!isRecord(schema)) {
|
|
361
|
+
reportOnce(collector, path, `invalid schema of type ${jsonTypeOf(schema)}`)
|
|
362
|
+
return
|
|
363
|
+
}
|
|
364
|
+
const next = depth + 1
|
|
365
|
+
|
|
366
|
+
if (value === null && schema.nullable === true) return
|
|
367
|
+
|
|
368
|
+
if (typeof schema.$ref === "string") {
|
|
369
|
+
const resolved = resolvePointer(collector.root, schema.$ref)
|
|
370
|
+
if (resolved.found) {
|
|
371
|
+
validate(collector, resolved.value, value, path, next)
|
|
372
|
+
} else {
|
|
373
|
+
report(collector, path, `cannot resolve $ref ${JSON.stringify(schema.$ref)}`)
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const expected = typeNames(schema.type)
|
|
378
|
+
if (expected.length > 0 && !expected.some((name) => matchesType(name, value))) {
|
|
379
|
+
report(collector, path, `expected ${expected.join(" or ")}, found ${jsonTypeOf(value)}`)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (
|
|
383
|
+
Array.isArray(schema.enum) &&
|
|
384
|
+
!schema.enum.some((candidate) => jsonEquals(candidate, value))
|
|
385
|
+
) {
|
|
386
|
+
report(
|
|
387
|
+
collector,
|
|
388
|
+
path,
|
|
389
|
+
`value ${render(value)} is not one of [${schema.enum.map(render).join(", ")}]`
|
|
390
|
+
)
|
|
391
|
+
}
|
|
392
|
+
if (hasValue(schema, "const") && !jsonEquals(schema.const, value)) {
|
|
393
|
+
report(collector, path, `value ${render(value)} is not the constant ${render(schema.const)}`)
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (typeof value === "string") validateString(collector, schema, value, path)
|
|
397
|
+
else if (typeof value === "number") validateNumber(collector, schema, value, path)
|
|
398
|
+
else if (Array.isArray(value)) validateArray(collector, schema, value, path, next)
|
|
399
|
+
else if (isRecord(value)) validateObject(collector, schema, value, path, next)
|
|
400
|
+
|
|
401
|
+
if (Array.isArray(schema.allOf)) {
|
|
402
|
+
for (const member of schema.allOf) validate(collector, member, value, path, next)
|
|
403
|
+
}
|
|
404
|
+
if (Array.isArray(schema.anyOf)) {
|
|
405
|
+
const matched = schema.anyOf.some((member) => passes(collector, member, value, path, next))
|
|
406
|
+
if (!matched) {
|
|
407
|
+
report(
|
|
408
|
+
collector,
|
|
409
|
+
path,
|
|
410
|
+
`value does not match any of the ${schema.anyOf.length} anyOf schemas`
|
|
411
|
+
)
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (Array.isArray(schema.oneOf)) {
|
|
415
|
+
const matched = schema.oneOf.filter((member) =>
|
|
416
|
+
passes(collector, member, value, path, next)
|
|
417
|
+
).length
|
|
418
|
+
if (matched !== 1) {
|
|
419
|
+
report(
|
|
420
|
+
collector,
|
|
421
|
+
path,
|
|
422
|
+
`expected exactly one of the ${schema.oneOf.length} oneOf schemas to match, ${matched} matched`
|
|
423
|
+
)
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (schema.not !== undefined && passes(collector, schema.not, value, path, next)) {
|
|
427
|
+
report(collector, path, "value matches the not schema")
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Validates `document` against `schema` and returns every issue found, in
|
|
433
|
+
* document order, without duplicates.
|
|
434
|
+
*
|
|
435
|
+
* Supported keywords: `type` (a name or a list; `integer` is a whole number),
|
|
436
|
+
* `nullable`, `enum`, `const`, `properties`, `required`, `additionalProperties`,
|
|
437
|
+
* `items`, `minItems`, `maxItems`, `uniqueItems`, `minLength`, `maxLength`,
|
|
438
|
+
* `pattern`, `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum`
|
|
439
|
+
* (numeric, or the draft-04 boolean form), `multipleOf`, `allOf`, `anyOf`,
|
|
440
|
+
* `oneOf`, `not`, `$ref` to a local pointer, and the boolean schemas.
|
|
441
|
+
*/
|
|
442
|
+
export const validateJson = (
|
|
443
|
+
schema: unknown,
|
|
444
|
+
document: unknown,
|
|
445
|
+
options?: JsonSchemaCheckOptions
|
|
446
|
+
): ReadonlyArray<JsonIssue> => {
|
|
447
|
+
const collector: Collector = {
|
|
448
|
+
root: options?.root ?? schema,
|
|
449
|
+
issues: [],
|
|
450
|
+
seen: new Set(),
|
|
451
|
+
once: new Set(),
|
|
452
|
+
patterns: new Map(),
|
|
453
|
+
top: undefined
|
|
454
|
+
}
|
|
455
|
+
validate(collector, schema, document, "$", 0)
|
|
456
|
+
return collector.issues
|
|
457
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect"
|
|
2
|
+
import * as Schema from "effect/Schema"
|
|
3
|
+
import { validateJson } from "./JsonSchemaCheck.ts"
|
|
4
|
+
import { ToolError, defineTool, parseJsonDocument, readToolFile, report } from "./Tool.ts"
|
|
5
|
+
|
|
6
|
+
const name = "json-validate"
|
|
7
|
+
|
|
8
|
+
const Input = Schema.Struct({
|
|
9
|
+
document: Schema.String,
|
|
10
|
+
schema: Schema.optionalKey(Schema.String),
|
|
11
|
+
openapi: Schema.optionalKey(Schema.String),
|
|
12
|
+
component: Schema.optionalKey(Schema.String)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const componentOf = (
|
|
16
|
+
openapi: unknown,
|
|
17
|
+
component: string,
|
|
18
|
+
path: string
|
|
19
|
+
): Effect.Effect<unknown, ToolError> => {
|
|
20
|
+
const schemas =
|
|
21
|
+
typeof openapi === "object" &&
|
|
22
|
+
openapi !== null &&
|
|
23
|
+
"components" in openapi &&
|
|
24
|
+
typeof openapi.components === "object" &&
|
|
25
|
+
openapi.components !== null &&
|
|
26
|
+
"schemas" in openapi.components &&
|
|
27
|
+
typeof openapi.components.schemas === "object" &&
|
|
28
|
+
openapi.components.schemas !== null
|
|
29
|
+
? openapi.components.schemas
|
|
30
|
+
: undefined
|
|
31
|
+
if (schemas === undefined) {
|
|
32
|
+
return Effect.fail(new ToolError({ tool: name, message: `${path} has no components.schemas` }))
|
|
33
|
+
}
|
|
34
|
+
const found = Object.entries(schemas).find(([key]) => key === component)
|
|
35
|
+
if (found === undefined) {
|
|
36
|
+
const known = Object.keys(schemas).sort().slice(0, 20).join(", ")
|
|
37
|
+
return Effect.fail(
|
|
38
|
+
new ToolError({
|
|
39
|
+
tool: name,
|
|
40
|
+
message: `${path} has no component schema '${component}'; known: ${known}`
|
|
41
|
+
})
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
return Effect.succeed(found[1])
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Validates a JSON document against a JSON Schema file, or against one
|
|
49
|
+
* component schema of an OpenAPI document (`$ref`s resolve inside it).
|
|
50
|
+
*/
|
|
51
|
+
export const jsonValidate = defineTool({
|
|
52
|
+
name,
|
|
53
|
+
purpose:
|
|
54
|
+
"Validate a JSON document against a JSON Schema file, or against a component schema of an OpenAPI document",
|
|
55
|
+
args: {
|
|
56
|
+
document: { description: "Path of the JSON document to check", kind: "path", required: true },
|
|
57
|
+
schema: { description: "Path of a JSON Schema file", kind: "path" },
|
|
58
|
+
openapi: {
|
|
59
|
+
description: "Path of an OpenAPI document (JSON); use with --component",
|
|
60
|
+
kind: "path"
|
|
61
|
+
},
|
|
62
|
+
component: {
|
|
63
|
+
description: "Name under components.schemas of the OpenAPI document",
|
|
64
|
+
kind: "string"
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
input: Input,
|
|
68
|
+
run: (input) =>
|
|
69
|
+
Effect.gen(function* () {
|
|
70
|
+
const document = yield* parseJsonDocument(
|
|
71
|
+
name,
|
|
72
|
+
input.document,
|
|
73
|
+
yield* readToolFile(name, input.document)
|
|
74
|
+
)
|
|
75
|
+
let schema: unknown
|
|
76
|
+
let root: unknown
|
|
77
|
+
let against: string
|
|
78
|
+
if (input.schema !== undefined) {
|
|
79
|
+
schema = yield* parseJsonDocument(
|
|
80
|
+
name,
|
|
81
|
+
input.schema,
|
|
82
|
+
yield* readToolFile(name, input.schema)
|
|
83
|
+
)
|
|
84
|
+
root = schema
|
|
85
|
+
against = input.schema
|
|
86
|
+
} else if (input.openapi !== undefined) {
|
|
87
|
+
if (input.component === undefined) {
|
|
88
|
+
return yield* new ToolError({ tool: name, message: "--openapi needs --component <name>" })
|
|
89
|
+
}
|
|
90
|
+
root = yield* parseJsonDocument(
|
|
91
|
+
name,
|
|
92
|
+
input.openapi,
|
|
93
|
+
yield* readToolFile(name, input.openapi)
|
|
94
|
+
)
|
|
95
|
+
schema = yield* componentOf(root, input.component, input.openapi)
|
|
96
|
+
against = `${input.openapi}#/components/schemas/${input.component}`
|
|
97
|
+
} else {
|
|
98
|
+
return yield* new ToolError({
|
|
99
|
+
tool: name,
|
|
100
|
+
message: "give --schema <file> or --openapi <file> --component <name>"
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
const issues = validateJson(schema, document, { root })
|
|
104
|
+
return report(name, issues, `${input.document} is valid against ${against}`)
|
|
105
|
+
})
|
|
106
|
+
})
|