npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fluid-validation

v1.60.0

Published

Chain all the things!

Downloads

17

Readme

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 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