
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
json-schema-walker
Advanced tools
A system that visits all schema objects in a JSON Schema document and makes callbacks before visiting all of the current schema object's subschemas.
The json-schema-walker package is a utility for traversing JSON schemas. It allows developers to walk through JSON schema objects and perform operations on each node, which is useful for tasks such as validation, transformation, or analysis of JSON schemas.
Schema Traversal
This feature allows you to traverse a JSON schema and perform operations on each node. In the code sample, the walker function is used to iterate over each node in the schema, printing the pointer (path) to each node.
const walker = require('json-schema-walker');
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' }
}
};
walker(schema, {
all: (subSchema, pointer) => {
console.log('Visiting:', pointer);
}
});
Custom Node Processing
This feature allows you to apply custom logic to nodes based on their properties. In the code sample, the walker function is used to identify nodes of type 'string' and print their path.
const walker = require('json-schema-walker');
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' }
}
};
walker(schema, {
all: (subSchema, pointer) => {
if (subSchema.type === 'string') {
console.log('String type found at:', pointer);
}
}
});
json-schema-traverse is a similar package that provides functionality to traverse JSON schemas. It offers a more flexible API for visiting nodes and allows for pre- and post-order traversal. Compared to json-schema-walker, json-schema-traverse might offer more control over the traversal process.
Ajv is a JSON schema validator that also includes functionality for traversing schemas. While its primary purpose is validation, it can be used to walk through schemas as part of the validation process. Ajv is more feature-rich and includes validation, compilation, and transformation capabilities, making it a more comprehensive tool compared to json-schema-walker.
Loosely based on CloudFlare's json schema tools
A system that visits all schema objects in a JSON Schema document and makes callbacks before and/or after visiting all of the current schema object's subschemas.
import { Walker } from "json-schema-walker";
const schema = {
// your json schema
};
const walker = new Walker<T>();
await walker.loadSchema(schema, {
cloneSchema: true,
dereference: false,
dereferenceOptions: {
dereference: {
circular: "ignore",
},
},
});
const convertSchema = (schema) => {
// do something with the schema properties
};
await walker.walk(convertSchema, walker.vocabularies.DRAFT_07);
const updatedSchema = walker.rootSchema;
Passing the options
{
"dereferenceOptions": {
"dereference": {
"circular": "ignore"
}
}
}
will dereference all non-circular references in your schema.
FAQs
A system that visits all schema objects in a JSON Schema document and makes callbacks before visiting all of the current schema object's subschemas.
The npm package json-schema-walker receives a total of 164,935 weekly downloads. As such, json-schema-walker popularity was classified as popular.
We found that json-schema-walker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.