Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
json-schema-diff
Advanced tools
A language agnostic CLI tool and nodejs api to identify differences between two json schema files.
The json-schema-diff npm package is a tool for comparing JSON schemas. It helps identify differences between two JSON schemas, which can be useful for versioning, migration, and ensuring compatibility between different versions of APIs or data structures.
Compare JSON Schemas
This feature allows you to compare two JSON schemas and identify the differences between them. The code sample demonstrates how to use the json-schema-diff package to compare two schemas and print the differences.
const jsonSchemaDiff = require('json-schema-diff');
const schema1 = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name']
};
const schema2 = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
email: { type: 'string' }
},
required: ['name', 'email']
};
jsonSchemaDiff.diffSchemas({
sourceSchema: schema1,
destinationSchema: schema2
}).then((diff) => {
console.log(JSON.stringify(diff, null, 2));
}).catch((error) => {
console.error(error);
});
The json-diff package provides a way to compare two JSON objects and highlight the differences. While it is not specifically designed for JSON schemas, it can be used to compare any JSON data structures. It is more general-purpose compared to json-schema-diff, which is specialized for JSON schema comparison.
The deep-diff package allows for deep comparison of JavaScript objects, including JSON objects. It can be used to find differences between two objects at any depth. Similar to json-diff, it is not specialized for JSON schemas but can be used for a wide range of JSON data comparisons.
The diff package is a general-purpose text comparison tool that can be used to compare JSON strings. It provides a variety of diff algorithms and output formats. While it is not specifically designed for JSON schemas, it can be used to compare JSON data by converting it to strings.
A language agnostic CLI tool and nodejs api to identify differences between two json schema files.
Install the tool using npm and add it to the package.json
npm install json-schema-diff --save-dev
This tool identifies what has changed between two json schema files. These changes are classified into two groups, added and removed. Using an approach based on set theory this tool is able to calculate these differences to a high level of accuracy.
KEYWORDS.md contains the details of what json schema keywords are supported.
A change is considered an addition when the destination schema has become more permissive relative to the source schema. For example {"type": "string"}
-> {"type": ["string", "number"]}
.
A change is considered a removal when the destination schema has become more restrictive relative to the source schema. For example {"type": ["string", "number"]}
-> {"type": "string"}
.
The addition and removal changes detected are returned in JsonSchema format. These schemas represent the set of values that have been added or removed.
{
"properties": {
"id": {
"type": "number"
}
},
"type": "object"
}
{
"properties": {
"id": {
"type": ["string", "number"]
}
},
"type": "object"
}
All objects that contain an id property of type string. The id property is required because both source and destination schemas accept objects without an id property, so we want to exclude those objects from the added result.
{
"properties": {
"id": {
"type": "string"
}
},
"required": ["id"],
"type": "object"
}
All values accepted by the source schema are also accepted by the destination schema, so the removed result is a schema that accepts no values.
false
Invoke the tool with a file path to the source schema file and the destination schema file. These files should be in JSON format and be valid according to the json schema draft-07 specification.
The tool will return two json schemas as output, one representing the values that were added by the destination schema and the other representing the values that were removed by the destination schema.
The tool will fail if any removed differences are detected.
/path/to/source-schema.json
{
"type": "string"
}
/path/to/destination-schema.json
{
"type": ["string", "number"]
}
Invoking the tool
json-schema-diff /path/to/source-schema.json /path/to/destination-schema.json
Output
Non-breaking changes found between the two schemas.
Values described by the following schema were added:
{
"type": [
"number"
]
}
Values described by the following schema were removed:
false
Invoke the library with the source schema and the destination schema. These objects should be simple javascript objects and be valid according to the json schema draft-07 specification.
For full details of the nodejs api please refer to api-types.d.ts
const jsonSchemaDiff = require('json-schema-diff');
const source = {type: 'string'};
const destination = {type: ['string', 'number']};
const result = await jsonSchemaDiff.diffSchemas({
sourceSchema: source,
destinationSchema: destination
});
if (result.removalsFound) {
console.log('Something was removed!');
}
if (result.additionsFound) {
console.log('Something was added!');
}
During the process of parsing input schemas and/or diffing, some mathematical functions and heuristics might be applied to improve the performance of calculations. These do not affect the format or content of the results.
Some of these simplifications, while having a positive effect on memory consumption may require a higher CPU usage. If you come across memory consumption issues or process taking too slow to finish you may give it a try at turning these on/off, via environment variables
n×m
results given inputs of size n
and m
). Some of these
intermediate results can be discarded immediately if it can be proved to be subsets of previously calculated results
( A ∪ (A ∩ B) ⇔ A
).JSON_SCHEMA_DIFF_APPLY_ABSORPTION_IN_CARTESIAN_PRODUCT
true
See CHANGELOG.md
See CONTRIBUTING.md
See LICENSE.txt
FAQs
A language agnostic CLI tool and nodejs api to identify differences between two json schema files.
We found that json-schema-diff demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.