1 | import type {AnySchema, AnySchemaObject, UriResolver} from "../types"
|
---|
2 | import type Ajv from "../ajv"
|
---|
3 | import type {URIComponent} from "fast-uri"
|
---|
4 | import {eachItem} from "./util"
|
---|
5 | import * as equal from "fast-deep-equal"
|
---|
6 | import * as traverse from "json-schema-traverse"
|
---|
7 |
|
---|
8 | // the hash of local references inside the schema (created by getSchemaRefs), used for inline resolution
|
---|
9 | export type LocalRefs = {[Ref in string]?: AnySchemaObject}
|
---|
10 |
|
---|
11 | // TODO refactor to use keyword definitions
|
---|
12 | const SIMPLE_INLINED = new Set([
|
---|
13 | "type",
|
---|
14 | "format",
|
---|
15 | "pattern",
|
---|
16 | "maxLength",
|
---|
17 | "minLength",
|
---|
18 | "maxProperties",
|
---|
19 | "minProperties",
|
---|
20 | "maxItems",
|
---|
21 | "minItems",
|
---|
22 | "maximum",
|
---|
23 | "minimum",
|
---|
24 | "uniqueItems",
|
---|
25 | "multipleOf",
|
---|
26 | "required",
|
---|
27 | "enum",
|
---|
28 | "const",
|
---|
29 | ])
|
---|
30 |
|
---|
31 | export function inlineRef(schema: AnySchema, limit: boolean | number = true): boolean {
|
---|
32 | if (typeof schema == "boolean") return true
|
---|
33 | if (limit === true) return !hasRef(schema)
|
---|
34 | if (!limit) return false
|
---|
35 | return countKeys(schema) <= limit
|
---|
36 | }
|
---|
37 |
|
---|
38 | const REF_KEYWORDS = new Set([
|
---|
39 | "$ref",
|
---|
40 | "$recursiveRef",
|
---|
41 | "$recursiveAnchor",
|
---|
42 | "$dynamicRef",
|
---|
43 | "$dynamicAnchor",
|
---|
44 | ])
|
---|
45 |
|
---|
46 | function hasRef(schema: AnySchemaObject): boolean {
|
---|
47 | for (const key in schema) {
|
---|
48 | if (REF_KEYWORDS.has(key)) return true
|
---|
49 | const sch = schema[key]
|
---|
50 | if (Array.isArray(sch) && sch.some(hasRef)) return true
|
---|
51 | if (typeof sch == "object" && hasRef(sch)) return true
|
---|
52 | }
|
---|
53 | return false
|
---|
54 | }
|
---|
55 |
|
---|
56 | function countKeys(schema: AnySchemaObject): number {
|
---|
57 | let count = 0
|
---|
58 | for (const key in schema) {
|
---|
59 | if (key === "$ref") return Infinity
|
---|
60 | count++
|
---|
61 | if (SIMPLE_INLINED.has(key)) continue
|
---|
62 | if (typeof schema[key] == "object") {
|
---|
63 | eachItem(schema[key], (sch) => (count += countKeys(sch)))
|
---|
64 | }
|
---|
65 | if (count === Infinity) return Infinity
|
---|
66 | }
|
---|
67 | return count
|
---|
68 | }
|
---|
69 |
|
---|
70 | export function getFullPath(resolver: UriResolver, id = "", normalize?: boolean): string {
|
---|
71 | if (normalize !== false) id = normalizeId(id)
|
---|
72 | const p = resolver.parse(id)
|
---|
73 | return _getFullPath(resolver, p)
|
---|
74 | }
|
---|
75 |
|
---|
76 | export function _getFullPath(resolver: UriResolver, p: URIComponent): string {
|
---|
77 | const serialized = resolver.serialize(p)
|
---|
78 | return serialized.split("#")[0] + "#"
|
---|
79 | }
|
---|
80 |
|
---|
81 | const TRAILING_SLASH_HASH = /#\/?$/
|
---|
82 | export function normalizeId(id: string | undefined): string {
|
---|
83 | return id ? id.replace(TRAILING_SLASH_HASH, "") : ""
|
---|
84 | }
|
---|
85 |
|
---|
86 | export function resolveUrl(resolver: UriResolver, baseId: string, id: string): string {
|
---|
87 | id = normalizeId(id)
|
---|
88 | return resolver.resolve(baseId, id)
|
---|
89 | }
|
---|
90 |
|
---|
91 | const ANCHOR = /^[a-z_][-a-z0-9._]*$/i
|
---|
92 |
|
---|
93 | export function getSchemaRefs(this: Ajv, schema: AnySchema, baseId: string): LocalRefs {
|
---|
94 | if (typeof schema == "boolean") return {}
|
---|
95 | const {schemaId, uriResolver} = this.opts
|
---|
96 | const schId = normalizeId(schema[schemaId] || baseId)
|
---|
97 | const baseIds: {[JsonPtr in string]?: string} = {"": schId}
|
---|
98 | const pathPrefix = getFullPath(uriResolver, schId, false)
|
---|
99 | const localRefs: LocalRefs = {}
|
---|
100 | const schemaRefs: Set<string> = new Set()
|
---|
101 |
|
---|
102 | traverse(schema, {allKeys: true}, (sch, jsonPtr, _, parentJsonPtr) => {
|
---|
103 | if (parentJsonPtr === undefined) return
|
---|
104 | const fullPath = pathPrefix + jsonPtr
|
---|
105 | let innerBaseId = baseIds[parentJsonPtr]
|
---|
106 | if (typeof sch[schemaId] == "string") innerBaseId = addRef.call(this, sch[schemaId])
|
---|
107 | addAnchor.call(this, sch.$anchor)
|
---|
108 | addAnchor.call(this, sch.$dynamicAnchor)
|
---|
109 | baseIds[jsonPtr] = innerBaseId
|
---|
110 |
|
---|
111 | function addRef(this: Ajv, ref: string): string {
|
---|
112 | // eslint-disable-next-line @typescript-eslint/unbound-method
|
---|
113 | const _resolve = this.opts.uriResolver.resolve
|
---|
114 | ref = normalizeId(innerBaseId ? _resolve(innerBaseId, ref) : ref)
|
---|
115 | if (schemaRefs.has(ref)) throw ambiguos(ref)
|
---|
116 | schemaRefs.add(ref)
|
---|
117 | let schOrRef = this.refs[ref]
|
---|
118 | if (typeof schOrRef == "string") schOrRef = this.refs[schOrRef]
|
---|
119 | if (typeof schOrRef == "object") {
|
---|
120 | checkAmbiguosRef(sch, schOrRef.schema, ref)
|
---|
121 | } else if (ref !== normalizeId(fullPath)) {
|
---|
122 | if (ref[0] === "#") {
|
---|
123 | checkAmbiguosRef(sch, localRefs[ref], ref)
|
---|
124 | localRefs[ref] = sch
|
---|
125 | } else {
|
---|
126 | this.refs[ref] = fullPath
|
---|
127 | }
|
---|
128 | }
|
---|
129 | return ref
|
---|
130 | }
|
---|
131 |
|
---|
132 | function addAnchor(this: Ajv, anchor: unknown): void {
|
---|
133 | if (typeof anchor == "string") {
|
---|
134 | if (!ANCHOR.test(anchor)) throw new Error(`invalid anchor "${anchor}"`)
|
---|
135 | addRef.call(this, `#${anchor}`)
|
---|
136 | }
|
---|
137 | }
|
---|
138 | })
|
---|
139 |
|
---|
140 | return localRefs
|
---|
141 |
|
---|
142 | function checkAmbiguosRef(sch1: AnySchema, sch2: AnySchema | undefined, ref: string): void {
|
---|
143 | if (sch2 !== undefined && !equal(sch1, sch2)) throw ambiguos(ref)
|
---|
144 | }
|
---|
145 |
|
---|
146 | function ambiguos(ref: string): Error {
|
---|
147 | return new Error(`reference "${ref}" resolves to more than one schema`)
|
---|
148 | }
|
---|
149 | }
|
---|