How to use the io-ts.brand function in io-ts

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 maasglobal / maas-schemas / maas-schemas-ts / src / core / booking-option.ts View on Github external
},
    LegDeltaBrand
  > => true,
  'LegDelta',
);
export interface LegDeltaBrand {
  readonly LegDelta: unique symbol;
}

// BookingOption
// The default export. More information at the top.
export type BookingOption = t.Branded<
  ContentWithCost | ContentWithConfigurator,
  BookingOptionBrand
>;
export const BookingOption = t.brand(
  t.union([ContentWithCost, ContentWithConfigurator]),
  (x): x is t.Branded =>
    true,
  'BookingOption',
);
export interface BookingOptionBrand {
  readonly BookingOption: unique symbol;
}

export default BookingOption;

// Success
github maasglobal / maas-schemas / maas-schemas-ts / src / core / components / i18n.ts View on Github external
export type Locale = t.Branded;
export const Locale = t.brand(
  t.string,
  (x): x is t.Branded =>
    typeof x !== 'string' ||
    x.match(RegExp('^[a-z]{2,3}(?:-[a-zA-Z]{4})?(?:-[A-Z]{2,3})?$')) !== null,
  'Locale',
);
export interface LocaleBrand {
  readonly Locale: unique symbol;
}

// I18n
// The default export. More information at the top.
export type I18n = t.Branded;
export const I18n = t.brand(
  t.unknown,
  (x): x is t.Branded => true,
  'I18n',
);
export interface I18nBrand {
  readonly I18n: unique symbol;
}

export default I18n;

// Success
github maasglobal / maas-schemas / maas-schemas-ts / src / core / components / api-common.ts View on Github external
undefined
Common definitions for API schemas

!!! AUTO GENERATED BY CONVERT.TS REFRAIN FROM MANUAL EDITING !!!

*/

import * as t from 'io-ts';

export const schemaId = 'http://maasglobal.com/core/components/api-common.json';

// AcceptHeader
// SemVer versioning schemas for versioning our resources
export type AcceptHeader = t.Branded;
export const AcceptHeader = t.brand(
  t.string,
  (x): x is t.Branded =>
    typeof x !== 'string' ||
    x.match(
      RegExp(
        '\\bapplication/json[;,]\\s*version=((0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(-[\\da-z\\-]+(?:\\.[\\da-z\\-]+)*)?(?:\\+[\\da-z\\-]+(?:\\.[\\da-z\\-]+)*)?)\\b',
      ),
    ) !== null,
  'AcceptHeader',
);
export interface AcceptHeaderBrand {
  readonly AcceptHeader: unique symbol;
}

// UserAgentHeader
// The purpose of this remains a mystery
github maasglobal / maas-schemas / maas-schemas-ts / src / tsp / customer-registration / response.ts View on Github external
t.null,
]);

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

// Response
// The default export. More information at the top.
export type Response = t.Branded<
  {
    customer?: {};
  } & {
    customer: Defined;
  },
  ResponseBrand
>;
export const Response = t.brand(
  t.intersection([
    t.partial({
      customer: t.type({}),
    }),
    t.type({
      customer: Defined,
    }),
  ]),
  (
    x,
  ): x is t.Branded<
    {
      customer?: {};
    } & {
      customer: Defined;
    },
github maasglobal / maas-schemas / maas-schemas-ts / src / core / components / point-cost.ts View on Github external
undefined
Subscription option's point cost

!!! AUTO GENERATED BY CONVERT.TS REFRAIN FROM MANUAL EDITING !!!

*/

import * as t from 'io-ts';

export const schemaId = 'http://maasglobal.com/core/components/point-cost.json';

// PointCost
// The default export. More information at the top.
export type PointCost = t.Branded;
export const PointCost = t.brand(
  t.number,
  (x): x is t.Branded => true,
  'PointCost',
);
export interface PointCostBrand {
  readonly PointCost: unique symbol;
}

export default PointCost;

// Success
github maasglobal / maas-schemas / maas-schemas-ts / src / core / booking.ts View on Github external
}

// Cost
// The purpose of this remains a mystery
export type Cost = Cost_.Cost;
export const Cost = Cost_.Cost;

// Configurator
// The purpose of this remains a mystery
export type Configurator = Configurator_.Configurator;
export const Configurator = Configurator_.Configurator;

