How to use the runtypes.Union function in runtypes

To help you get started, we’ve selected a few runtypes 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 pelotom / runtypes / examples / src / union-values.ts View on Github external
import { Static, Union, Literal, match } from 'runtypes';

// Define the runtype
const Day = Union(
  Literal('Sunday'),
  Literal('Monday'),
  Literal('Tuesday'),
  Literal('Wednesday'),
  Literal('Thursday'),
  Literal('Friday'),
  Literal('Saturday'),
);

// Extract the static type
type Day = Static; // = 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday'

// Extract enumerated literal values
const days: Day[] = Day.alternatives.map(lit => lit.value);

for (const day of days) {
github hmil / rest.ts / test / fixtures / testAPI.ts View on Github external
.response(rt.String),

    simplePatch: PATCH `/method/patch`
        .body(SimpleMessage)
        .response(rt.String),

    simpleDelete: DELETE `/method/delete`
        .response(rt.String),

    noTemplateString: GET('/path/string')
        .response(rt.String),

    simpleQueryParams: GET `/query`
        .query(rt.Record({
            'mandatory': rt.String,
            'union': rt.Union(rt.Literal('true'), rt.Literal('false')),
            'optional': rt.Union(rt.String, rt.Undefined)
        }))
        .response(QueryParamsResponse),

    optionalQueryParams: GET `/query/optional`
        .query({
            'maybeParam': '' as string | undefined
        }),

    simpleRequestBody: POST `/simpleBody`
        .body(TodoItem)
        .response(SavedTodoItem),

    noRepsonseEndpoint: PUT `/noResponse`,

    pathParams: GET `/path/${'kind'}/id/${'id'}`
github typeetfunc / runtypes-generate / src / custom.spec.ts View on Github external
Literal('sibling'),
    Literal('child'),
    Literal('parent'),
    Void
  ))
  const Member = Spouse.Or(NotSpouse)
  const FamilyWithTypeAndMember = (type, countSpouse) => Record({
    type,
    members: ArrayWithContains(Array(Member), Spouse, countSpouse)
  })
  const FamilyWithSpouse = FamilyWithTypeAndMember(
    Literal('espoused'),
    1
  )
  const FamilyWithoutSpouse = FamilyWithTypeAndMember(
    Union(
      Literal('single'),
      Literal('common_law_marriage'),
      Void
    ),
    0
  )
  const membersWithSpouse = ArrayWithContains(Array(Member), Spouse, 1)
  const FamilyObject = Union(FamilyWithSpouse, FamilyWithoutSpouse)
  test('Fio', generateAndCheck(Fio))
  test('Member', generateAndCheck(Member))
  test('Member', generateAndCheck(Member))
  test('MemberWithSpouse', generateAndCheck(membersWithSpouse))
  test('FamilyWithSpouse', generateAndCheck(FamilyWithSpouse))
  test('FamilyWithoutSpouse', generateAndCheck(FamilyWithoutSpouse))
  test('FamilyObject', generateAndCheck(FamilyObject))
});
github klemola / foobar2000-web-ui / server / Models.ts View on Github external
Literal('prev'),
    Literal('next'),
    Literal('rand')
)

export type PlaybackAction = Static
export const playbackActions: readonly PlaybackAction[] = [
    'play',
    'pause',
    'stop',
    'prev',
    'next',
    'rand'
]

export const VolumeAction = Union(
    Literal('vol mute'),
    Literal('vol down'),
    Literal('vol up')
)

export type VolumeAction = Static
export const volumeActions: readonly VolumeAction[] = [
    'vol mute',
    'vol down',
    'vol up'
]

export const MetaAction = Union(Literal('trackinfo'))
export type MetaAction = Static

export const Action = Union(PlaybackAction, VolumeAction, MetaAction)
github klemola / foobar2000-web-ui / server / Models.ts View on Github external
]

export const VolumeAction = Union(
    Literal('vol mute'),
    Literal('vol down'),
    Literal('vol up')
)

