@kalstong/fluentvalidation

3.0.2 • Public • Published

FluentValidation.js

Inspired by the awsome FluentValdiation .NET

A node.js package for providing a model validation based on a chain of rules with a fluent syntax.

Tests

Support me

Here's the option for you to buy me a coffee - if you like my software, if you find it useful and you can, please consider this small gesture for all the hard work I've been putting into these projects.

That would mean a lot to me!

Of course, don't feel pressured if you can't, I will continue to support and create more software.

"Buy Me A Coffee"

Get Started

This is a Node.js module available through the npm registry. Installation is using the npm install command:

npm i @kalstong/fluentvalidation

Example 1 - Standard rules

import FluentValidation from '@kalstong/fluentvalidation';

const person = {
    name : 'John Doe',
    age : 'unknown'
}

const config = {
    breakOnFirstError : true // Stop at first error
}

const validation = new FluentValidation()
    .Config(config)
    .RuleFor(person.name).NotEmpty().ErrorMessage("Name cannot be empty")
    .RuleFor(person.age).IsNumber().ErrorMessage("Age must be a number")
    .errors;

console.log(validation);

// Output:
// [ { error: 'Age must be a number' } ]

Example 2 - Custom rules

import FluentValidation from '@kalstong/fluentvalidation';

const model = {
    state : 'idle'
}

const config = {
    breakOnFirstError : true // Stop at first error
}

function BeActive(data) {
    return (data === 'active');
}

const validation = new FluentValidation()
    .Config(config)
    .RuleFor(model.state).IsNotNullOrWhitespace().ErrorMessage("State cannot be empty")
    .RuleFor(model.state).Must(BeActive).ErrorMessage("State is not Active")
    .errors;

console.log(validation);

// Output:
// [ { error: 'State is not Active' } ]

Documentation

See documentation

Dependencies (0)

    Dev Dependencies (8)

    Package Sidebar

    Install

    npm i @kalstong/fluentvalidation

    Weekly Downloads

    1

    Version

    3.0.2

    License

    MIT

    Unpacked Size

    27.7 kB

    Total Files

    29

    Last publish

    Collaborators

    • kalstong