@axetroy/struct
v2.2.1
Published
A Modern, Scalable , Graceful, Easy Use data structure validator
Downloads
27
Readme
Struct
A Modern, Scalable , Graceful, Easy Use data structure validator, Support browser and NodeJs
- [x] All in Javascript. No Magic string.
- [x] Strict mode, no one excess field.
- [x] Most of type validator support.
- [x] Scalable, easy to define your customize validator.
- [x] Highly customizable.
- [x] Validate with params, Support pass the argument to the validator.
- [x] Pipe line, multiple validator work together.
- [x] Support endless nest object, including Object and Array.
- [x] Clear error message.
- [x] 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)
data
: <*>- returns: <Undefined | TypeError>
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
- Basic Validation
- Custom Validator
- Custom Validator With Argument
- Custom Validator Function
- Custom Validator Function With Type
- Composing Validator
- Composing Struct
- Composing Struct In Array
- Custom Error Message
- Deep Nest Object
- Deep Nest Array
There is the examples, may be it can help you
Contributing
Contributors
| Axetroy💻 🐛 🎨 | | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |