Open Sourced json-normalizer
Hi there, I've open-sourced my new library, json-normalizer, a JavaScript library that normalize a json object to meets a json-schema using extended schema descriptor. Please give it a try. Comments and issue reports are welcome. Thank you!
json-normalizer
Normalize a json object to meets a json-schema using extended schema descriptor.
Note: Json-normalizer is not a json-schema validator. If you are looking for json-schema validators,
please check out here: json-schema validators.
Contents
- Overview
- Install
- API
- Other functions that may be handy
- Usage Examples
- Build and Contribute
- Issues
- Test
- Change Logs
- License
Overview
The Problems
You want:
- Alias, e.g., both "entry" and "entries" are valid property name, but you don't want to check both of them,
- Support either single item or an array of items, but you don't want to handle both of them,
- Flat data objects, i.e., user don't have to nest properties, and
- Maximum reuse of schemas, i.e., you want multiple inheritance.
The Solution
The normalizer is based on json-schema with the "normalizable" extension:
- Properties can have alias and being normalized, using the "
alias
" keyword. - For schemas of "
object
" type, a "primary
" property can be specified,
that when provided value is not an object (a primitive or an array), can be assigned to. - For schemas of "
object
" type, a "gathering
" property can be specified, that when "additionalProperties
" is set to false, all unrecognized additional properties can be gathered to. The "gathering
" property name defaults to "others
". - For schemas of "
array
" type, if the provided value is not an array, converts that value to an array automatically. - Allow schema extends other schemas, using the "
extends
" keyword.
Install
1 | $ npm install --save json-normalizer |
API
normalize(schema, data, [options,] callback)
Normalizes a loose JSON data object to a strict json-schema data object.
Context
Don't care.
Parameters
schema
The schema used to normalize the given JSON data object.
data
The JSON data object.
options
Optional. Currently only accepts a loader or an array of loaders.
options.loader
| options.loaders
A loader or an array of loaders that help loading remote schemas. Loaders are tested in the order listed.
callback
The callback function with function (err, detail)
signature that the normalizer delivers the normalized JSON object to. Called with null context.
Returns
No return value.
Example
1 | var normalize = require('json-normalizer'); |
normalize.sync(schema, data [, options])
Same as the async version but returns normalized schema. Throws when error;
Note that loaders must also be sync version.
Returns
The normalized schema.
Example
1 | var normalize = require('json-normalizer'); |
Other functions that may be handy:
deref(schema, [options,] callback)
Dereferences JSON references in a schema. Default implementation supports only local references, i.e. references that starts with "#
". Custom loaders can be provided via the options
object.
Context
Don't care.
Parameters
schema
The schema that contains JSON references.
options
Optional. Currently only accepts a loader or an array of loaders.
options.loader
| options.loaders
A loader or an array of loaders that help loading remote schemas. Loaders are tested in the order listed.
callback
The callback function with function (err, detail)
signature that the deref
function delivers the deferenced schema to. Called with null context.
Returns
No return value.
deref.sync(schema [, options])
Same as the async version but returns dereferenced schema. Throws when error;
Note that loaders must also be sync version.
maperFactory([definitions]): function
Creates a mapper schema loader, i.e. a schema loader that holds schema definitions in memory, initialized with the optional definitions
hash object.
Context
Don't care.
Parameters
definitions
Optional. If provided, all definitions in the definitions
hash object will be available for mapping. See mapper#map(definitions)
for more details.
Returns
The mapper schema loader.
mapper(root, $ref, callback)
The async version of mapper.
mapper#sync(root, $ref)
The sync version of mapper.
mapper#map($ref, schema)
Add a schema
with the $ref
JSON pointer. Subsequent calls with the same JSON pointer will override definition of preceding calls.
Context
Don't care.
Parameters
$ref
The JSON pointer the schema being published.
schema
The schema.
Returns
No return value.
mapper#map(definitions)
Add all $ref
: schema
pairs defined in the definitions
hash object. Subsequent calls with the same JSON pointer will override definition of preceding calls.
Context
Don't care.
Parameters
definitions
The definitions hash object. Keys in the hash object should be valid JSON pointers; Values in the hash object should be the schema of the corresponding JSON pointer.
Returns
No return value.
Example
1 | var mapperFactory = require('json-normalizer/src/loader/mapper'); |
Write Your Own Loader
- Async loaders are functions with
function (rootSchema, $ref, callback(err, schema))
signature. - Async loaders must return truthy if it can handle the given reference
$ref
, and the callback be called later when the schema is ready. However, if error occurred, e.g., network failure, the callback be called with theerr
set to the type of error and the second argument provides details of that error. - Async loaders must return falsy if it can not handle the given reference
$ref
, and thecallback
should not be called at all. - Sync loaders are functions with
function (rootSchema, $ref)
signature. - A local loader is always tested first, and other loaders provided in
options.loader
oroptions.loaders
argument are then tested by the order listed.
Usage Examples
Alias and Array Type
With the schema:
1 | { |
will normalize this data object:
1 | { |
to:
1 | { |
Primary Property and Gathering Property
With the schema:
1 | { |
will normalize this data object:
1 | "src/index.js" |
to:
1 | { |
and this data object:
1 | { |
to:
1 | { |
Extending Schema
1 | { |
Please refer to test for more examples.
Build and Contribute
1 | $ git clone https://github.com/amobiz/json-normalizer.git |
Issues
Test
Tests are written in mocha. Run tests in terminal:
1 | $ npm test |
Change Logs
- 2015/09/15 - 0.1.1
- Add sync version.
- Remove mapper#add() method, use mapper#map() instead.
License
MIT