0.0.3 • Published 5 years ago

data_walk v0.0.3

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

data_walk

check and set default value for you value that not sure!

installation

npm install data_walk

usage

import { $walk, $typeCheck } from 'data_walk';
const { $string } = type;
const afterWalk = $walk(
  $object({
    a: $typeCheck('string'),
  }),
)({
  a: 'abc',
}); // { a: "abc" }

check and warn

if it's not the type we want, warn it.

we provide undefined, null, symbol, string, number, boolean,promise, function, array, object

and optional type if you want to skip it

and oneOf type if you want multiple type

you can use custom validation function to validate, but need to put it into test.

ideal Way:

grandparent/parent/children/superJunior:
  set       value: 2;
            type: number;

  required  type: string;

将一个数据传入, 然后这个数据通过层层验证

use test("", () => {

}); to replace the default error message

helper Function

nametypeexample
$walk(validate: (value: T) => boolean | any[] | string) => (value: T) => boolean\$walk($checkType("string"))("hahaha")
$const(value1: any) => (value: any) => true | string$const("haha")("hahaha") const required haha got       hahaha
$typeCheck(type: string) => (value: any) => string | int | float ....$typeCheck("string")(1) type check require string  haha           got value          type number
$array(validateArray: any[]) => (array: any[]) => boolean | string | any[]$array($typeCheck("string"))("haha")
$object(validateObj: any) => (obj: any) => boolean | string | any[]$object({hahah: $typeCheck("string")})({hahah: "haha"})
$oneOf(...typeArray: any[]) => (value: T) => boolean | string | any[]$oneOf($const("lalala"), $const("nanana"))("asdf")
$optional(func: (value: T) => boolean | string | any[]) => (value: T) => boolean | string | any[]$optional($const("dd"))("dd")
$validate(func: (value: T) => boolean) => (value: T) =>$validate($const("haha"))("hahah") => boolean | string | any[]
$test(errorMessage: string, func: (value: T) => boolean | string | any[]) => (value: T) => boolean | string | any[]$test("test it tobe lt 1", const("hahaha"))("hahah")
$every(...funcArray: Array<(value: any) => boolean | any[] | string>) => (value: any) => boolean | string | any[]$every($typeCheck("int"), $validate(gt(10)), $validate(lt(100)))
0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago