How to use io-ts - 10 common examples

To help you get started, we’ve selected a few io-ts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tangdrew / fhir-ts / packages / primitives / src / R4 / id.ts View on Github external
* ID FHIR Primitive Runtime Type
 */

import { Type, success, failure, identity, TypeOf } from "io-ts";

// TODO: This regex seems incorrect as well
const ID_REGEX = /[A-Za-z0-9\-\.]{1,64}/;

const isId = (m: unknown): m is string =>
  typeof m === "string" && ID_REGEX.test(m);

/**
 * Any combination of upper- or lower-case ASCII letters ('A'..'Z', and 'a'..'z', numerals ('0'..'9'),
 * '-' and '.', with a length limit of 64 characters.
 */
export const id = new Type(
  "id",
  isId,
  (m, c) => (isId(m) ? success(m) : failure(m, c)),
  identity
);

export type id = TypeOf;
github maasglobal / maas-schemas / maas-schemas-ts / src / maas-backend / customers / verification / initiate / request.ts View on Github external
import * as t from 'io-ts';
import * as Units_ from 'maas-schemas-ts/core/components/units';
import * as ApiCommon_ from 'maas-schemas-ts/core/components/api-common';

type Defined =
  | Record
  | Array
  | string
  | boolean
  | number
  | null;
const Defined = t.union([
  t.UnknownRecord,
  t.UnknownArray,
  t.string,
  t.boolean,
  t.number,
  t.null,
]);

export const schemaId =
  'http://maasglobal.com/maas-backend/customers/verification/initiate/request.json';

