Powerful object serialization/deserialization library.
- Easy to use
- Flexible
- Written in TypeScript
How to use
$ npm install transform-ts
or $ yarn add transform-ts
Usage
Transformer
Transformer<A,B>
has A
to B
transformation.
It also has B
to A
(inverse) transformation.
Transformation
declare declare declare
Composition
Transformer<A,B>
is composable to Tranformer<B,C>
.
declare declare
Builtin Transformers
any: Transformer<unknown, unknown>
(validate that input value is notundefined
ornull
)number: Transformer<unknown, number>
string: Transformer<unknown, string>
boolean: Tranformer<unknown, boolean>
Transformer Combinators
nullable
converts Transformer<A, B>
to Transformer<A | null, B | null>
declare
optional
converts Transformer<A, B>
to Transformer<A | undefined, B | undefined>
declare
array
converts Transformer<unknown, A>
to Transformer<unknown, A[]>
tuple
obj
You can construct complex Transformer with simple transformers and combinators. Try it out!
Example
user.json
unknown, > = $.obj
Custom Transformer
You can write your own transformer.
/*Transformer requires `A` to `B` and `B` to `A` transformation.`A` to `B` transformation is a function `(a: A) => ValidationResult<B>`.You can use `$.ok` and `$.error` to create `ValidationResult`.Returning `$.ok(value)` represents that transformation is success with `value`.Returning `$.error(error)` represents that error(s) has occured.*/
Using custom transformers, you can transform unknown
to any type you like.