Git Product home page Git Product logo

congruence's Introduction

congruence

NPM version License Build status Dependency Status

Validate Javascript objects using semantic templates. Written as a lodash 4+ mixin.

Install

$ npm install congruence --save

Introduction

_.mixin(require('congruence'));
var template = { module: _.isString,   version: semver.valid };
var object =   { module: 'abc', version: 'v1.0.0'     };
assert.isTrue(_.congruent(template, object));

Above, the object is congruent to the template because object.module is a string, and semver.valid[1] returns true for object.version.

It's like regular expressions for Javascript objects. Easily test the structure of Javascript objects using expressive templates. Designed as an lodash mixin.

Usage

Use this module to check the congruence of Javascript structures and validity of values using semantic templates. Suppose an object:

var obj = {
  megahertz: 266,
  message: 'hello world'
};

We use the built-in lodash matching functions to build a template (an isometry) that we can validate against. Here is a template that matches obj above:

var matchingTemplate = {
  megahertz: _.isNumber
  message: _.isString
};

But this will not match:

var failedTemplate = {
  megahertz: 500,
  foo: _.isFunction
};

Both properties will fail validation. If a non-function is given in the template value, it will be used as a strict equality check. megahertz is not equal to 500 so that fails. And the template requires foo to be a function, but obj.foo is not even defined.

Any lodash isXYZ function can be used as a predicate; you can also define your own, e.g.

var template = {
  a: function (list) {
    return _.all(list, function (value) {
      return (value % 2) > 0;
    });
  }
},
object = {
  a: [ 1, 3, 5 ]
};
assert.isTrue(_.congruent(template, object));

Examples

A. Simple Template Congruence

var object = {
  a: 3.1415926535,
  foo: {
    bar: {
      b: 'hello world',
      c: [ 1, 1, 2, 3, 5, 8 ],
      d: new Date()
    }
  }
};
var matchingTemplate = {
  a: 3.1415926535,
  foo: _.congruent({
    bar: _.congruent({
      b: _.isString,
      c: _.isArray,
      d: _.compose(_.not, _.isFunction)
    })
  })
};

assert.isTrue(_.congruent(matchingTemplate, object));

B. Simple Template Similarity

var template = {
  id: 57,
  name: 'Travis'
};
var object = {
  id: 57,
  name: 'Travis',
  color: 'blue',
  foo: 1
};

// the extra object properties are ignored
assert.isTrue(_.similar(template, object));

3. Full Lodash API

_.congruent(template, object)

Return true if the object matches all of the conditions in the specified template, and the keysets are identical.

@param description
template the congruence template used to validate the object
object the object to validate
@return description
Boolean true if the object is congruent to the template, false otherwise
var template = {
  <key>: <predicate>
};
var object = {
  <key>: <value>
};

_.congruent(template, object);

_.similar(template, object)

Return true if the object matches all the conditions specified by the template.

@param description
template the congruence template used to validate the object
object the object to validate
@return description
Boolean true if the object is congruent to the template, false otherwise
var template = {
  <key>: <predicate>
};
var object = {
  <key>: <value>
};

License

MIT

congruence's People

Contributors

iansltx avatar tjwebb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

isabella232

congruence's Issues

0.3

use underscore-contrib functions for predicates

congruence and xtuple-query

Hey, TJ

Looking through your congruence/xtuple-query code and trying to do something similar. I noticed xtuple-query uses an older version of congruence that has an "or" function to support using an array as a predicate. That was removed in this commit.

I'm wanting to use the latest version of congruence but can't quite figure out how you replaced the "or" method. From what I can tell you didn't, and you expect the user to write their own "or" predicate. Is this assumption correct or am I missing something?

A good example of what I am trying to achieve is something similar to this template.

Make it easier to validate nested arrays

Currently arrays basically can only be validated with _.isArray. Validating their elements requires additional programming logic, which diminishes the value of the template.

TypeError: Object.keys called on non-object

Should not be possible to cause this error

TypeError: Object.keys called on non-object
      at Function.keys (native)
      at _testSubtree (/home/tjwebb/workspace/xtuple/xtuple/node-datasource/lib/query/node_modules/congruence/congruence.js:31:22)
      at congruence.isDefined (/home/tjwebb/workspace/xtuple/xtuple/node-datasource/lib/query/node_modules/congruence/congruence.js:50:17)
      at Array.every (native)
      at Function._.every._.all (/home/tjwebb/workspace/xtuple/xtuple/node-datasource/lib/query/node_modules/underscore/underscore.js:193:62)
      at _testSubtree (/home/tjwebb/workspace/xtuple/xtuple/node-datasource/lib/query/node_modules/congruence/congruence.js:36:14)
      at Function.test (/home/tjwebb/workspace/xtuple/xtuple/node-datasource/lib/query/node_modules/congruence/congruence.js:92:12)

Error when not using as underscore mixin

Unrecoverable exception. Object function (obj) {
    if (obj instanceof _) return obj;
    if (!(this instanceof _)) return new _(obj);
    this._wrapped = obj;
  } has no method 'isObjectStrict'

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.