-
-
Notifications
You must be signed in to change notification settings - Fork 18
Refactor: Minor safety and modernization updates in processSchema #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Manas-Dikshit
wants to merge
8
commits into
webpack:main
Choose a base branch
from
Manas-Dikshit:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7a2eb9
Refactor processSchema for improved safety and readability
Manas-Dikshit de598eb
Add JSDoc comments for schema processing functions
Manas-Dikshit e91043f
Refactor JSDoc comments and type definitions
Manas-Dikshit 2f9b1f3
Refine type definitions for JSONSchema processing
Manas-Dikshit a8841e6
Update SchemaVisitor context type to ProcessContext
Manas-Dikshit d94d408
Refactor process-schema.js for clarity and structure
Manas-Dikshit 2fb2ff8
Refine JSONSchema and ProcessContext typedefs
Manas-Dikshit 89ddde0
Refactor JSON schema types and context handling
Manas-Dikshit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,36 +1,64 @@ | ||
| const NESTED_WITH_NAME = ["definitions", "properties"]; | ||
| /** | ||
| * @typedef {import("json-schema").JSONSchema7} JSONSchema | ||
| */ | ||
|
|
||
| const NESTED_DIRECT = ["items", "additionalProperties", "not"]; | ||
| /** | ||
| * @typedef {Object} SchemaVisitor | ||
| * @property {(schema: JSONSchema | boolean, context?: unknown) => JSONSchema | boolean | void} [schema] | ||
| * @property {(obj: JSONSchema | boolean, context?: unknown) => JSONSchema | boolean | void} [object] | ||
| * @property {(arr: (JSONSchema | boolean)[], context?: unknown) => void} [array] | ||
| */ | ||
|
|
||
| const NESTED_WITH_NAME = ["definitions", "properties"]; | ||
| const NESTED_DIRECT = ["items", "additionalProperties", "not"]; | ||
| const NESTED_ARRAY = ["oneOf", "anyOf", "allOf"]; | ||
|
|
||
| /** | ||
| * Recursively processes a JSON Schema using the visitor pattern. | ||
| * | ||
| * @param {SchemaVisitor} visitor | ||
| * @param {JSONSchema | boolean} json | ||
| * @param {unknown} [context] | ||
| * @returns {JSONSchema | boolean} | ||
| */ | ||
| const processSchema = (visitor, json, context) => { | ||
| if (!json || typeof json !== "object") return json; | ||
|
|
||
| json = { ...json }; | ||
| if (visitor.schema) json = visitor.schema(json, context); | ||
|
|
||
| if (typeof visitor?.schema === "function") { | ||
| json = visitor.schema(json, context) || json; | ||
| } | ||
|
|
||
| for (const name of NESTED_WITH_NAME) { | ||
| if (name in json && json[name] && typeof json[name] === "object") { | ||
| if (visitor.object) json[name] = visitor.object(json[name], context); | ||
| for (const key in json[name]) { | ||
| if (json[name] && typeof json[name] === "object" && !Array.isArray(json[name])) { | ||
| if (typeof visitor?.object === "function") { | ||
| json[name] = visitor.object(json[name], context) || json[name]; | ||
| } | ||
|
|
||
| for (const key of Object.keys(json[name])) { | ||
| json[name][key] = processSchema(visitor, json[name][key], context); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const name of NESTED_DIRECT) { | ||
| if (name in json && json[name] && typeof json[name] === "object") { | ||
| if (json[name] && typeof json[name] === "object" && !Array.isArray(json[name])) { | ||
| json[name] = processSchema(visitor, json[name], context); | ||
| } | ||
| } | ||
|
|
||
| for (const name of NESTED_ARRAY) { | ||
| if (name in json && Array.isArray(json[name])) { | ||
| json[name] = json[name].slice(); | ||
| for (let i = 0; i < json[name].length; i++) { | ||
| json[name][i] = processSchema(visitor, json[name][i], context); | ||
| if (Array.isArray(json[name])) { | ||
| json[name] = json[name].map((item) => processSchema(visitor, item, context)); | ||
|
|
||
| if (typeof visitor?.array === "function") { | ||
| visitor.array(json[name], context); | ||
| } | ||
| if (visitor.array) visitor.array(json[name], context); | ||
| } | ||
| } | ||
|
|
||
| return json; | ||
| }; | ||
|
|
||
| module.exports = processSchema; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.