npm.io
0.0.7 • Published 9 years ago

data-transform-descriptor

Licence
ISC
Version
0.0.7
Deps
3
Vulns
1
Weekly
0

data-transform-descriptor

A tiny module to describe and implement data transforms/mappings.

Uses validate.js (and some custom validators) to document a data transformation in terms of input and output requirements.

Demo site: https://lukepur.github.io/data-transform-descriptor/

Installation

npm install data-transform-descriptor

Usage

const createTransformer = require('data-transform-descriptor');

const adder = createTransformer(
    // input constraints (see validate.js)
   [
        {
            id: 'a',
            type: 'number',
            required: true
        },
        {
            id: 'b',
            type: 'number',
            required: true
        }
    ],
    // output constraints
    {
        type: 'number'
    },
    // worker resolvable
    {
        fn: add,
        args: ['$.a', '$.b']
    },
    // meta
    { name: 'Adder' }
);

adder.meta();
// -> { name: 'Adder', ...inputConstraints, ...outputConstraints }

adder.validateInput({a: 1, b: 2});
// -> undefined

adder.validateInput({a: 'a', b: 2});
// -> {a: ['a must be a number (got string)']}

adder.run({a: 1, b: 2});
// -> 3

adder.run({a: '1', b: 2});
// -> {a : ['a must be a number (got string))']}

adder.run({a: 1});
// -> {b : ["b is can't be blank", 'b must be a number (got undefined)']}

Why?

The motivation for data-transform-descriptor is to build a simple framework for describing units of work against data.

This could be enhanced with tooling to build rich systems for composing these units of work into much more useful workflows.