Runtime validation for TypeScript
npm install typend---
Always wanted a runtime validation that implements TypeScript's declarations as expectations?
``ts`
import { check } from 'typend';
// Constructors
check
check
check
check
check
check
check
check
check
check
check<[string, number]>(['foo', 1337]);
check
// Literals
check<'foo'>('foo');
check<1337>(1337);
check
check
check<{ key: string }>({ key: 'foo' });
check<['foo', 1337]>(['foo', 1337]);
`ts
import { check } from 'typend';
interface Person {
firstName: string;
lastName: string;
height: number;
}
check
`
Runtime on-construction class validation?
`ts
import { expect } from 'chai';
import { define, check, UnequalValueError, PropsOf } from 'typend';
@define()
class Unicorn {
sentence: 'sparkle';
constructor(sentence: 'sparkle') {
check
this.sentence = sentence;
}
}
expect(() => new Unicorn('🦄🦄 Charrlieee! 🍌👑').to.throw(
UnequalValueError
);
`
`ts
import { define, check } from 'typend';
@define()
class MyClass {
key: string;
constructor(key: string) {
this.key = key;
}
}
const myClass = new MyClass('my-string');
expect(check
expect(check<$TypeOf
`
Custom _types_ not provided by TypeScript:
`ts
import { check, integer } from 'typend';
check
`
Reflecting interfaces or other types(classes included!) to native-a-like forms?
`ts
import { expect } from 'chai';
interface Person {
firstName: string;
lastName: string;
height: number;
getName(): string;
}
const PersonInterface = reflect
expect(PersonInterface).to.be.instanceof(Object);
expect(PersonInterface).to.be.eql({
firstName: String,
lastName: String,
height: Number,
getName: Function,
});
`
Converting interfaces or other types(classes included!) to validable forms?
`ts
import { expect } from 'chai';
import { convert, PropTypes, Interface } from 'typend';
interface Person {
firstName: string;
lastName: string;
height: number;
getName(): string;
}
const PersonInterface = convert
expect(PersonInterface).to.be.instanceof(Interface);
expect(PersonInterface).to.be.eql({
firstName: PropTypes.instanceOf(String),
lastName: PropTypes.instanceOf(String),
height: PropTypes.instanceOf(Number),
getName: PropTypes.instanceOf(Function),
});
`
This package uses [tsruntime][tsruntime] done by [Vadym Holoveichuk@goloveychuk][goloveychuk] as a base to evaluate kind's of types.
Typend is an experimental package that should be used as toolbox. Package exposes all internal components so most of them can be replaced or be used as part of building blocks for creating frameworks or other packages.
- [Node.js][nodejs] MUST be v14.0 or later
1. To use typend with your app:
`bash`
npm install typend tsruntime ttypescript reflect-metadata
or
`bash`
yarn add typend tsruntime ttypescript reflect-metadata
2. Add new tsconfig.json or change existing one:
`json`
{
"compilerOptions": {
"experimentalDecorators": true,
"plugins": [
{
"transform": "tsruntime/dist/transform/transformer.js",
"type": "program"
}
]
}
}
3. Run ttypescript with compiler:
##### [ts-node][ts-node]
Use arguments:
``
ts-node --compiler=ttypescript src/index.ts
or
``
cross-env TS_NODE_COMPILER=\"ttypescript\" ts-node --project ./tsconfig.json src/index.ts
##### [rollup.js][rollupjs]
1. Install dependencies:
`bash`
npm i --save-dev rollup rollup-plugin-commonjs rollup-plugin-filesize rollup-plugin-json rollup-plugin-node-resolve rollup-plugin-sourcemaps rollup-plugin-typescript2 rimraf cross-env
2. Add/change rollup.config.js to resemble:
`js
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import filesize from 'rollup-plugin-filesize';
import typescript from 'rollup-plugin-typescript2';
import json from 'rollup-plugin-json';
import sourceMaps from 'rollup-plugin-sourcemaps';
const env = process.env.NODE_ENV;
const pkg = require('./package.json');
export default {
input: 'src/index.ts',
output: {
file: {
es: pkg.module,
cjs: pkg.main,
}[env],
format: env,
},
external: [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
],
plugins: [
resolve(),
json(),
commonjs(),
filesize(),
typescript({
typescript: require('typescript'),
}),
sourceMaps(),
],
};
`
3. Add scripts to package.json:
`json`
{
"scripts": {
"build:cjs": "./node_modules/.bin/cross-env NODE_ENV=cjs rollup -c",
"build:es": "./node_modules/.bin/cross-env NODE_ENV=es rollup -c",
"build": "npm run clean && npm run build:es && npm run build:cjs",
"clean": "./node_modules/.bin/rimraf dist"
}
}
> If you have problems with setting up compilation environment - try using our [eveble-boilerplate][eveble-boilerplate] as a template.
Learn more about typend at [https://eveble.github.io/typend/][package-homepage] .
Please be aware that this is experimental module and using it has some drawbacks:
1. Compilation time will be increased(its less visible on more modern computer builds).
2. Making changes to referenced types will be not reflected real-time in testing environment in watch mode. Tests need to be rerunned again(this includes types defined in separate, dedicated file also).
3. Not every declaration of TypeScript generic type will work for validation.
4. We did our best to flush out most applicable architecture for this concept, however API can be subjected to change upon valid reasons.
Typend uses tsruntime for type reflection on validation with TypeScript declaration.
End-user API functions like: check, is, instanceOf, validate, isValid, isInstanceOf use exposed Typend class instance.
With this in mind - as developer you are able to replace or extend parts of the Typend that implements [Library interface][library].
Using yarn:
```
yarn test
[package-homepage]: https://eveble.github.io/typend/
[nodejs]: https://nodejs.org/
[eveble]: http://eveble.com
[ts-node]: https://github.com/TypeStrong/ts-node
[rollupjs]: https://rollupjs.org/
[mocha]: https://mochajs.org/
[eveble-boilerplate]: http://github.com/eveble/eveble-boilerplate
[tsruntime]: https://github.com/goloveychuk/tsruntime
[goloveychuk]: https://github.com/goloveychuk
[ddd]: https://eveble.github.io/typend/docs/guides/04-advanced/02-ddd
[library]: https://eveble.github.io/typend/docs/api/interfaces/types.library