export type VolumeAction = Static
export const volumeActions: readonly VolumeAction[] = [
    'vol mute',
    'vol down',
    'vol up'
]

export const MetaAction = Union(Literal('trackinfo'))
export type MetaAction = Static

export const Action = Union(PlaybackAction, VolumeAction, MetaAction)
export type Action = Static

export const Config = Record({
    foobarPath: String,
    controlServerPort: Number,
    webserverPort: Number,
    controlServerMessageSeparator: String,
    environment: Env
})

export type Config = Static

export const FB2KInstance = (Unknown as Runtype<
github citycide / trilogy / src / types.ts View on Github external
CriteriaBase>[]

export type CriteriaListNormalized  =
  CriteriaBaseNormalized>[]

export type Criteria  =
  | CriteriaBase
  | CriteriaList

export type CriteriaNormalized  =
  | CriteriaBaseNormalized
  | CriteriaListNormalized

export const Index = t.Union(
  t.String,
  t.Array(t.Union(t.String, t.Array(t.String))),
  t.Dictionary(t.Union(t.String, t.Array(t.String)))
)

export const GroupClause = t.Union(
  t.String,
  t.Array(t.String)
)

export const OrderClause = t.Union(
  t.String,
  t.Tuple(t.String, t.String)
)

export const TrilogyOptions = t.Partial({
  client: t.Union(t.Literal('sqlite3'), t.Literal('sql.js')),
  dir: t.String
github citycide / trilogy / src / types.ts View on Github external
export type CriteriaList  =
  CriteriaBase>[]

export type CriteriaListNormalized  =
  CriteriaBaseNormalized>[]

export type Criteria  =
  | CriteriaBase
  | CriteriaList

export type CriteriaNormalized  =
  | CriteriaBaseNormalized
  | CriteriaListNormalized

export const Index = t.Union(
  t.String,
  t.Array(t.Union(t.String, t.Array(t.String))),
  t.Dictionary(t.Union(t.String, t.Array(t.String)))
)

export const GroupClause = t.Union(
  t.String,
  t.Array(t.String)
)

export const OrderClause = t.Union(
  t.String,
  t.Tuple(t.String, t.String)
)

export const TrilogyOptions = t.Partial({
github typeetfunc / runtypes-generate / src / custom.spec.ts View on Github external
const StringOrVoid = String.Or(Void)
  const Fio = Partial({
    firstname: StringOrVoid,
    lastname: StringOrVoid,
    middlename: StringOrVoid
  })
  const MemberWithRole = role => Record({
    role,
    fio: Fio
  }).And(
    Partial({
      dependant: Boolean.Or(Void)
    })
  )
  const Spouse = MemberWithRole(Literal('spouse'))
  const NotSpouse = MemberWithRole(Union(
    Literal('sibling'),
    Literal('child'),
    Literal('parent'),
    Void
  ))
  const Member = Spouse.Or(NotSpouse)
  const FamilyWithTypeAndMember = (type, countSpouse) => Record({
    type,
    members: ArrayWithContains(Array(Member), Spouse, countSpouse)
  })
  const FamilyWithSpouse = FamilyWithTypeAndMember(
    Literal('espoused'),
    1
  )
  const FamilyWithoutSpouse = FamilyWithTypeAndMember(
    Union(
github citycide / trilogy / src / types.ts View on Github external
t.Array(t.Union(t.String, t.Array(t.String))),
  t.Dictionary(t.Union(t.String, t.Array(t.String)))
)

export const GroupClause = t.Union(
  t.String,
  t.Array(t.String)
)

export const OrderClause = t.Union(
  t.String,
  t.Tuple(t.String, t.String)
)

export const TrilogyOptions = t.Partial({
  client: t.Union(t.Literal('sqlite3'), t.Literal('sql.js')),
  dir: t.String
})

export const ModelOptions = t.Partial({
  index: Index,
  primary: t.Array(t.String),
  unique: t.Array(t.String),
  timestamps: t.Boolean
})

export const AggregateOptions = t.Partial({
  distinct: t.Boolean,
  group: GroupClause,
  order: OrderClause
})