How to use the runtypes.Literal 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.

github Hypercubed / dynamo / tests / types-runtypes.spec.ts View on Github external
// tslint:disable-next-line:variable-name
const Name = convertConstraint(nameConstraint);
type Name = InstanceType<typeof Name>;

const ageConstraint = NumberType.withConstraint(n => {
  return Number.isInteger(n) && n >= 0;
});

// tslint:disable-next-line:variable-name
const Age = convertConstraint(ageConstraint);
type Age = InstanceType<typeof Age>;

const personRecord = Record({
  name: nameConstraint,
  age: ageConstraint,
  kind: Literal('$person')
});
// tslint:disable-next-line:variable-name
const Person = convertConstraint(personRecord);
type Person = Static<typeof personRecord>;

dynamo.add(Name, Age, Person);

class CreatePerson {
  @signature()
  person(name: Name, age: Age): Person {
    return { name, age, kind: '$person' };
  }
}

const createPerson = dynamo.function(CreatePerson);
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<typeof Day>; // = '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) {
  console.log(`Good morning, it's ${day}!`);
}
github pelotom / runtypes / examples / src / space-game.ts View on Github external
});
type Planet = Static<
  typeof Planet
>; /* = {
  type: 'planet';
  location: Vector;
  mass: NonNegative;
  population: NonNegative;
  habitable: Boolean;
}*/

const Rank = Union(
  Literal('captain'),
  Literal('first mate'),
  Literal('officer'),
  Literal('ensign'),
);
type Rank = Static<typeof Rank>; // = 'captain' | 'first mate' | 'officer' | 'ensign'

const CrewMember = Record({
  name: String,
  age: NonNegative,
  rank: Rank,
  home: Planet,
});
type CrewMember = Static<
  typeof CrewMember
>; /* = {
  name: string;
  age: NonNegative;
  rank: Rank;
  home: Planet;
github jmcdo29 / zeldaPlay / apps / api / src / app / config / model / env.model.ts View on Github external
import { Literal, Record, Static, String, Undefined, Union } from 'runtypes';

const NodeEnv = Union(
  Literal('prod'),
  Literal('production'),
  Literal('dev'),
  Literal('development'),
  Literal('test'),
);

const LogLevel = Union(
  Literal('INFO'),
  Literal('FATAL'),
  Literal('DEBUG'),
  Literal('ERROR'),
  Literal('WARN'),
  Literal('FINE'),
  Literal('OFF'),
);

export const EnvRunType = Record({
  DATABASE_URL: String,
  JWT_SECRET: String,
  SESSION_SECRET: String,
  REDIS_URL: String,
  GOOGLE_CLIENT: String,
  GOOGLE_SECRET: String,
  GOOGLE_CALLBACK_URL: String,
  GLOBAL_PREFIX: String,
  RATE_LIMIT: String,
github klemola / foobar2000-web-ui / server / Models.ts View on Github external
import { Socket } from 'net'
import { ChildProcessWithoutNullStreams } from 'child_process'

import { Logger } from './Logger'

export const Env = Union(
    Literal('production'),
    Literal('development'),
    Literal('test')
)

export type Env = Static<typeof Env>

export const StatusType = Union(
    Literal('playing'),
    Literal('stopped'),
    Literal('paused'),
    Literal('volumeChange'),
    Literal('info'),
    Literal('unknown')
)

export type StatusType = Static<typeof StatusType>

export const PlaybackAction = Union(
    Literal('play'),
    Literal('pause'),
    Literal('stop'),
    Literal('prev'),
    Literal('next'),
    Literal('rand')
)
github jmcdo29 / zeldaPlay / apps / api / src / app / config / model / env.model.ts View on Github external
import { Literal, Record, Static, String, Undefined, Union } from 'runtypes';

const NodeEnv = Union(
  Literal('prod'),
  Literal('production'),
  Literal('dev'),
  Literal('development'),
  Literal('test'),
);

const LogLevel = Union(
  Literal('INFO'),
  Literal('FATAL'),
  Literal('DEBUG'),
  Literal('ERROR'),
  Literal('WARN'),
  Literal('FINE'),
  Literal('OFF'),
);

export const EnvRunType = Record({
  DATABASE_URL: String,
  JWT_SECRET: String,
  SESSION_SECRET: String,
github jmcdo29 / zeldaPlay / apps / api / src / app / config / model / env.model.ts View on Github external
import { Literal, Record, Static, String, Undefined, Union } from 'runtypes';

const NodeEnv = Union(
  Literal('prod'),
  Literal('production'),
  Literal('dev'),
  Literal('development'),
  Literal('test'),
);

const LogLevel = Union(
  Literal('INFO'),
  Literal('FATAL'),
  Literal('DEBUG'),
  Literal('ERROR'),
  Literal('WARN'),
  Literal('FINE'),
  Literal('OFF'),
);

export const EnvRunType = Record({
  DATABASE_URL: String,
  JWT_SECRET: String,
  SESSION_SECRET: String,
  REDIS_URL: String,
  GOOGLE_CLIENT: String,
  GOOGLE_SECRET: String,
  GOOGLE_CALLBACK_URL: String,
github klemola / foobar2000-web-ui / server / Models.ts View on Github external
export const StatusType = Union(
    Literal('playing'),
    Literal('stopped'),
    Literal('paused'),
    Literal('volumeChange'),
    Literal('info'),
    Literal('unknown')
)

export type StatusType = Static<typeof StatusType>

export const PlaybackAction = Union(
    Literal('play'),
    Literal('pause'),
    Literal('stop'),
    Literal('prev'),
    Literal('next'),
    Literal('rand')
)

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

export const VolumeAction = Union(
    Literal('vol mute'),
github typeetfunc / runtypes-generate / src / index.spec.ts View on Github external
mass: Number,
    })

    const Planet = Record({
        type: Literal('planet'),
        location: Vector,
        mass: Number,
        population: Number,
        habitable: Boolean,
    })

    const Rank = Union(
        Literal('captain'),
        Literal('first mate'),
        Literal('officer'),
        Literal('ensign'),
    )

    const CrewMember = Record({
        name: String,
        age: Number,
        rank: Rank,
        home: Planet,
    })

    const Ship = Record({
        type: Literal('ship'),
        location: Vector,
        mass: Number,
        name: String,
        crew: Array(CrewMember),
    })