# json-schema-instantiator

> A simple instantiator for json schemas.

Latest version **1.0.1** (published 2024-12-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-schema-instantiator
pnpm add json-schema-instantiator
yarn add json-schema-instantiator
bun add json-schema-instantiator
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2024-12-27 |
| First published | 2015-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 43 |
| Maintainers | tomarad |
| Keywords | JSON, schema, instantiate, instantiator, JSON-Schema, Generator, Generate, Data Generation |

## Links

- npm: https://www.npmjs.com/package/json-schema-instantiator
- Repository: https://github.com/tomarad/JSON-Schema-Instantiator
- Homepage: https://github.com/tomarad/JSON-Schema-Instantiator#readme
- Issues: https://github.com/tomarad/JSON-Schema-Instantiator/issues
- npm.io page: https://npm.io/package/json-schema-instantiator

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.1 (latest) — 2024-12-27
- 1.0.0 — 2023-07-25
- 0.6.0 — 2023-03-28
- 0.5.0 — 2021-06-18
- 0.4.8 — 2018-10-11
- 0.4.7 — 2018-06-28
- 0.4.6 — 2018-04-21
- 0.4.5 — 2018-03-24
- 0.4.4 — 2017-07-17
- 0.4.2 — 2016-09-27
- 0.4.0 — 2016-05-28
- 0.3.0 — 2015-10-31
- 0.2.2 — 2015-08-02
- 0.2.1 — 2015-04-12
- 0.2.0 — 2015-04-12
- … 1 more at https://npm.io/package/json-schema-instantiator/versions

## README

# JSON-Schema-Instantiator
A simple tool for instantiating JSON Schemas, with Angular support!

## Installing

### Node.js

```
npm install json-schema-instantiator
```

### AngularJS

```
bower install angular-schema-instantiator
```

## Using

### Node.js
``` javascript
var instantiator = require('json-schema-instantiator');

...

var schema = {
    "type": "object",
    "properties": {
        "title": {
            "type": "string",
            "default": "Example"
        },
        "description": {
            "type": "string"
        },
        "quantity": {
            "type": "number"
        },
        "endDate": {
            "type": "string",
            "format": "date"
        }
    },
    "required": ["title"]
};

instance = instantiator.instantiate(schema);
// instance === { title: "Example", description: "", quantity: 0, endDate: "" }

instance = instantiator.instantiate(schema, {requiredPropertiesOnly: false});
// instance === { title: "Example", description: "", quantity: 0, endDate: "" }

instance = instantiator.instantiate(schema, {requiredPropertiesOnly: true});
// instance === { title: "Example" }

// Override default values for a given type with a static value
instance = instantiator.instantiate(schema, { defaults: { number: 42 } });
// instance === { title: "Example", description: "", quantity: 42, endDate: "" }

// Override default values for a given type function that returns a value
instance = instantiator.instantiate(schema, {
  defaults: {
    // Function that receives current property/val and returns a value
    string: function (val) {
      var format = val.format;

      if (format && format === "date") {
        return new Date(2021, 0, 1);
      }

      return "";
    },
  },
});
// instance === { title: "Example", description: "", quantity: 0, endDate: new Date(2021, 0, 1) }
```

### AngularJS
Add the sources to index.html:

    <script src="json-schema-instanciator/src/instantiator.js"></script>
    <script src="json-schema-instanciator/src/angular-instantiator.js"></script>

Include the module:
``` javascript
angular.module('myApp', ['schemaInstantiator'])

...
```

Inject the InstantiatorService and use it:
``` javascript
...

.controller({
    MyCtrl: ['InstantiatorService', function(Instantiator) {
        var schema = {
            type: "string",
            default: "Hello!"
        };
        
        console.log(Instantiator.instantiate(schema));
        // Hello!
    }]
})

...
```

### Note
This does not replace schema validation! Invalid schemas will yield unexpected results.

---
_Source: https://npm.io/package/json-schema-instantiator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
