Snow 1d0b9c5323 增加了node环境 | 1 mese fa | |
---|---|---|
.. | ||
lib | 1 mese fa | |
.npmignore | 1 mese fa | |
.travis.yml | 1 mese fa | |
CHANGELOG.md | 1 mese fa | |
Gruntfile.js | 1 mese fa | |
LICENSE | 1 mese fa | |
Readme.md | 1 mese fa | |
bower.json | 1 mese fa | |
index.js | 1 mese fa | |
karma.conf.js | 1 mese fa | |
package.json | 1 mese fa |
Generate JSON object from default values in JSON Schema
Works both in node and browser.
npm
npm install json-schema-defaults
bower
bower install json-schema-defaults
manual
Download lib/defaults.js
CommonJS (node.js)
var defaults = require('./path/to/index.js');
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({ ... });
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.
Run tests
npm test
Or individually
# in node
./node_modules/.bin/jasmine-node test/
# in browser
./node_modules/karma/bin/karma start
(c) 2015 Chute Corporation. Released under the terms of the MIT License.