1.0.4 • Published 3 years ago

@aster-js/validation v1.0.4

Weekly downloads
13
License
ISC
Repository
github
Last release
3 years ago

@aster-js/validation Node.js Package

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);
}