3.0.2 • Published 3 months ago

@kalstong/fluentvalidation v3.0.2

Weekly downloads
18
License
MIT
Repository
github
Last release
3 months ago

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

3.0.2

3 months ago

3.0.1

8 months ago

3.0.0

8 months ago

2.0.0

8 months ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago