A simple tool for instantiating JSON Schemas.
JavaScript
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist/scripts
src
tests
.gitignore
LICENSE.md
README.md
bower.json
gulpfile.js
index.js
package-lock.json
package.json

README.md

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

var instantiator = require('json-schema-instantiator');

...

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

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

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

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

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:

angular.module('myApp', ['schemaInstantiator'])

...

Inject the InstantiatorService and use it:

...

.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.