Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/d-fischer/ts-runtime-check

Check the types of your external objects with (mostly) just TypeScript types.
https://github.com/d-fischer/ts-runtime-check

Last synced: about 22 hours ago
JSON representation

Check the types of your external objects with (mostly) just TypeScript types.

Awesome Lists containing this project

README

        

## Example

```ts
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 {
@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
```