2.2.1 • Published 7 years ago

@axetroy/struct v2.2.1

Weekly downloads
-
License
Apache
Repository
github
Last release
7 years ago

Struct

Greenkeeper badge Build Status Coverage Status Dependency License Prettier Node npm version Size

A Modern, Scalable , Graceful, Easy Use data structure validator, Support browser and NodeJs

  • All in Javascript. No Magic string.
  • Strict mode, no one excess field.
  • Most of type validator support.
  • Scalable, easy to define your customize validator.
  • Highly customizable.
  • Validate with params, Support pass the argument to the validator.
  • Pipe line, multiple validator work together.
  • Support endless nest object, including Object and Array.
  • Clear error message.
  • Support nest Struct

Quick start

npm install @axetroy/struct --save
const { Struct, type } = require('@axetroy/struct');

const data = {
  name: 'axetroy',
  age: 18,
  address: {
    city: 'DC',
    code: '12' // invalid city code, it should be an integer
  }
};

const User = Struct({
  name: type.string,
  age: type.int,
  address: {
    city: type.string,
    code: type.int
  }
});

const err = User.validate(data);

console.log(err); // if all validator success, the error should be undefined

/**
{ Error
    at Object.<anonymous> (/home/axetroy/gpm/github.com/axetroy/struct/src/error.js:19:23)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/axetroy/gpm/github.com/axetroy/struct/src/type.js:2:19)
    at Module._compile (module.js:635:30)
  validator: 'int',
  path: [ 'address', 'code' ],
  value: '12',
  detail: 'Expected a value of type `int` for `address.code` but received `12`.',
  message: 'Expected a value of type `int` for `address.code` but received `12`.' }
 */

Advanced usage

const { Struct, type } = require('@axetroy/struct');

const data = {
  name: 'axetroy',
  age: 18,
  address: {
    city: 'DC',
    code: 100
  },
  message: [
    { from: 'marry', msg: 'How are you?', timestamp: 1513155028 },
    { from: 'henry', msg: "How's going one?", timestamp: 1513135028 }
  ]
};

const User = new Struct({
  name: type.string,
  age: type.int.gte(18), // age is int && and age >= 18
  address: {
    city: type.string,
    code: type.int.gte(100)
  },
  message: [
    {
      from: type.string,
      msg: type.string,
      timestamp: type.int
    }
  ]
});

const err = User.validate(data);

console.log(err); // undefined, because the data pass the validator

Document

class: Struct

Create a struct

const { Struct, type } = require('@axetroy/struct');

const struct1 = new Struct(type.string);
const struct2 = Struct(type.string);

static Struct.define

static Struct.Type

struct.validate(data)

const err = Struct.validate({ word: 'Hello world' });

validate the data is match with struct, if all match. return undefined, if not, return an TypeError

class: Type

Create a type

static: Type.define(validatorName, handler)

  • validatorName:
  • handler: <(input):bool | (argv):(input):bool>
Type.define('email', function(input) {
  // here to check is it a email string
  return true;
});

define a customize type, will add an property on type.prototype

type.xxx

const stringType = type.string;
const intType = type.int;
const composingType = type.int.gte(100);
ValidatorDescriptionRequire ArgumentSource Code
numberCheck the type is a numberfalsesrc/validator/number
intCheck the type is a intfalsesrc/validator/int
floatCheck the type is a floatfalsesrc/validator/float
stringCheck the type is a stringfalsesrc/validator/string
boolCheck the type is a boolfalsesrc/validator/bool
anyAny typefalsesrc/validator/any
oddCheck the type is a number and oddfalsesrc/validator/odd
evenCheck the type is a number and evenfalsesrc/validator/even
jsonCheck the type is json stringfalsesrc/validator/json
eq(value)Equal to some valuetruesrc/validator/eq
gt(number)Greater then a numbertruesrc/validator/gt
gte(number)Greater then or equal a numbertruesrc/validator/gte
lt(number)Less then a numbertruesrc/validator/lt
lte(number)Less then or equal a numbertruesrc/validator/lte
bt(min, max)Between the min and maxtruesrc/validator/bt
in(array)The value is in the arraytruesrc/validator/in
len(int)The values's length property equal to xxxtruesrc/validator/len
msg(message)Custom error message of this fieldtruesrc/validator/msg
func(validatorFunc)Custom Validatortruesrc/validator/func

All the validator is define on type.prototype.

class: TypeError

  • validator: What validator fail
  • path: What key not pass the validator
  • value: The value which not pass the validator
  • message: The error message
  • detail: The error message

The TypeError inherit from Error

Examples

There is the examples, may be it can help you

Contributing

Contributing Guid

Contributors

Axetroy💻 🐛 🎨

License

FOSSA Status

2.2.1

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago

2.0.0

8 years ago

1.2.0

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago