How to use the runtypes.Number.withConstraint 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 Hypercubed / dynamo / tests / types-runtypes.spec.ts View on Github external
constraint['name'] = name ? `${name} ${constraint['tag']}` : constraint['tag'];
  guard()(constraint, 'guard');
  return constraint as any;
}

const dynamo = new Dynamo();

const nameConstraint = StringType.withConstraint(x => {
  return x.length > 0;
});

// tslint:disable-next-line:variable-name
const Name = convertConstraint(nameConstraint);
type Name = InstanceType;

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

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

const personRecord = Record({
  name: nameConstraint,
  age: ageConstraint,
  kind: Literal('$person')
});
// tslint:disable-next-line:variable-name
const Person = convertConstraint(personRecord);
type Person = Static;
github pelotom / runtypes / examples / src / space-game.ts View on Github external
import {
  Boolean,
  Number,
  String,
  Literal,
  Array,
  Tuple,
  Record,
  Dictionary,
  Union,
  Static,
  match,
} from 'runtypes';

const NonNegative = Number.withConstraint(n => n >= 0);
type NonNegative = Static; // = number

const Vector = Tuple(Number, Number, Number);
type Vector = Static; // = [number, number, number]

const Asteroid = Record({
  type: Literal('asteroid'),
  location: Vector,
  mass: NonNegative,
});
type Asteroid = Static<
  typeof Asteroid
>; /* = {
  type: 'asteroid';
  location: Vector;
  mass: NonNegative;