Provides validateData(data, Class), which throws a TypeError if the runtime types of any properties
annotated with @validate() do not match their Typescript types
Version 3 is a complete rewrite in typescript. Use @validate() decorators to automatically tag class members that should be validated at runtime, using reflected type metadata.
import{validate}from'fresh-validation';classA{
@validate()count: number;
@validate()// deep typechecking on decorated classesoptions: Position;
@validate()//only validates if add is a function (no parameter checking)add: (a: number,b: number)=>number;
@validate(true)// optionalname?: string;
@validate(false,string)// array element type must be passednames: string[];
@validate(false,number,true)// array elements may be unefined/nullsums: number[];}classPosition{
@validate()x: number;
@validate()y: number;}consta: A=validateData({count: 10,},A);Uses emitDecoratorMetadata to provide Typescript type information at runtime
Caveats
- limited type information
- additional type data must be passed to @validate for some types.
- optional types
- arrays
- unions
- discriminated unions
npminstallfresh-validationyarn install
yarn build
yarn test
yarn docs