0.0.2 • Published 4 years ago
Share package Some typescript decorators, like spring-validation and so on.
Setup npm i @bpframework/validationset config in tsconfig.json
"experimentalDecorators": true,
"emitDecoratorMetadata": true,Example import {NotNull, Type} from '@bpframework/validation';
class BeanA {
@NotNull
@Type.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
class BeanA {
@NotNull
a: boolean;
}
// No exception is thrown.
let obj = new BeanA();
// Will throw a exception (trigger by settter)
obj.a = null;All validate decorators have a corresponding array validation method, e.g. @Max.List use to validator array
like java spring validation Null value validator 名称 作用 例子 @Nullmust be null or undefined. @Null({message: "must is null"}) @NotNullcannot be null or undefined. @NotNull({message: "cannot be null"}) @NotBlankmust contain a non-null char trim(str).length>0 @NotBlank @NotEmptycannot be null or undefined, and the length is greater then 0 (o.size() > 0 或 o.length > 0) @NotEmpty @Size({max:number, min:number})check the length of a string or array (o.size() 或 o.length)
Boolean validator 名称 作用 例子 @AssertFalsemust be false @AssertFalse({message: "must be false"}) @AssertTruemuse be true @AssertTrue({message: "muse be true"})
Number validator 名称 作用 例子 @DecimalMax({value:number ¦ string})Number value must be less than or equal to the specified value @DecimalMax({value:1000, message: "error"}) @DecimalMin({value:number ¦ string})Number value must be greater than or equal to the specified value @DecimalMin({value:0, message: "error"}) @Max({value:number ¦ string})Integer value must be less than or equal to the specified value. @Max({value:1000, message: "error"}) @Min({value:number ¦ string})Integer value must be greater than or equal to the specified value. @Min({value:1000, message: "error"}) unsupported @Digits({integer:number, fraction:number})unsupported @Digits({integer:5, fraction:1}) @NegativeNumber value must be negative @NegativeOrZeroNumber value must be negative or zero @PositiveNumber value must be positive @PositiveOrZeroNumber value must be positive or zero @Range({min:number=0, max:number})check the range of number value
String validator 名称 作用 例子 @Email({regexp:RegExp})Whether the specified value is Email. You can specify a regular @Email({regexp:/.*/}) @Pattern({regexp:RegExp})Whether the specified value matches regular @Pattern({regexp:/.*/})
Time validator 名称 作用 例子 @FutureDate value is the future time @FutureOrPresentDate value is the future time or now @PastDate value is the past time @PastOrPresentDate value is the past time or now
Enum validator 名称 作用 @Enum({allows: v1, v2, v3, ... })Verify the value is one of the allowed values. @Type.Enum({enumType: EnumName})Verify the value is enum type.
@Type.Enum example:
import {Type} from '@bpframework/validation';
enum Enum1 {
a = '2323',
b = 'xxxx'
}
class BeanA {
@Type.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 名称 作用 @Type.BooleanVerify that the value is a Boolean. @Type.NumberVerify that the value is a Number. @Type.IntegerVerify that the value is a Integer. @Type.BigIntVerify that the value is a Big-Integer. @Type.StringVerify that the value is a String. @Type.DateVerify that the value is a Date or Date-string. @Type.ObjectVerify that the value is a Object. @Type.ArrayVerify that the value is a Array.
Custom validator 名称 作用 @Type.ValidatorCustom validator.
// validator value == 1
@Type.Validator({
checkCB(value:any) {
if (value !== 1) return false;
}
})
value: number;