@bpframework/validation
Version:
Some typescript decorators, like spring-validation and so on
174 lines (132 loc) • 7.71 kB
Markdown
Some typescript decorators, like spring-validation and so on.
- [Setup](#setup)
- [Example](#example)
- [like java spring validation](#like-java-spring-validation)
- [Null value validator](#null-value-validator)
- [Boolean validator](#boolean-validator)
- [Number validator](#number-validator)
- [String validator](#string-validator)
- [Time validator](#time-validator)
- [Enum validator](#enum-validator)
- [Type validator](#type-validator)
- [Custom validator](#custom-validator)
## Setup
```
npm i /validation
```
set config in tsconfig.json
```
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
```
## Example
```js
import {NotNull, Type} from '@bpframework/validation';
class BeanA {
.Boolean
a: boolean = null;
}
// will throw a exception:
// - febs.exception('message', febs.exception.PARAM, __filename, __line, __column);
let obj = new BeanA();
// will throw a exception
obj.a = null;
obj.a = undefined;
```
**!!!WARNING: The following code does not trigger a validity check**
```js
class BeanA {
a: boolean;
}
// No exception is thrown.
let obj = new BeanA();
// Will throw a exception (trigger by settter)
obj.a = null;
```
> **<font color=#ff0000>All validate decorators have a corresponding array validation method<br>, e.g. `.List` use to validator array</font>**
## like java spring validation
### Null value validator
| 名称 | 作用 | 例子 |
| --------------------------------- | ------------------------------------------------------------------------------------------------ | ------------------------------------- |
| `` | must be null or undefined. | |
| `` | cannot be null or undefined. | |
| `` | must contain a non-null char<br> trim(str).length>0 | |
| `` | cannot be null or undefined, and the length is greater then 0<br> (o.size() > 0 或 o.length > 0) | |
| ``({max:number, min:number}) | check the length of a string or array<br> (o.size() 或 o.length) | |
### Boolean validator
| 名称 | 作用 | 例子 |
| -------------- | ------------- | ---------------------------------------- |
| `` | must be false | |
| `` | muse be true | |
### Number validator
| 名称 | 作用 | 例子 |
| ------------------------------------------------------------ | ------------------------------------------------------------------- | ------------------------------------------- |
| ``({value:number ¦ string}) | Number value must be less than or equal to the specified value | |
| ``({value:number ¦ string}) | Number value must be greater than or equal to the specified value | |
| ``({value:number ¦ string}) | Integer value must be less than or equal to the specified value. | |
| ``({value:number ¦ string}) | Integer value must be greater than or equal to the specified value. | |
| [unsupported] ~~~~ | unsupported | |
| `` | Number value must be negative | |
| `` | Number value must be negative or zero | |
| `` | Number value must be positive | |
| `` | Number value must be positive or zero | |
| ``({min:number=0, max:number}) | check the range of number value | |
### String validator
| 名称 | 作用 | 例子 |
| --------------------------- | --------------------------------------------------------------- | ----------------------- |
| ``({regexp:RegExp}) | Whether the specified value is Email. You can specify a regular | |
| ``({regexp:RegExp}) | Whether the specified value matches regular | |
### Time validator
| 名称 | 作用 | 例子 |
| ------------------ | ---------------------------- | ------ |
| `` | Date value is the future time | |
| `` | Date value is the future time or now | |
| `` | Date value is the past time | |
| `` | Date value is the past time or now | |
## Enum validator
| 名称 | 作用 |
| ------------------------------------ | ------------------------------- |
| ``({allows: [v1, v2, v3, ...]}) | Verify the value is one of the allowed values. |
| `.Enum`({enumType: EnumName}) | Verify the value is enum type. |
`.Enum` example:
```js
import {Type} from '@bpframework/validation';
enum Enum1 {
a = '2323',
b = 'xxxx'
}
class BeanA {
.Enum({enumType: Enum1 })
a: any = null;
}
let obj = new BeanA();
obj.a = Enum1.a; // ok.
obj.a = Enum1.b; // ok.
obj.a = 1; // will throw a exception.
```
## Type validator
| 名称 | 作用 |
| --------------- | ------------------------ |
| `.Boolean` | Verify that the value is a Boolean. |
| `.Number` | Verify that the value is a Number. |
| `.Integer` | Verify that the value is a Integer. |
| `.BigInt` | Verify that the value is a Big-Integer. |
| `.String` | Verify that the value is a String. |
| `.Date` | Verify that the value is a Date or Date-string. |
| `.Object` | Verify that the value is a Object. |
| `.Array` | Verify that the value is a Array. |
## Custom validator
| 名称 | 作用 |
| ----------------- | ------------- |
| `.Validator` | Custom validator. |
```js
// validator value == 1
.Validator({
checkCB(value:any) {
if (value !== 1) return false;
}
})
value: number;
```