ts-runtime-check
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

Example

import 'reflect-metadata';
import { BaseValidator, Check, CheckedObject, Type, Validator } from 'ts-runtime-check';

const ColorValidator: Validator = (value, options) =>
	(typeof value === 'string' && /^#(?:[0-9a-f]{3}){1,2}$/i.test(value)) || BaseValidator(value, options);

export default class User extends CheckedObject<User> {
	@Check() id: number;
	@Check() name: string;
	@Check(Type.Array.of(ColorValidator)) colors: string[];
}

console.log(User.fromPlainObject({ id: 1, name: 'foo', colors: ['#ff0000'] }));
console.log(User.fromPlainObject({ id: 2, name: 1337 }));  // TypeError

Readme

Keywords

none

Package Sidebar

Install

npm i ts-runtime-check

Weekly Downloads

0

Version

0.0.4

License

MIT

Unpacked Size

17.4 kB

Total Files

33

Last publish

Collaborators

  • d-fischer