|
1 | 1 | import {isPlainObject} from 'lodash'
|
2 | 2 | import {DereferencedPaths} from './resolver'
|
3 |
| -import {AnnotatedJSONSchema, JSONSchema, Parent, isAnnotated} from './types/JSONSchema' |
| 3 | +import {AnnotatedJSONSchema, JSONSchema, Parent, KeyNameFromDefinition, isAnnotated} from './types/JSONSchema' |
4 | 4 |
|
5 |
| -/** |
6 |
| - * Traverses over the schema, assigning to each |
7 |
| - * node metadata that will be used downstream. |
8 |
| - */ |
9 |
| -export function annotate( |
10 |
| - schema: JSONSchema, |
11 |
| - dereferencedPaths: DereferencedPaths, |
12 |
| - parent: JSONSchema | null = null, |
13 |
| -): AnnotatedJSONSchema { |
14 |
| - if (!Array.isArray(schema) && !isPlainObject(schema)) { |
15 |
| - return schema as AnnotatedJSONSchema |
| 5 | +const annotators = new Set< |
| 6 | + (schema: JSONSchema, parent: JSONSchema | null, dereferencedPaths: DereferencedPaths) => void |
| 7 | +>() |
| 8 | + |
| 9 | +// note: this must run first, since it is used by the next annotator |
| 10 | +annotators.add(function annotateParent(schema, parent) { |
| 11 | + Object.defineProperty(schema, Parent, { |
| 12 | + enumerable: false, |
| 13 | + value: parent, |
| 14 | + writable: false, |
| 15 | + }) |
| 16 | +}) |
| 17 | + |
| 18 | +annotators.add(function annotateKeyNameFromDefinition(schema) { |
| 19 | + const keyNameFromSchema = getDefinitionKeyNameFromParent(schema as AnnotatedJSONSchema) // todo |
| 20 | + if (!keyNameFromSchema) { |
| 21 | + return |
16 | 22 | }
|
17 | 23 |
|
18 |
| - // Handle cycles |
19 |
| - if (isAnnotated(schema)) { |
20 |
| - return schema |
| 24 | + Object.defineProperty(schema, KeyNameFromDefinition, { |
| 25 | + enumerable: false, |
| 26 | + value: keyNameFromSchema, |
| 27 | + writable: false, |
| 28 | + }) |
| 29 | +}) |
| 30 | + |
| 31 | +annotators.add(function annotateKeyNameFrom$Ref(schema, _, dereferencedPaths) { |
| 32 | + if (schema.hasOwnProperty(KeyNameFromDefinition)) { |
| 33 | + return |
21 | 34 | }
|
22 | 35 |
|
23 |
| - // Add a reference to this schema's parent |
24 |
| - Object.defineProperty(schema, Parent, { |
| 36 | + const ref = dereferencedPaths.get(schema) |
| 37 | + if (!ref) { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + const keyNameFromRef = getDefinitionKeyNameFromRef(ref) |
| 42 | + if (!keyNameFromRef) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + Object.defineProperty(schema, KeyNameFromDefinition, { |
25 | 47 | enumerable: false,
|
26 |
| - value: parent, |
| 48 | + value: keyNameFromRef, |
27 | 49 | writable: false,
|
28 | 50 | })
|
| 51 | +}) |
29 | 52 |
|
30 |
| - // Arrays |
31 |
| - if (Array.isArray(schema)) { |
32 |
| - schema.forEach(child => annotate(child, dereferencedPaths, schema)) |
| 53 | +/** |
| 54 | + * If this schema came from a `definitions` block, returns the key name for the definition. |
| 55 | + */ |
| 56 | +function getDefinitionKeyNameFromParent(schema: AnnotatedJSONSchema): string | undefined { |
| 57 | + const parent = schema[Parent] |
| 58 | + if (!parent) { |
| 59 | + return |
| 60 | + } |
| 61 | + const grandparent = parent[Parent] |
| 62 | + if (!grandparent) { |
| 63 | + return |
33 | 64 | }
|
| 65 | + if (Object.keys(grandparent).find(_ => grandparent[_] === parent) !== 'definitions') { |
| 66 | + return |
| 67 | + } |
| 68 | + return Object.keys(parent).find(_ => parent[_] === schema) |
| 69 | +} |
34 | 70 |
|
35 |
| - // Objects |
36 |
| - for (const key in schema) { |
37 |
| - annotate(schema[key], dereferencedPaths, schema) |
| 71 | +function getDefinitionKeyNameFromRef(ref: string): string | undefined { |
| 72 | + const parts = ref.split('/') |
| 73 | + if (parts[parts.length - 2] !== 'definitions') { |
| 74 | + return |
38 | 75 | }
|
| 76 | + return parts[parts.length - 1] |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Traverses over the schema, assigning to each |
| 81 | + * node metadata that will be used downstream. |
| 82 | + */ |
| 83 | +export function annotate(schema: JSONSchema, dereferencedPaths: DereferencedPaths): AnnotatedJSONSchema { |
| 84 | + function go(s: JSONSchema, parent: JSONSchema | null): void { |
| 85 | + if (!Array.isArray(s) && !isPlainObject(s)) { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + // Handle cycles |
| 90 | + if (isAnnotated(s)) { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + // Run annotators |
| 95 | + annotators.forEach(f => { |
| 96 | + f(s, parent, dereferencedPaths) |
| 97 | + }) |
| 98 | + |
| 99 | + // Handle arrays |
| 100 | + if (Array.isArray(s)) { |
| 101 | + s.forEach(_ => go(_, s)) |
| 102 | + } |
| 103 | + |
| 104 | + // Handle objects |
| 105 | + for (const key in s) { |
| 106 | + go(s[key], s) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + go(schema, null) |
39 | 111 |
|
40 | 112 | return schema as AnnotatedJSONSchema
|
41 | 113 | }
|
0 commit comments