# flow-dynamic

> A helper library link runtime type check to static flow check.

Latest version **0.0.14** (published 2016-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install flow-dynamic
pnpm add flow-dynamic
yarn add flow-dynamic
bun add flow-dynamic
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.14 |
| Published | 2016-11-29 |
| First published | 2016-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | iamchenxin |
| Maintainers | iamchenxin |
| Keywords | flow, graphql, relay |

## Links

- npm: https://www.npmjs.com/package/flow-dynamic
- Repository: https://github.com/iamchenxin/flow-dynamic
- Homepage: https://github.com/iamchenxin/flow-dynamic#readme
- Issues: https://github.com/iamchenxin/flow-dynamic/issues
- npm.io page: https://npm.io/package/flow-dynamic

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.0.14 (latest) — 2016-11-29
- 0.0.13 — 2016-11-27
- 0.0.12 — 2016-08-30
- 0.0.11 — 2016-08-11
- 0.0.10 — 2016-07-01
- 0.0.9 — 2016-06-06
- 0.0.8 — 2016-06-04
- 0.0.7 — 2016-06-04
- 0.0.6 — 2016-06-03
- 0.0.5 — 2016-06-03
- 0.0.4 — 2016-06-03
- 0.0.3 — 2016-06-03
- 0.0.2 — 2016-06-03
- 0.0.1 — 2016-06-03

## README

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

## For quick start
goto [flow-dynamic/wiki](https://github.com/iamchenxin/flow-dynamic/wiki/Home).<br/>

## 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`.
  1.  `pro`(product) contain all `validator` which do not modify the input be checked.
  2.  `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).
  3. `cvt`(convert). This namespace contain all `validator`s 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).
3. `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 namespace`graph` ).
4. Code demonstration could be find in `__tests__` folds in `src`, and my [pr](https://github.com/graphql/graphql-relay-js/pull/89) to `graphql-relay`.

---
_Source: https://npm.io/package/flow-dynamic · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
