https://github.com/lkwr/vade
Simple class validator for Deno using TypeScript Decorators with built-in class transformer.
https://github.com/lkwr/vade
class-transformer class-transformer-validator class-validator decorators deno denoland typescript validator
Last synced: 4 months ago
JSON representation
Simple class validator for Deno using TypeScript Decorators with built-in class transformer.
- Host: GitHub
- URL: https://github.com/lkwr/vade
- Owner: lkwr
- License: mit
- Created: 2022-04-21T20:01:15.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-23T14:24:11.000Z (about 3 years ago)
- Last Synced: 2025-02-19T00:09:28.061Z (4 months ago)
- Topics: class-transformer, class-transformer-validator, class-validator, decorators, deno, denoland, typescript, validator
- Language: TypeScript
- Homepage: https://deno.land/x/vade
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# VADE
(**Va**lidate **De**corator)
#### Simple class validator for Deno using TypeScript Decorators with built-in class transformer.




## Key Features
- Made for [Deno](https://deno.land)
- Type-safe
- Lightweight
- Extendable
- Zero dependencies## How To Use
Simply create a Class (or Model) using TypeScript Decorators and run `validate`. If you need more validator types: **write issue**, **create merge request** or **implement it yourself** and only use it locally.
```ts
import {
validate,
createValidator,
IsNumber,
IsString,
SetDefault,
} from 'https://deno.land/x/vade/mod.ts';// Custom validator
const IsHigherThen = createValidator((value: number) => {
return (prop) => {
return prop > value;
};
});// Our example model/class
class ExampleClass {
@IsNumber()
@IsHigherThen(10)
specialNumber!: number;@SetDefault('default value')
@IsString()
randomValue!: string;
}// ---------- //
// Valid
const obj1 = new ExampleClass();
obj1.specialNumber = 13;
obj1.randomValue = 'asd';
console.log(validate(obj1, ExampleClass));
// ExampleClass { specialNumber: 13, randomValue: "asd" }// ---------- //
// Invalid
const obj2 = new ExampleClass();
obj2.specialNumber = 9; // Failed
obj2.randomValue = 'asd';
console.log(validate(obj2, ExampleClass));
// null// ---------- //
// Invalid
const obj3 = new ExampleClass();
// Failed -> no specialNumber
obj3.randomValue = 'asd';
console.log(validate(obj3, ExampleClass));
// null// ---------- //
// Valid
const obj4 = { specialNumber: 14, randomValue: 'cool' };
console.log(validate(obj4, ExampleClass));
// ExampleClass { specialNumber: 14, randomValue: "cool" }// ---------- //
// Valid -> randomValue has a default value
const obj5 = { specialNumber: 14 };
console.log(validate(obj5 as any, ExampleClass));
// ExampleClass { specialNumber: 14, randomValue: "default value" }// ---------- //
// Invalid -> unknown nice property
const obj6 = { specialNumber: 14, randomValue: 'cool', nice: true };
console.log(validate(obj6, ExampleClass));
// null
```## TODO
- add more built-in types (feel free to contribute)
## Known issues
--
## Contributing
Feel free to open merge requests!
## License
MIT
---
> Homepage [luke.id](https://luke.id) · GitHub
> [@lkwr](https://github.com/lkwr) · Twitter
> [@wlkrlk](https://twitter.com/wlkrlk)