0.9.2 • Published 5 years ago

field-inspector v0.9.2

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

node-field-inspector

Tiny field inspector for NodeJS

Install

npm install field-inspector --save

Usage

var inspector = require('field-inspector');

var ti = inspector.newInstance({
    token: {},
    game_id: {
        digit: true
    },
    user_id: {
        dight: true,
        default: 0
    },
    type: {
        values: [1, 2, 3]
    },
    description: {
        trim: true,
        empty: true,
        length_max: 128
    },
    tolower: {
        tolower: true
    },
    toupper: {
        toupper: true
    }
});

var value = {
    token: 'token',
    game_id: 1110,
    type: 1,
    description: ' abc ',
    tolower: 'Is Lower',
    toupper: 'Is Upper'
};

console.log('before', JSON.stringify(value));
var rst = ti.perform(value);
console.log('after', rst, JSON.stringify(value));

Output

before {"token":"token","game_id":1110,"type":1,"description":" abc ","tolower":"Is Lower","toupper":"Is Upper"}
after {"token":"token","game_id":1110,"type":1,"description":"abc","tolower":"is lower","toupper":"IS UPPER","user_id":0}

API

newInstance(model)

Create a field-inspector instance.

the model has two struction support
1) K/V object, V has more rules.

{remark: {empty: true}}
2) Array(object), object require a field 'name' for K, other fields was the rules.
{name: "remark", empty: true}

KeyTypeDefaultDescription
typestringundefinedthe typeof result check
emptybooleanfalseaccept empty value,
defaultstringOrNumberundefinedthe default value when empty
candidatestringundefinedaccept empty when has another prameter
matchstringundefinedaccept value when equal another prameter
trimbooleanfalsetrim the value string
tolowerbooleanfalseconvert value string to lower case
toupperbooleanfalseconvert value string to upper case
digitbooleanfalseaccept digit number value
lengthArray(number)min,maxundefinedaccept vlaue length IN range
length_minnumber1accept value minimal length
length_maxnumberundefinedaccept value maximal length
wordbooleanfalseaccept value match Word
regexpstringfalseaccept value match regexp
valuesArray(stringOrNumber)undefinedvalidate value IN Array
rangeArray(number)min,maxundefinedaccept vlaue IN range
range_minnumberundefinedaccept minimal value
range_maxnumberundefinedaccept maximal value

perform(object)

field-inspector proform the rules, it modify the parameter object.
it return a null whe no error or a *error object when detected a error.

Error object

KeyTypeDescription
namestringfield name
typestringthe error type

values for error type: empty ,length, type, value, match, regexp.

{ name: 'text', type: 'regexp' }
0.9.2

5 years ago

0.9.1

6 years ago

0.9.0

6 years ago