0.17.0 • Published 7 years ago

flow-runtime v0.17.0

Weekly downloads
21,141
License
MIT
Repository
github
Last release
7 years ago

Flow Runtime

A runtime type system for JavaScript with full Flow compatibility.

What?

Provides a rich API for defining, inspecting and verifying data types in JavaScript. Any value that can be represented in JS can be represented by flow-runtime, including full support for polymorphism and parameterized types.

See the docs for more information.

Usage

import t from 'flow-runtime';

const number = t.number();
const string = t.string();

string.accepts('foo'); // true
string.accepts(123); // false
number.accepts(123); // true

string.assert('Hello World!'); // ok
string.assert(false); // throws
number.assert(456); // ok
number.assert('nope'); // throws

const numberOrString = t.union(number, string);

numberOrString.assert(123); // ok
numberOrString.assert("baz"); // ok
numberOrString.assert(false); // throws

const fooOrBar = t.union(
  t.string('foo'),
  t.string('bar')
);

fooOrBar.assert('foo'); // ok
fooOrBar.assert('bar'); // ok
fooOrBar.assert('qux'); // throws

const Thing = t.object(
  t.property('name', t.string()),
  t.property('url', t.nullable(t.string()))
);

Thing.assert({
  name: 'Example',
  url: 'http://example.com/'
}); // OK


Thing.assert({
  name: 'Example'
}); // OK

Thing.assert({
  name: false
}); // throws

const arrayOfStrings = t.array(t.string());

arrayOfStrings.assert()

// ---------------------------------------------

const UserStatus = t.union(
  t.string('PENDING'),
  t.string('ACTIVE'),
  t.string('INACTIVE')
);

const PreferenceName = t.union(
  t.string('marketingOptIn'),
  t.string('darkColourScheme')
);

const UserPreferences = t.object(
  t.indexer(PreferenceName, t.boolean())
);

const User = t.object({
  id: t.number(),
  name: t.string(),
  email: t.string(),
  status: UserStatus,
  preferences: UserPreferences
});

const validUser = {
  id: 123,
  name: 'Sally',
  email: 'sally@example.com',
  status: 'PENDING',
  preferences: {
    marketingOptIn: true
  }
};

const invalidUser = {
  id: false, // invalid
  name: 'Bob',
  email: 'bob@example.com',
  status: 'NOPE', // invalid
  preferences: {
    marketingOptIn: true,
    nope: true // invalid
  }
};

User.accepts(validUser); // true
User.accepts(invalidUser); // false

User.assert(validUser); // OK
User.assert(invalidUser); // throws TypeError
@hellpack/packermultipart-raw-parser@lanetix/odata-ast-transformations@lanetix/odata-parser@oss-stealth/base@oss-stealth/paper@gratico/npmlazada-nodejs-sdk@everything-registry/sub-chunk-1684swaggernautisomorphic-webpack@bodidata/events@chantelle/config@chantelle/gulp-tasks@chantelle/scriptsyosugats-runtime@forabi/gql@flowaccount/lazada-open-platform-sdk@infinitebrahmanuniverse/nolb-flow@lectro/enhancer-flowruntime@leansdk/leanes@maggiben/electron-clipboard-manager@maggiben/google-apis@maggiben/scurry@nod/scripts@nod/empty@jcoreio/auth0-meteor@rachio-npm/com-rachio-web-core@playlyfe/gql@sprucelabs/heartwood-components-reactmultipart-raw-parser-legacyraspberry-clientreact-alp-routenitro-sgnightingale-handlernightingale-error-processornightingale-file-outputpg-db-objectspipesjspostfix.jspooliot-clientpromise-callback-factoryshipxanh-oalazadashopify-coresocifi-rollup-configsimple-auth-clientgoogle-apisgrumbler-scriptshangalilalila-webpack-lib-configlucas-gqlmangakamastarmliwi-rethinkdblazada-sdklazada-apilazada-open-platform-sdkkrobotjskoackmender@tychot/gql@troveng/swagger-gen-js@vlsergey/babel-config@zalastax/nolb-flow@stefanwimmer128/core-types@stefanwimmer128/core-utils@stefanwimmer128/core@stefanwimmer128/core-class@stefanwimmer128/core-data@stefanwimmer128/core-eventalp-authalp-configalp-errors-browseralp-react-reduxalp-limosaalp-languagealaudababel-preset-playlyfebabel-plugin-conditionalbabel-plugin-conditional-skitasync-storage-replcloecabbie-asynccabbie-syncboleto-brdraperfinal-serverflow-runtime-cliflow-runtime-docsflow-runtime-fakerflowqlfodyfinal-arango
0.17.0

7 years ago

0.16.0

8 years ago

0.15.0

8 years ago

0.14.0

8 years ago

0.13.0

8 years ago

0.12.0

8 years ago

0.11.1

8 years ago

0.11.0

8 years ago

0.10.0

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago

0.8.0

8 years ago

0.7.0

8 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.0.0

9 years ago