// TspId
// The purpose of this remains a mystery
export type TspId = t.Branded;
export const TspId = t.brand(
  t.string,
  (x): x is t.Branded =>
    (typeof x !== 'string' || x.length >= 1) &&
    (typeof x !== 'string' || x.length <= 256),
  'TspId',
);
export interface TspIdBrand {
  readonly TspId: unique symbol;
}

// Leg
// MaaS response may return any subset of legCore
export type Leg = t.Branded<
  {
    signature?: Common_.Signature;
    state?: Leg_.State;
github maasglobal / maas-schemas / maas-schemas-ts / src / core / components / travel-mode.ts View on Github external
// PrivateTransitMode
// A mode that is available on-demand for your personal use
export type PrivateTransitMode = t.Branded<
  string &
    (
      | 'BUSISH'
      | 'CABLE_CAR'
      | 'CAR'
      | 'FUNICULAR'
      | 'GONDOLA'
      | 'SHARED_CAR'
      | 'TAXI'
      | 'TRAINISH'),
  PrivateTransitModeBrand
>;
export const PrivateTransitMode = t.brand(
  t.intersection([
    t.string,
    t.union([
      t.literal('BUSISH'),
      t.literal('CABLE_CAR'),
      t.literal('CAR'),
      t.literal('FUNICULAR'),
      t.literal('GONDOLA'),
      t.literal('SHARED_CAR'),
      t.literal('TAXI'),
      t.literal('TRAINISH'),
    ]),
  ]),
  (
    x,
  ): x is t.Branded<
github maasglobal / maas-schemas / maas-schemas-ts / src / core / components / units.ts View on Github external
x.match(RegExp('^[A-Fa-f0-9]{8}(-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}$')) !== null,
  'Uuid',
);
export interface UuidBrand {
  readonly Uuid: unique symbol;
}
/** examplesUuid // => { _tag: 'Right', right: examplesUuidJson } */
export const examplesUuidJson: NonEmptyArray = [
  '4828507e-683f-41bf-9d87-689808fbf958',
];
export const examplesUuid = nonEmptyArray(Uuid).decode(examplesUuidJson);

// Url
// Uniform resource locator, see https://en.wikipedia.org/wiki/Uniform_Resource_Locator and https://mathiasbynens.be/demo/url-regex
export type Url = t.Branded;
export const Url = t.brand(
  t.string,
  (x): x is t.Branded =>
    typeof x !== 'string' ||
    x.match(RegExp('^(https?|ftp):\\/\\/[^\\s/$.?#].[^\\s]*$')) !== null,
  'Url',
);
export interface UrlBrand {
  readonly Url: unique symbol;
}

// Arn
// The purpose of this remains a mystery
export type Arn = t.Branded;
export const Arn = t.brand(
  t.string,
  (x): x is t.Branded =>
github maasglobal / maas-schemas / maas-schemas-ts / src / maas-backend / itineraries / itinerary-list / request.ts View on Github external
{
    identityId?: Units_.IdentityId;
    payload?: {
      startTime?: Units_.Time;
      endTime?: Units_.Time;
      states?: string;
      bookingId?: Booking_.Id;
      originalFare?: string;
    };
    headers?: ApiCommon_.Headers;
  } & {
    identityId: Defined;
  },
  RequestBrand
>;
export const Request = t.brand(
  t.intersection([
    t.partial({
      identityId: Units_.IdentityId,
      payload: t.partial({
        startTime: Units_.Time,
        endTime: Units_.Time,
        states: t.string,
        bookingId: Booking_.Id,
        originalFare: t.string,
      }),
      headers: ApiCommon_.Headers,
    }),
    t.type({
      identityId: Defined,
    }),
  ]),
github maasglobal / maas-schemas / maas-schemas-ts / src / maas-backend / bookings / bookings-list / request.ts View on Github external
export type Request = t.Branded<
  {
    identityId?: Units_.IdentityId;
    payload?: {
      startTime?: Units_.Time;
      endTime?: Units_.Time;
      states?: string;
      modes?: string;
    };
    headers?: ApiCommon_.Headers;
  } & {
    identityId: Defined;
  },
  RequestBrand
>;
export const Request = t.brand(
  t.intersection([
    t.partial({
      identityId: Units_.IdentityId,
      payload: t.partial({
        startTime: Units_.Time,
        endTime: Units_.Time,
        states: t.string,
        modes: t.string,
      }),
      headers: ApiCommon_.Headers,
    }),
    t.type({
      identityId: Defined,
    }),
  ]),
  (