Home

Awesome

map-schema Donate NPM version NPM monthly downloads NPM total downloads Build Status

Normalize an object by running normalizers and validators that are mapped to a schema.

You might also be interested in normalize-pkg.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Table of Contents

<details> <summary><strong>Details</strong></summary> </details>

Install

Install with npm (requires Node.js >=10):

$ npm install --save map-schema

Usage

var schema = require('map-schema');

Example

This is a basic example schema for normalizing and validating fields on package.json (a full version of this will be available on normalize-pkg when complete):

var fs = require('fs');
var isObject = require('isobject');
var Schema = require('map-schema');

// create a schema
var schema = new Schema()
  .field('name', 'string')
  .field('description', 'string')
  .field('repository', ['object', 'string'], {
    normalize: function(val) {
      return isObject(val) ? val.url : val;
    }
  })
  .field('main', 'string', {
    validate: function(filepath) {
      return fs.existsSync(filepath);
    }
  })
  .field('version', 'string', {
    default: '0.1.0'
  })
  .field('license', 'string', {
    default: 'MIT'
  })

var pkg = require('./package');
// normalize an object
console.log(schema.normalize(pkg));
// validation errors array
console.log(schema.errors);

Errors

Validation errors are exposed on schema.errors. Error reporting is pretty basic right now but I plan to implement something better soon.

API

Params

Example

var schema = new Schema()
  .field('name', 'string')
  .field('version', 'string')
  .field('license', 'string')
  .field('licenses', 'array', {
    normalize: function(val, key, config) {
       // convert license array to `license` string
       config.license = val[0].type;
       delete config[key];
    }
  })
  .normalize(require('./package'))

Set key on the instance with the given value.

Params

Push a warning onto the schema.warnings array. Placeholder for better message handling and a reporter (planned).

Params

Params

Example

var semver = require('semver');

schema
  .field('keywords', 'array')
  .field('version', 'string', {
    validate: function(val, key, config, schema) {
      return semver.valid(val) !== null;
    }
  })

Params

Example

schema.field('bugs', ['object', 'string']);
var field = schema.get('bugs', 'types');
//=> ['object', 'string']

Omit a property from the returned object. This method can be used in normalize functions as a way of removing undesired properties.

Params

Update a property on the returned object. This method will trigger validation and normalization of the updated property.

Params

Returns true if field name is an optional field.

Params

Returns true if field name was defined as a required field.

Params

Checks the config object for missing fields and. If found, a warning message is pushed onto the schema.warnings array, which can be used for reporting.

Params

Params

Example

schema.sortObject({z: '', a: ''}, ['a', 'z']);
//=> {a: '', z: ''}

When options.sortArrays is not false, sorts all arrays in the given config object using JavaScript's native .localeCompare method.

Params

Returns true if the given value is valid for field key.

Params

Normalize the given config object.

Params

Normalize a field on the schema.

Params

Visit method over the given object or array.

Params

Field

Create a new Field of the given type to validate against, and optional config object.

Params

Example

const field = new Field('string', {
  normalize: function(val) {
    // do stuff to `val`
    return val;
  }
});

.isValidType

Returns true if the given type is a valid type.

Params

.validate

Called in schema.validate, returns true if the given value is valid. This default validate method returns true unless overridden with a custom validate method.

Example

var field = new Field({
  types: ['string']
});

field.validate('name', {});
//=> false

.normalize

Normalize the field's value.

Example

var field = new Field({
  types: ['string'],
  normalize: function(val, key, config, schema) {
    // do stuff to `val`
    return val;
  }
});

About

<details> <summary><strong>Contributing</strong></summary>

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

</details> <details> <summary><strong>Running Tests</strong></summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
</details> <details> <summary><strong>Building docs</strong></summary>

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb
</details>

Related projects

You might also be interested in these projects:

Author

Jon Schlinkert

License

Copyright © 2020, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on March 01, 2020.