Giter VIP home page Giter VIP logo

json-schema-defaults's Introduction

JSON Schema Defaults Build Status

Generate JSON object from default values in JSON Schema

Works both in node and browser.

Installation

  • npm

    npm install json-schema-defaults
  • bower

    bower install json-schema-defaults
  • manual

    Download lib/defaults.js

Usage

  • CommonJS (node.js)

    var defaults = require('json-schema-defaults');
    defaults({ ... });
  • RequireJS

    // in require.js config
    paths: {
      'defaults': './path/to/lib/defaults.js'
    }
    
    // in a file
    define(['defaults'], function(defaults) {
      defaults({ ... });
    });
  • standalone

    window.jsonSchemaDefaults({ ... });

    If the standalone version causes any conflict with existing jsonSchemaDefaults global variable, you can return back the original variable:

    var defaults = window.jsonSchemaDefaults.noConflict();
    // `window.jsonSchemaDefaults` now points to the original variable
    // `defaults` points to this script
    defaults({ ... });
  • CLI

    If installed globally:

    json-schema-defaults schema.json

    If locally:

    ./node_modules/.bin/json-schema-defaults schema.json

    Custom indentation (defaults to 2):

    json-schema-defaults --indent 4 schema.json

    Write into a file:

    json-schema-defaults schema.json > defaults.json

Documentation

Call defaults with JSON Schema. The default values will be extracted as a JSON.

var json = defaults({
  "title": "Album Options",
  "type": "object",
  "properties": {
    "sort": {
      "type": "string",
      "default": "id"
    },
    "per_page": {
      "default": 30,
      "type": "integer"
    }
  }
});

// would return
{
  sort: 'id',
  per_page: 30
}

For more examples, see the tests.

Development

Run tests

npm test

Or individually

# in node
./node_modules/.bin/jasmine-node test/

# in browser
./node_modules/karma/bin/karma start

Contributors

  • Eugene Tsypkin @jhony-chikens

License

(c) 2015 Chute Corporation. Released under the terms of the MIT License.

json-schema-defaults's People

Contributors

hakonsol avatar itsypkin avatar mohd-akram avatar petrbela avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.