json-born
v1.0.0
Published
Construct, scaffold, and validate JSON. Every JSON has a beginning.
Maintainers
Readme
json-born
Every JSON has a beginning.
Like Jason Bourne, your JSON didn't ask to exist. One moment there was nothing. Then — structure. Fields. Types. Validation rules. A schema. A purpose.
json-born brings your JSON into the world.
What it does
json-born is a toolkit for constructing, scaffolding, and validating JSON — building structured, schema-backed JSON from scratch or from raw/unknown input.
Features
- Schema generation — infer a JSON Schema from any sample object
- Scaffolding — generate an empty JSON object from a schema
- Type enforcement — build objects that are validated against a schema on construction
- Deep nesting — compose complex nested structures with a fluent builder API
- Defaults injection — fill in missing fields with defined default values
Install
npm install json-bornUsage
Infer a schema from an object
import { inferSchema } from 'json-born';
const schema = inferSchema({
name: "Jason",
age: 41,
aliases: ["John Michael Kane"]
});
// Returns a JSON Schema object describing the structureScaffold an empty object from a schema
import { scaffold } from 'json-born';
const empty = scaffold(schema);
// { name: "", age: 0, aliases: [] }Builder API
import { define } from 'json-born';
const schema = define()
.field('name', 'string', { default: '' })
.field('age', 'number', { required: true })
.field('aliases', 'array', { items: 'string' })
.build();Validate on construction
import { create } from 'json-born';
const person = create(schema, {
name: "David Webb",
age: 41,
aliases: ["Jason Bourne", "John Michael Kane"]
});
// Throws if input doesn't match schemaAPI
| Function | Description |
|----------|-------------|
| inferSchema(obj) | Infer a JSON Schema from a sample object |
| scaffold(schema) | Generate an empty object matching a schema |
| define() | Fluent builder for constructing a schema |
| create(schema, data) | Validate and construct an object from a schema |
See also
json-deruler — the opposite of this package. Strips schemas, flattens structure, removes all the rules. Because sometimes things fall apart.
License
MIT
