Giter Site home page Giter Site logo

flow-dynamic's Introduction

Flow-Dynamic

Dynamic type check library which will generate corresponding static Flow Type.

For quick start

goto flow-dynamic/wiki.

Why need dynamic check?

  1. Data go though typed and untyped packages.(this case should be resolved by flow typed all packages)
  2. Data are in a complex code stream between packages,too complex for flow type between those packages. (Like graphql's resolver,which be used in graphql-relay,and the params are narrowed to a custom type).
  3. Data from remote,out of your control.(In this case,flow-dynamic could be used at both client and server,with single type valid logic).

Recommend Validate Style

if the params need to check is short.I prefer write them inline. like:

const wrappedFn = check1(
(v) => ({
  mail:pro.isString.isEmail(v.mail) // check src.mail.
}), (v) => { // Now, the input data is validated and have a Flow type {mail:string}
  // .... doing something
});

if things gets longer,should write a separated caster:

const userValid = (src) => ({
  id:pro.isString(src.id),
  name:pro.isString(src.name),
  age:pro.isNumber(src.age),
  friend:pro.isArray.isNumArr.inLength(src.friend, {max:5}),
});

In your real project,there also should be an explicitly Flow type wrote by you.

type USER = {
  id:string,
  name:string,
  age:number,
  friend:number[]
};
const userValid = (src) => ({
  id:pro.isString(src.id),
  name:pro.isString(src.name),
  age:pro.isNumber(src.age),
  friend:pro.isArray.isNumArr.inLength(src.friend, {max:5}),
});

const resolver = check1(userValid, (src:USER) => {
  // .... doing something
});

Note: explicitly declare (src:USER) to your hand-writing Flow type is helpful, will avoid wrong typo in writing userValid. If the userValid you writing is not corresponding with USER , flow will notice you.

Functions

  1. validate functions are group by the corresponding Flow type.Like pro.isArray.isNumArr.inLength.
  2. There are three top namespace for validator. dev pro cvt.
  3. pro(product) contain all validator which do not modify the input be checked.
  4. dev(develop) is just a copy from pro. but when NODE_ENV != 'dev',all function in dev will do nothing,and just return the passed in value back. (So dev is used for those type check you think should not be checked in production environment).
  5. cvt(convert). This namespace contain all validators which will mutate the been checked value. (Like setdefault for passin value,or clip them. or more complex usage. Like Graphql's connection cursor can be narrowed to offset when you using an array-like data model).
  6. checker: The common checker is in normal.js. And i hand-writed some custom checker for graphql(in graphql-ck.js have more clearly Error notice,its in namespacegraph ).
  7. Code demonstration could be find in __tests__ folds in src, and my pr to graphql-relay.

flow-dynamic's People

Contributors

iamchenxin avatar

Stargazers

Daniel Torres avatar Sibelius Seraphini avatar

Watchers

James Cloos avatar  avatar

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.