- A simple class decorator that validates the properties of a class upon instance initialization. On validation error, the ValidationException is thrown.
- It's based on class-validator library.
- Repository can be found at github.
npm install validate-class-properties
import { ValidateProperties } from 'validate-class-properties';
import { IsNumber, Min } from 'class-validator';
@ValidateProperties
export class ValueObject {
@IsNumber({ maxDecimalPlaces: 2 })
@Min(0)
readonly value: number;
constructor(value: number) {
this.value = value;
}
}
function getInsanceOfValueObject(value: number): ValueObject {
try {
return new ValueObject(value);
} catch (e) {
if (e instanceof ValidationException) {
// handle validation error
}
throw e;
}
}