Socket
Socket
Sign inDemoInstall

js-data-schema

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-data-schema

Define and validate rules, datatypes and schemata in Node and in the browser.


Version published
Weekly downloads
87
increased by135.14%
Maintainers
1
Weekly downloads
 
Created
Source

js-data-schema

Define and validate rules, datatypes and schemata in Node and in the browser.

Install

Node

npm install --save js-data-schema

var Schemator = require('js-data-schema');
Browser

npm install --save js-data-schema or bower install --save js-data-schema

Load js-data-schema/dist/js-data-schema.js into your browser.

// global constructor if you're not using AMD or CommonJS
window.Schemator;

// AMD
define(['js-data-schema'], function (Schemator) { ... })

// CommonJS
var Schemator = require('js-data-schema');

Getting Started

var schemator = new Schemator();

schemator.defineSchema('Person', {
  name: 'string'
});

var errors = schemator.validateSync('Person', { name: 'John' });

errors; // null

errors = schemator.validateSync('Person', { name: 50043 });

errors; // {
              rule: 'type',
              actual: 'number',
              expected: 'string'
            }

Status

BranchMaster
BowerBower version
NPMNPM version
Build StatusBuild Status
Code ClimateCode Climate
Dependency StatusDependency Status
CoverageCoverage Status

API

Schemator

Schemator()
var schemator = new Schemator();
Schemator#availableDataTypes()
schemator.availableDataTypes(); // ['boolean', 'string', 'etc.']
Schemator#availableRules()
schemator.availableRules(); // ['type', 'minLength', 'etc.']
Schemator#availableSchemata()
schemator.defineSchema('PersonSchema', { ... });
schemator.availableSchemata(); // ['PersonSchema']
Schemator#getDataType(name)
schemator.getDataType('myDataType');
Schemator#getRule(name)
schemator.getRule('myRule');
Schemator#getSchema(name)
schemator.getSchema('PersonSchema');
Schemator#removeDataType(name)
schemator.removeDataType('myDataType');
Schemator#removeRule(name)
schemator.removeRule('myRule');
Schemator#removeSchema(name)
schemator.removeSchema('PersonSchema');
Schemator#defineDataType(name, typeDefinition)
schemator.defineDataType('NaN', function (x) {
  if (isNaN(x)) {
    return null;
  } else {
    return {
      rule: 'type',
      actual: typeof x,
      expected: 'NaN'
    };
  }
});
Schemator#defineRule(name, ruleFunc[, async])
schemator.defineRule('divisibleBy', function (x, divisor) {
  if (typeof x === 'number' && typeof divisor === 'number' && x % divisor !== 0) {
    return {
      rule: 'divisibleBy',
      actual: '' + x + ' % ' + divisor + ' === ' + (x % divisor),
      expected: '' + x + ' % ' + divisor + ' === 0'
    };
  }
  return null;
});

schemator.defineSchema('mySchema', {
  seats: {
    divisibleBy: 4
  }
});

var errors = schemator.getSchema('mySchema').validateSync({
  seats: 16
});

errors; //  null

errors = schemator.getSchema('mySchema').validateSync({
  seats: 17
});

errors; //  {
        //    seats: {
        //      errors: [ {
        //        rule: 'divisibleBy',
        //        actual: '17 % 4 === 1',
        //        expected: '17 % 4 === 0'
        //      } ]
        //    }
        //  }

Asynchronous rule:

schemator.defineRule('divisibleBy', function (x, divisor, cb) {
  
  // asynchronity here is fake, but you could do something async, like make an http request
  setTimeout(function () {
    if (typeof x === 'number' && typeof divisor === 'number' && x % divisor !== 0) {
      cb({
        rule: 'divisibleBy',
        actual: '' + x + ' % ' + divisor + ' === ' + (x % divisor),
        expected: '' + x + ' % ' + divisor + ' === 0'
      });
    }
    cb(null);
  }, 1);
}, true); // pass true as the third argument

schemator.defineSchema('mySchema', {
  seats: {
    divisibleBy: 4
  }
});

var errors = schemator.getSchema('mySchema').validate({
  seats: 16
}, function (err) {
  errors; //  null

  errors = schemator.getSchema('mySchema').validate({
    seats: 17
  }, function (err) {
    errors; //  {
            //    seats: {
            //      errors: [ {
            //        rule: 'divisibleBy',
            //        actual: '17 % 4 === 1',
            //        expected: '17 % 4 === 0'
            //      } ]
            //    }
            //  }  
  });
});
Schemator#defineSchema(name, definition)
schemator.defineSchema('PersonSchema', {
  name: {
    first: {
      type: 'string',
      maxLength: 255
    },
    last: {
      type: 'string',
      maxLength: 255
    }
  },
  age: {
    type: 'number',
    max: 150,
    min: 0
  }
});
Schemator#validate(schemaName, attrs[, options], cb)

See Schema#validate(attrs[, options], cb)

Schemator#validateSync(schemaName, attrs[, options])

See Schema#validateSync(attrs[, options])

Schemator#setDefaults(schemaName, attrs)

See Schema#setDefaults(attrs)

Schemator#getDefaults()

See Schema#getDefaults()

Schemator#addDefaultsToTarget(schemaName, target[, overwrite])

See Schema#addDefaultsToTarget(target)

Schemator#stripNonSchemaAttrs(schemaName, target)

See Schema#stripNonSchemaAttrs(target)

Schema

Schema#validate(attrs[, options], cb)
PersonSchema.validate({
  name: 'John Anderson'
}, function (err) {
  err; // null
});

PersonSchema.validate({
  name: 5
}, function (err) {
  err;  //  {
        //    name: {
        //      errors: [{
        //        rule: 'type',
        //        actual: 'number',
        //        expected: 'string'
        //      }]
        //    }
        //  }
});
Schema#validateSync(attrs[, options])
var errors = PersonSchema.validate({
  name: 'John Anderson'
});

errors; // null

errors = mySchema.validate({
  name: 5
});
errors; //  {
        //    name: {
        //      errors: [{
        //        rule: 'type',
        //        actual: 'number',
        //        expected: 'string'
        //      }]
        //    }
        //  }
Schema#setDefaults(attrs)
PersonSchema.setDefaults({
  first: '',
  last: '',
  plan: 'free'
});
Schema#getDefaults()
PersonSchema.getDefaults(); // {
                                 first: '',
                                 last: '',
                                 age: 0
                               }
Schema#addDefaultsToTarget(target[, overwrite])
var person = {
  first: 'John',
  plan: 'premium'
};

PersonSchema.addDefaultsToTarget(person);
 
person; // {
             first: 'John',
             last: '',
             plan: 'premium'
           }

PersonSchema.addDefaultsToTarget(person, true);
 
person; // {
             first: '',
             last: '',
             plan: 'free'
           }
Schema#stripNonSchemaAttrs(target)
var person = {
  first: 'John',
  plan: 'premium',
  nonSchema: 'value'
};

PersonSchema.stripNonSchemaAttrs(person);
 
person; // {
             first: 'John',
             plan: 'premium'
           }

License

MIT License

Copyright © 2013-2014 Jason Dobry

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 16 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc