JSON Schema Test Data Generator
The `json-schema-test-data-generator` utility is designed to create sample test data objects based on a provided JSON schema. It generates an array of test scenarios, each indicating whether the generated data is `valid` against the schema, the `data` itself, a descriptive `message`, and an optional `property` key. As of version 0.1.1, the package is considered abandoned, with its last commit occurring 8 years ago. It focuses on covering various combinations for testing against a schema, including required and optional properties, and basic type mismatches. Its primary differentiator is generating both valid and intentionally invalid data scenarios with descriptive messages, helping users quickly create a test suite without manual data construction. Due to its abandoned status, users should be aware that it will not receive bug fixes, security updates, or new features, and might not support newer JSON Schema drafts or complex constructs.
Common errors
-
TypeError: generate is not a function
cause Attempting to use `import generate from 'json-schema-test-data-generator';` in a modern ESM environment without proper CommonJS interoperability.fixUse the CommonJS `require` syntax: `const generate = require('json-schema-test-data-generator');` -
Generated data does not match schema expectations for complex types (e.g., allOf, anyOf)
cause The generator has limited support for complex JSON Schema keywords and might not correctly interpret or generate data for all possible combinations.fixThis package is noted to cover 'various (but not all)' combinations. For schemas using advanced keywords like `allOf`, `anyOf`, `oneOf`, or `not`, manual inspection of generated tests is required. If the coverage is insufficient, consider a different, more actively maintained library that explicitly lists support for these keywords, such as `json-schema-faker`.
Warnings
- breaking The project is abandoned, with the last code changes dating back 8 years. This means there will be no further updates, bug fixes, or security patches, which could lead to unaddressed vulnerabilities or compatibility issues with newer Node.js versions or JSON Schema drafts.
- gotcha The generator does not support `$ref` keywords in JSON schemas. Schemas must be 'fully expanded' before being passed to the `generate` function, meaning all references must be resolved beforehand.
- gotcha The utility generates 'various (but not all) combinations' of test data. This means complex JSON Schema constructs (e.g., `oneOf`, `anyOf`, `not`, advanced array/object constraints, specific string patterns) might not be fully covered, potentially leading to incomplete test suites.
Install
-
npm install json-schema-test-data-generator -
yarn add json-schema-test-data-generator -
pnpm add json-schema-test-data-generator
Imports
- generate
import generate from 'json-schema-test-data-generator';
const generate = require('json-schema-test-data-generator');
Quickstart
const generate = require('json-schema-test-data-generator');
const schema = {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 5
},
"active": {
"type": "boolean"
},
"email": {
"type": "string",
"format": "email"
},
"accountNumber": {
"type": "number"
}
},
"required": ["name", "email"]
}
console.dir(generate(schema));