@aster-js/validation
v1.0.4
Published
Aster validation library part of Aster js library
Downloads
15
Readme
@aster-js/validation 
Fluent validation library inspired by the fluent validation library from .NET.
Usage
We will take the following model as a sample:
type MyModel = {
readonly id: number,
readonly name: string,
readonly value: any;
};
Validator declaration
import { Validator, Validate } from "@aster-js/validation";
const myModelValidator = Validator.create<MyModel>(expect => {
expect("id").toBeNumber({ min: 0 }).orFail("id must be a number greater than 0");
expect("name").toBeString({ minLength: 5, maxLength: 20 }).orFail("name must have more than 5 chars and less than 20");
expect("value").toBeDefined().orFail("value must be defined");
});Validator usage
const model = { id: 0, name: "Joe", value: null };
const validationResult = myModelValidator.validate(model);
if(validationResult.type === "failed") {
console.debug(validationResult.errors);
}