Giter Site home page Giter Site logo

map-schema's Introduction

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 ❤️ and support.

Table of Contents

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

  • options {Object}

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

  • key {String}
  • value {Object}

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

Params

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

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;
    }
  })

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

Returns true if field name is an optional field.

Params

  • name {String}
  • returns {Boolean}

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

Params

  • name {String}
  • returns {Boolean}

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

  • config {Object}
  • returns {Array}

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: ''}

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

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

Params

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

Normalize the given config object.

Params

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

Normalize a field on the schema.

Params

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

Visit method over the given object or array.

Params

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

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

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

Returns true if the given type is a valid type.

Params

  • type {String}
  • returns {Boolean}

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 the field's value.

Example

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

About

Contributing

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

Running Tests

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
Building docs

(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

Related projects

You might also be interested in these projects:

  • get-value: Use property paths like 'a.b.c' to get a nested value from an object. Even works… more | homepage
  • normalize-pkg: Normalize values in package.json using the map-schema library. | homepage
  • object.omit: Return a copy of an object excluding the given key, or array of keys. Also… more | homepage
  • object.pick: Returns a filtered copy of an object with only the specified keys, similar to _.pick… [more](https://github.com/jonschlinkert/object.pick) | [homepage](https://github.com/jonschlinkert/object.pick "Returns a filtered copy of an object with only the specified keys, similar to_.pick` from lodash / underscore.")
  • set-value: Create nested values and any intermediaries using dot notation ('a.b.c') paths. | homepage

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.

map-schema's People

Contributors

jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

map-schema's Issues

Normalization not working as expected

Considering the following example:

const Schema = require("map-schema");

const TestSchema = new Schema();
TestSchema.field("bool", "boolean");

console.log(TestSchema.normalize({
  bool: 1
}));

I would expect that the bool: 1 is changed to bool: true. But it stays as 1. Am I missing something? I don't want to have to specify normalization functions for all records as that makes the type parameter quite meaningless.

Hope I'm just missing something here :)

Upgrade set-value to latest version 4.1.0

set-value is vulnerable to prototype pollution due to insufficient input validation. An attacker could exploit this vulnerability to modify the Object prototype properties which causes additional properties or changes to the objects which could result in denial-of-service (DoS) or alter the integrity of the application. If the application uses polluted objects in conjunction with code execution primitives (such as eval), it may be possible for an attacker to execute malicious code.

https://security.snyk.io/vuln/SNYK-JS-SETVALUE-450213

Memory leaks after continued use

I create my schemas in global state - simply at the start of a module in NodeJS. I don't create them for every request (API) that I normalise for performance reasons, but that this ends up doing is keeping the schemas in memory along with their lengthy errors/results. As more and more payloads are validated, the memory use increases to the point of crashing whatever service is using normalize.

I'd recommend one of the following options:

  • Remove the errors/results from the schemas (make that a part of the outcome of calling schema.normalize rather than recording them on instance) (preferred)
  • Document this potential leak in a noticeable place in your readme

The issue is this, I believe:

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

And here you can see how they're being retained, as described:

image

I'm using it like so:

const Schema = require("map-schema");

function createSchema() {
    return new Schema();
}

const AnswerSchema = createSchema()
    .field("id", "number")
    .field("questionID", "number")
    .field("text", "string");

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.