1.60.0 • Published 8 years ago

fluid-validation v1.60.0

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

fluid-validation

Travis Appveyor Codecov David(https://img.shields.io/david/dev/lholznagel/fluid-validation.svg?style=flat-square&label=Dev Dependencies) license semantic-release

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 1Chain 2Result
000
010
100
111

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 1Chain 2Result
000
011
101
111

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 1Chain 2Result
000
011
101
110

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

optiondescriptiondefaulttype
asNumberchecks for 0 and 1falseboolean
asStringchecks for true and false in stringsfalseboolean
asTypechecks if the type is a booleantrueboolean

.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

1.60.0

8 years ago

1.59.0

8 years ago

1.58.0

8 years ago

1.57.0

8 years ago

1.56.0

8 years ago

1.55.0

8 years ago

1.54.0

8 years ago

1.53.0

8 years ago

1.52.0

8 years ago

1.51.0

8 years ago

1.50.0

8 years ago

1.49.0

8 years ago

1.48.0

8 years ago

1.47.0

8 years ago

1.46.0

8 years ago

1.45.0

8 years ago

1.44.0

8 years ago

1.43.0

8 years ago

1.42.0

8 years ago

1.41.0

8 years ago

1.40.0

8 years ago

1.39.0

8 years ago

1.38.0

8 years ago

1.37.0

8 years ago

1.36.0

8 years ago

1.35.0

8 years ago

1.34.0

8 years ago

1.33.0

8 years ago

1.32.0

8 years ago

1.31.0

8 years ago

1.30.0

8 years ago

1.29.0

8 years ago

1.28.0

8 years ago

1.27.0

8 years ago

1.26.0

8 years ago

1.25.0

8 years ago

1.24.0

8 years ago

1.23.0

8 years ago

1.22.0

8 years ago

1.21.0

8 years ago

1.20.0

8 years ago

1.19.0

8 years ago

1.18.0

8 years ago

1.17.0

8 years ago

1.16.0

8 years ago

1.15.0

8 years ago

1.14.0

8 years ago

1.13.0

8 years ago

1.12.0

8 years ago

1.11.0

8 years ago

1.10.0

8 years ago

1.9.0

8 years ago

1.8.0

8 years ago

1.7.0

8 years ago

1.6.0

8 years ago

1.5.0

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago