JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 56414
  • Score
    100M100P100Q146950F
  • License MIT

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

Package Exports

  • map-schema

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (map-schema) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

map-schema NPM version Build Status

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

Install

Install with npm:

$ npm i map-schema --save

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

Schema

Create a new Schema with the given options.

Params

  • options {Object}

Example

var schema = new Schema()
  .field('name', 'string')
  .field('version', 'string')
  .field('license', 'string')
  .field('licenses', {
    validate: function(val, key) {
      this.error(key, 'licenses is deprecated. use "license" instead.');
    }
  })
  .normalize(require('./package'))

.error

Push an error onto the schema.errors array. Placeholder for better error handling and a reporter (planned).

Params

  • method {String}: The name of the method where the error is recorded.
  • prop {String}: The name of the field for which the error is being created.
  • message {String}: The error message.
  • value {String}: The value associated with the error.
  • returns {any}

.field

Add a field to the schema with the given name, type or types, and options.

Params

  • name {String}
  • type {String|Array}
  • options {Object}
  • returns {Object}: Returns the instance for chaining.

Example

var semver = require('semver');

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

.get

Get field name from the schema. Get a specific property from the field by passing the property name as a second argument.

Params

  • name {Strign}
  • prop {String}
  • returns {Object|any}: Returns the field instance or the value of prop if specified.

Example

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

.omit

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

Params

  • key {String}: The property to remove
  • returns {Object}: Returns the instance for chaining.

.update

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

Params

  • key {String}: The property to update.
  • val {any}: Value of the property to update.
  • returns {Object}: Returns the instance for chaining.

.isOptional

Returns true if field name is an optional field.

Params

  • name {String}
  • returns {Boolean}

.isRequired

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

Params

  • name {String}
  • returns {Boolean}

.missingFields

Checks the config object for missing fields and. If found, an error message is pushed onto the schema.errors array, which can be used for reporting.

Params

  • config {Object}
  • returns {Array}

.sortObject

If a keys array is passed on the constructor options, or as a second argument to sortObject, this sorts the given object so that keys are in the same order as the supplied array of keys.

Params

  • config {Object}
  • returns {Object}: Returns the config object with keys sorted to match the given array of keys.

Example

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

.sortArrays

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

Params

  • config {Object}
  • returns {Object}: returns the config object with sorted arrays

.isValidType

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

Params

  • key {String}
  • val {any}
  • config {Object}
  • returns {Boolean}

.normalize

Normalize the given config object.

Params

  • {String}: key
  • {any}: value
  • {Object}: config
  • returns {Object}

.normalizeField

Normalize a field on the schema.

Params

  • {String}: key
  • {any}: value
  • {Object}: config
  • returns {Object}

.visit

Visit method over the given object or array.

Params

  • method {String}
  • value {Object|Array}
  • returns {Object}: Returns the instance for chaining.

Field

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

Params

  • type {String|Array}: One more JavaScript native types to use for validation.
  • config {Object}

Example

var 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

  • type {String}
  • returns {Boolean}

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

  • returns {Boolean}

Example

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

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

normalize-pkg: Normalize values in package.json to improve compatibility, programmatic readability and usefulness with third party libs. | homepage

Generate docs

Generate readme and API documentation with [verb][]:

$ npm i -d && npm run docs

Or, if [verb][] is installed globally:

$ verb

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

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

Author

Jon Schlinkert

License

Copyright © 2016 Jon Schlinkert Released under the MIT license.


This file was generated by verb, v0.9.0, on February 17, 2016.