// Request
// The default export. More information at the top.
export type Request = t.Branded<
  {
    customerId?: Units_.IdentityId;
    headers?: ApiCommon_.Headers;
    identityId?: Units_.IdentityId;
    payload?: {
github AugurProject / augur / packages / augur-node / src / server / getters / get-dispute-tokens.ts View on Github external
import * as t from "io-ts";
import Knex from "knex";
import { formatBigNumberAsFixed } from "../../utils/format-big-number-as-fixed";
import { BigNumber, DisputeTokensRowWithTokenState, ReportingState, UIDisputeTokenInfo, UIDisputeTokens } from "../../types";
import { reshapeDisputeTokensRowToUIDisputeTokenInfo } from "./database";

export const DisputeTokenState = t.keyof({
  ALL: null,
  UNCLAIMED: null,
  UNFINALIZED: null,
});

export const DisputeTokensParams = t.type({
  universe: t.string,
  account: t.string,
  stakeTokenState: t.union([DisputeTokenState, t.null, t.undefined]),
});

export async function getDisputeTokens(db: Knex, augur: {}, params: t.TypeOf) {
  const query: Knex.QueryBuilder = db.select(["payouts.*", "disputes.crowdsourcerId as disputeToken", "balances.balance", "market_state.reportingState"]).from("disputes");
  query.join("markets", "markets.marketId", "crowdsourcers.marketId");
  query.leftJoin("market_state", "markets.marketStateId", "market_state.marketStateId");
  query.leftJoin("blocks", "markets.creationBlockNumber", "blocks.blockNumber");
  query.join("crowdsourcers", "crowdsourcers.crowdsourcerId", "disputes.crowdsourcerId");
  query.join("payouts", "payouts.payoutId", "crowdsourcers.payoutId");
  query.join("balances", "crowdsourcers.crowdsourcerId", "balances.token");
  query.where("universe", params.universe).where("disputes.reporter", params.account).where("balances.balance", ">", 0).where("balances.owner", params.account);
  if (params.stakeTokenState == null || params.stakeTokenState === "ALL") {
    // currently, do nothing, leaving this in case we want to flavor how we group or present response
  } else if (params.stakeTokenState === "UNFINALIZED") {
github elastic / kibana / x-pack / legacy / plugins / fleet / server / repositories / agent_events / types.ts View on Github external
t.literal('STATE'),
  t.literal('ERROR'),
  t.literal('ACTION_RESULT'),
  t.literal('ACTION'),
]);

export const RuntimeAgentEventSubtype = t.union([
  // State
  t.literal('RUNNING'),
  t.literal('STARTING'),
  t.literal('IN_PROGRESS'),
  t.literal('CONFIG'),
  t.literal('FAILED'),
  t.literal('STOPPED'),
  // Action results
  t.literal('DATA_DUMP'),
  // Actions
  t.literal('ACKNOWLEDGED'),
  t.literal('UNKNOWN'),
]);

export const RuntimeAgentEvent = t.intersection(
  [
    t.interface({
      type: RuntimeAgentEventType,
      subtype: RuntimeAgentEventSubtype,
      timestamp: t.string,
      message: t.string,
    }),
    t.partial({
      payload: t.any,
      data: t.string,
github wemake-services / wemake-vue-template / template / client / logic / comments / types.ts View on Github external
// This is a definition of domain model for `comments` context.

import * as ts from 'io-ts'

// Runtime type, that can be used for schema validation:
export const RawComment = ts.type({
  'id': ts.number,
  'body': ts.string,
  'email': ts.string,
})

// Static TypeScript type, that can be used as a regular `type`:
export type RawCommentType = ts.TypeOf

// We add rating only on the client (for demo purposes)
export type CommentType = RawCommentType & {
  rating: number
}

export interface CommentPayloadType {
  commentId: number
  delta: number
github nomiclabs / buidler / packages / buidler-core / src / internal / core / config / config-validation.ts View on Github external
return new t.Type(
    name,
    (u: unknown): u is TypeT | undefined => u === undefined || codec.is(u),
    (u, c) => (u === undefined ? t.success(u) : codec.validate(u, c)),
    a => (a === undefined ? undefined : codec.encode(a))
  );
}

// IMPORTANT: This t.types MUST be kept in sync with the actual types.

const BuidlerNetworkAccount = t.type({
  privateKey: t.string,
  balance: t.string
});

const BuidlerNetworkConfig = t.type({
  hardfork: optional(t.string),
  chainId: optional(t.number),
  from: optional(t.string),
  gas: optional(t.union([t.literal("auto"), t.number])),
  gasPrice: optional(t.union([t.literal("auto"), t.number])),
  gasMultiplier: optional(t.number),
  accounts: optional(t.array(BuidlerNetworkAccount)),
  blockGasLimit: optional(t.number),
  throwOnTransactionFailures: optional(t.boolean),
  throwOnCallFailures: optional(t.boolean)
});

const HDAccountsConfig = t.type({
  mnemonic: t.string,
  initialIndex: optional(t.number),
  count: optional(t.number),
github maasglobal / maas-schemas / maas-schemas-ts / src / core / product.ts View on Github external
!!! AUTO GENERATED BY CONVERT.TS REFRAIN FROM MANUAL EDITING !!!

*/

import * as t from 'io-ts';
import * as Fare_ from 'maas-schemas-ts/core/components/fare';
import * as Common_ from 'maas-schemas-ts/core/components/common';

type Defined =
  | Record
  | Array
  | string
  | boolean
  | number
  | null;
const Defined = t.union([
  t.UnknownRecord,
  t.UnknownArray,
  t.string,
  t.boolean,
  t.number,
  t.null,
]);

export const schemaId = 'http://maasglobal.com/core/product.json';

// Id
// The purpose of this remains a mystery
export type Id = t.Branded;
export const Id = t.brand(
  t.string,
  (x): x is t.Branded =>
github maasglobal / maas-schemas / maas-schemas-ts / src / tsp / stations-list / response.ts View on Github external
!!! AUTO GENERATED BY CONVERT.TS REFRAIN FROM MANUAL EDITING !!!

*/

import * as t from 'io-ts';
import * as Station_ from 'maas-schemas-ts/core/components/station';

type Defined =
  | Record
  | Array
  | string
  | boolean
  | number
  | null;
const Defined = t.union([
  t.UnknownRecord,
  t.UnknownArray,
  t.string,
  t.boolean,
  t.number,
  t.null,
]);

export const schemaId = 'http://maasglobal.com/tsp/stations-list/response.json';

// Response
// The default export. More information at the top.
export type Response = t.Branded<
  {
    stations?: Array<
      {
github maasglobal / maas-schemas / maas-schemas-ts / src / maas-backend / tsp-auth / init / response.ts View on Github external
!!! AUTO GENERATED BY CONVERT.TS REFRAIN FROM MANUAL EDITING !!!

*/

import * as t from 'io-ts';
import * as Units_ from 'maas-schemas-ts/core/components/units';

type Defined =
  | Record
  | Array
  | string
  | boolean
  | number
  | null;
const Defined = t.union([
  t.UnknownRecord,
  t.UnknownArray,
  t.string,
  t.boolean,
  t.number,
  t.null,
]);

export const schemaId = 'http://maasglobal.com/maas-backend/tsp-auth/init/response.json';

// Response
// The default export. More information at the top.
export type Response = t.Branded<
  {
    authUrl?: Units_.Url;
  } & {
github maasglobal / maas-schemas / maas-schemas-ts / src / core / components / travel-mode.ts View on Github external
PrivateTransitModeBrand
  > => true,
  'PrivateTransitMode',
);
export interface PrivateTransitModeBrand {
  readonly PrivateTransitMode: unique symbol;
}

// TravelMode
// The default export. More information at the top.
export type TravelMode = t.Branded<
  WaitingMode | TransferMode | PersonalMode | PublicTransitMode | PrivateTransitMode,
  TravelModeBrand
>;
export const TravelMode = t.brand(
  t.union([
    WaitingMode,
    TransferMode,
    PersonalMode,
    PublicTransitMode,
    PrivateTransitMode,
  ]),
  (
    x,
  ): x is t.Branded<
    WaitingMode | TransferMode | PersonalMode | PublicTransitMode | PrivateTransitMode,
    TravelModeBrand
  > => true,
  'TravelMode',
);
export interface TravelModeBrand {
  readonly TravelMode: unique symbol;