-
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.
[1/x] generalize linker to annotator
- Loading branch information
Showing
14 changed files
with
150 additions
and
145 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {isPlainObject} from 'lodash' | ||
import {DereferencedPaths} from './resolver' | ||
import {AnnotatedJSONSchema, JSONSchema, Parent, 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 | ||
} | ||
|
||
// Handle cycles | ||
if (isAnnotated(schema)) { | ||
return schema | ||
} | ||
|
||
// Add a reference to this schema's parent | ||
Object.defineProperty(schema, Parent, { | ||
enumerable: false, | ||
value: parent, | ||
writable: false, | ||
}) | ||
|
||
// Arrays | ||
if (Array.isArray(schema)) { | ||
schema.forEach(child => annotate(child, dereferencedPaths, schema)) | ||
} | ||
|
||
// Objects | ||
for (const key in schema) { | ||
annotate(schema[key], dereferencedPaths, schema) | ||
} | ||
|
||
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.