attr-pipeline
Simple object validation and transformations.
Last updated 6 years ago by admosity .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install attr-pipeline 
SYNC missed versions from official npm registry.

Attribute Pipeline

Easy to use no frills object validation/object transformations.

Quick start

npm install attr-pipeline

var ap = require('attr-pipeline');

var validator = new ap.Validator({
  attr1: {required: true},
  // attribute with pipeline
  // will only apply if the attribute is present in the object
  attr2: {
    // pipeline functions executed in order and only if attribute exists
    // if required is not supplied
    pipe: [
      // single argument functions must return a boolean (for success/failure)
      function(value) {
        return typeof value === 'string';
      },
      // transformations must have a callback
      function(value, callback) {
        return callback(null, value + " World!");
      }
    ]
  }
});

// EXAMPLES

// with callbacks

validator.validate({}, function(err, object) {
  // error here attr1 is required
});

validator.validate({attr1: "anything"}, function(err, object) {
  console.log(object); // {attr1: "anything"}
});

validator.validate({attr1: "anything", attr2: 1}, function(err, object) {
  // error here attr2 is not a string
});

validator.validate({attr1: "anything", attr2: "Hello"}, function(err, object) {
  console.log(object); // {attr1: "anything", attr2: "Hello World!"}
});

// as promises

validator.validate({})
  .then(function(object) {
    // does not execute
  })
  .catch(function(err) {
    // error here attr1 is required
  })
  .done();

validator.validate({attr1: "anything"})
  .then(function(object) {
    console.log(object); // {attr1: "anything"}
  })
  .catch(function(err) {
    // does not execute
  })
  .done();

validator.validate({attr1: "anything", attr2: 1})
  .then(function(object) {
    // does not execute
  })
  .catch(function(err) {
    // error here attr2 is not a string
  })
  .done();

validator.validate({attr1: "anything", attr2: "Hello"})
  .then(function(object) {
    console.log(object); // {attr1: "anything", attr2: "Hello World!"}
  })
  .catch(function(err) {
    // does not execute
  })
  .done();

Current Tags

  • 0.6.0                                ...           latest (6 years ago)

6 Versions

  • 0.6.0                                ...           6 years ago
  • 0.5.0                                ...           6 years ago
  • 0.4.0                                ...           6 years ago
  • 0.3.0                                ...           6 years ago
  • 0.2.0                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 3
Dependencies (3)
Dev Dependencies (5)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |