direct-validation
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

direct-validation

A simple JavaScript/TypeScript lib to validate your staff without nesting your code.

import validate from 'direct-validation';

var myValue = null;
validate(myValue).notNull("This fail because myValue is null.");

This library is inspired by two great libraries: FluentValidation and FluentAssertions.

With direct-validation you can use a simple and direct code style to validate your variables without worry about nesting your code with if, else. Just call your validation directly.

We use the fail first design. This means that if your validation don't pass an exception are thrown immediately:

import validate from 'direct-validation';

var myValue = "foo";
validate(myValue).equals("bar", "myValue is not bar.");

Get Started

direct-validation can be installed using npm package manager.

npm install direct-validation

Example

Just import or require validate to your code scope and call validate() method in any type of value to access the validation methods.

import validate from 'direct-validation';

var myValue = "foo";
validate(myValue).notEmptyOrWhitespace().notEquals("bar").equals("foo"); // etc

All validation methods receives a message parameter to use a custom fail message.

The lib has a bunch of validations, I put the most commons. If your feel some other is missing, just send a PR to this repository, I appreciate.

I implement most validations found in FluentValidation and FluentAssertions, so you can found there what I talk about.

License

DirectValidation has adopted Apache License, so you can use this freely to all-purpose.

Readme

Keywords

none

Package Sidebar

Install

npm i direct-validation

Weekly Downloads

13

Version

1.3.1

License

ISC

Unpacked Size

17.8 kB

Total Files

6

Last publish

Collaborators

  • digovc