Giter Club home page Giter Club logo

struct's Introduction

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);

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);
Validator Description Require Argument Source Code
number Check the type is a number false src/validator/number
int Check the type is a int false src/validator/int
float Check the type is a float false src/validator/float
string Check the type is a string false src/validator/string
bool Check the type is a bool false src/validator/bool
any Any type false src/validator/any
odd Check the type is a number and odd false src/validator/odd
even Check the type is a number and even false src/validator/even
json Check the type is json string false src/validator/json
eq(value) Equal to some value true src/validator/eq
gt(number) Greater then a number true src/validator/gt
gte(number) Greater then or equal a number true src/validator/gte
lt(number) Less then a number true src/validator/lt
lte(number) Less then or equal a number true src/validator/lte
bt(min, max) Between the min and max true src/validator/bt
in(array) The value is in the array true src/validator/in
len(int) The values's length property equal to xxx true src/validator/len
msg(message) Custom error message of this field true src/validator/msg
func(validatorFunc) Custom Validator true src/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

struct's People

Contributors

axetroy avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

struct's Issues

V2

Breaking Change:

  • Return error array, not only one error
  • Drop support array()/object() checker

New Feature

  • Custom error message

  • Support define a non object struct

  • Support Object/Array in Literal

before:

new Struct({
  name: type.string,
  age: type.int,
  address: type.object({
    city: type.string,
    code: type.int
  })
});

after:

new Struct({
  name: type.string,
  age: type.int,
  address: {
    city: type.string,
    code: type.int
  }
});
  • Support validate native constructor
new Struct({
  name: String,
  age: Number,
  address: {
    city: String,
    code: Number
  }
});

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

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.