-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace legacy mechanism for looking up key name from definitions
- Loading branch information
Showing
5 changed files
with
58,284 additions
and
85,329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,78 @@ | ||
import {isPlainObject} from 'lodash' | ||
import {DereferencedPaths} from './resolver' | ||
import {AnnotatedJSONSchema, JSONSchema, Parent, isAnnotated} from './types/JSONSchema' | ||
import {AnnotatedJSONSchema, JSONSchema, Parent, KeyNameFromDefinition, isAnnotated} from './types/JSONSchema' | ||
|
||
/** | ||
* Traverses over the schema, assigning to each | ||
* node metadata that will be used downstream. | ||
*/ | ||
export function annotate( | ||
schema: JSONSchema, | ||
dereferencedPaths: DereferencedPaths, | ||
parent: JSONSchema | null = null, | ||
): AnnotatedJSONSchema { | ||
if (!Array.isArray(schema) && !isPlainObject(schema)) { | ||
return schema as AnnotatedJSONSchema | ||
const annotators = new Map< | ||
string, | ||
(schema: JSONSchema, parent: JSONSchema | null, dereferencedPaths: DereferencedPaths) => void | ||
>() | ||
|
||
annotators.set('Add Parent to each node', (schema, parent) => { | ||
Object.defineProperty(schema, Parent, { | ||
enumerable: false, | ||
value: parent, | ||
writable: false, | ||
}) | ||
}) | ||
|
||
annotators.set('Add key name from the original definition, if there was one', (schema, _, dereferencedPaths) => { | ||
const ref = dereferencedPaths.get(schema) | ||
if (!ref) { | ||
return | ||
} | ||
|
||
// Handle cycles | ||
if (isAnnotated(schema)) { | ||
return schema | ||
const keyName = getDefinitionKeyNameFromRef(ref) | ||
if (!keyName) { | ||
return | ||
} | ||
|
||
// Add a reference to this schema's parent | ||
Object.defineProperty(schema, Parent, { | ||
Object.defineProperty(schema, KeyNameFromDefinition, { | ||
enumerable: false, | ||
value: parent, | ||
value: keyName, | ||
writable: false, | ||
}) | ||
}) | ||
|
||
// Arrays | ||
if (Array.isArray(schema)) { | ||
schema.forEach(child => annotate(child, dereferencedPaths, schema)) | ||
function getDefinitionKeyNameFromRef(ref: string): string | undefined { | ||
const parts = ref.split('/') | ||
if (parts[parts.length - 2] !== 'definitions') { | ||
return | ||
} | ||
return parts[parts.length - 1] | ||
} | ||
|
||
// Objects | ||
for (const key in schema) { | ||
annotate(schema[key], dereferencedPaths, schema) | ||
/** | ||
* Traverses over the schema, assigning to each | ||
* node metadata that will be used downstream. | ||
*/ | ||
export function annotate(schema: JSONSchema, dereferencedPaths: DereferencedPaths): AnnotatedJSONSchema { | ||
function go(s: JSONSchema, parent: JSONSchema | null): void { | ||
if (!Array.isArray(s) && !isPlainObject(s)) { | ||
return | ||
} | ||
|
||
// Handle cycles | ||
if (isAnnotated(s)) { | ||
return | ||
} | ||
|
||
// Run annotators | ||
annotators.forEach(f => { | ||
f(s, parent, dereferencedPaths) | ||
}) | ||
|
||
// Handle arrays | ||
if (Array.isArray(s)) { | ||
s.forEach(_ => go(_, s)) | ||
} | ||
|
||
// Handle objects | ||
for (const key in s) { | ||
go(s[key], s) | ||
} | ||
} | ||
|
||
go(schema, null) | ||
|
||
return schema as AnnotatedJSONSchema | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.