fluid-validation
v1.60.0
Published
Chain all the things!
Downloads
17
Readme
fluid-validation
![David](https://img.shields.io/david/dev/lholznagel/fluid-validation.svg?style=flat-square&label=Dev Dependencies)
At some point we all have to validate the input of values, sometimes we need to validate it with multiple validators. This validation libary uses class chaining to provide a easy to use multi validators validation
Chain all the stuff!
Requirements
nodejs above 4
- other versions can work but are not supported
ES6 or TypeScript project
- the sources are written in TypeScript but a transpiled version gets published to npm
- for TypeScript users are declaration files generated during the transpiling
Install
$ npm install --save fluid-validation
Usage
import { Validate } from 'fluid-validation';
const myVarToValidate: string = 'ThisIsAExampleTest';
let validate: Validate = new Validate(myVarToValidate).contain('Example');
// gives back false
let result: boolean = validate.hasError();
When combining multiple chains together the are internal handled as a digital AND gate. In the following examples the digital states are handled like: 0 -> false
and 1 -> true
Chain 1 | Chain 2 | Result ------- | ------- | ------ 0 | 0 | 0 0 | 1 | 0 1 | 0 | 0 1 | 1 | 1
When you want to change this behaviour you can add a .or()
between two chains. The link between these two chains will than be handled as a digital OR gate.
Chain 1 | Chain 2 | Result ------- | ------- | ------ 0 | 0 | 0 0 | 1 | 1 1 | 0 | 1 1 | 1 | 1
Another way to break this behaviour is to use .xor()
between to chains. Like the name says it will be handled like a digital XOR gate.
Chain 1 | Chain 2 | Result ------- | ------- | ------ 0 | 0 | 0 0 | 1 | 1 1 | 0 | 1 1 | 1 | 0
For more please take a look at the examples
Contributing
Please read the contributing guidlines
API
Message(): Message
Starting point for creating a custom error message
.text(test: string): Message
Adds text for the custom message
.placeholder('variable' | 'value'): Message
These placeholder will be replaced when building the error message. Currently only the expected and the value that the validator uses for comparing are supported
.build(expected: string, value: string): string
Builds the error message and gives it back as a string. The values expected and value are used for replacing the placeholders
Validate(variable: any): Validation
Starting point for validation. Returns a instance of Validation which provides all validation methods
.alpha([customErrorMessage: Message]): Validate
Checks if the string only contains alpha letters. If you don´t like the default error message you can add a own message
.alphanumeric([customErrorMessage: Message]): Validate
Checks if the string only contains alphanumeric letters. If you don´t like the default error message you can add a own message
.ascii([customErrorMessage: Message]): Validate
Checks if the variable only contains ascii letters. If you don´t like the default error message you can add a own message
.boolean([customErrorMessage: Message, options: IBooleanOption]): Validate
Checks if the given Variable is from the type boolean. If you don´t like the default error message you can add a own message
option | description | default | type -------- | ------------------------------------ | ------- | ------- asNumber | checks for 0 and 1 | false | boolean asString | checks for true and false in strings | false | boolean asType | checks if the type is a boolean | true | boolean
.date([customErrorMessage: Message]): Validate
Checks if the given Variable is a valid date. If you don´t like the default error message you can add a own message
.decimal([customErrorMessage: Message]): Validate
Checks if the given Variable is from type decimal. If you don´t like the default error message you can add a own message
.json([customErrorMessage: Message]): Validate
Checks if the given Variable is a valid json object. If you don´t like the default error message you can add a own message
.object([customErrorMessage: Message]): Validate
Checks if the given Variable is from the type object. If you don´t like the default error message you can add a own message
.null([customErrorMessage: Message]): Validate
Checks if the given Variable is null. If you don´t like the default error message you can add a own message
.number([customErrorMessage: Message]): Validate
Checks if the given Variable is from the type number. If you don´t like the default error message you can add a own message
.string([customErrorMessage: Message]): Validate
Checks if the given Variable is from the type string. If you don´t like the default error message you can add a own message
.undefined([customErrorMessage: Message]): Validate
.after(afterDate: Date [, customErrorMessage: Message]): Validate
Checks if the given Variable date is after the value date. If you don´t like the default error message you can add a own message
.before(beforeDate: Date [, customErrorMessage: Message]): Validate
Checks if the given Variable date is before the value date. If you don´t like the default error message you can add a own message
Checks if the given Variable is undefined. If you don´t like the default error message you can add a own message
.contain(shouldContain: string [, customErrorMessage: Message]): Validate
Checks if the given variable contains a specific string. If you don´t like the default error message you can add a own message
.equal(shouldEqual: any [, customErrorMessage: Message]): Validate
Checks if the given variable is equal to the given value. If you don´t like the default error message you can add a own message
.max(maxValue [, customErrorMessage: Message]): Validate
Checks if the given Variable is under the given max value. If you don´t like the default error message you can add a own message
.min(minValue [, customErrorMessage: Message]): Validate
Checks if the given Variable is larger than the given min value. If you don´t like the default error message you can add a own message
.modulo(moduloValue [, customErrorMessage: Message]): Validate
Checks if the given Variable modulo the given value is 0. If you don´t like the default error message you can add a own message
regex(regex: RegExp [, customErrorMessage: Message]): Validate
Tries to match the given regex with the variable. If you don´t like the default error message you can add a own message
.not(): Validation
Negates the following validation method true -> false
, false -> true
.or(): Validation
Breaks the default behaviour to handle all chains as digital AND´s. When using this chain the connected chains are handled as a digital OR
.hasError(): boolean
Runs the actual validations. Gives back true if there is a error, if not it gives false back
.getErrorMessages(): string[]
Runs the actual validation. Contains the error messages