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

predicate-validation

v6.1.2

Published

[![CircleCI](https://circleci.com/gh/jamiemcl001/predicate-validation/tree/master.svg?style=svg)](https://circleci.com/gh/jamiemcl001/predicate-validation/tree/master) [![Coverage Status](https://coveralls.io/repos/github/jamiemcl001/predicate-validation/

Downloads

1

Readme

Predicates Validator

CircleCI Coverage Status NPM

Allows you to chain together multiple predicate functions which, when evaluated will return a combined validation result.

API

The API for the two validators are the same, the only thing that differs is the return type from the validate function.

Object Validator

This version of the validator will return a discriminatave union object which will allow you to easily differentiate between an error and a success result. The property that will be the basis of the discriminated union is _tag, and it will be equal to either success or error.

Monoid Validator

This validator will return an Either object (as defined by fp-ts, as this is the package that is used in this project). It's return types if an error will be of type string[], which will be the list of error messages, whereas if it's a successful validation then the value will be of type unknown[]. This is because the predicates validator will simply return all of the values that were passed into the chain.

Standard Functions

getObjectValidator() => ObjectValidator

This will return an ObjectValidator that you can then chain predicate validation functions to, before validating it.

Example:

import { getObjectValidator } from 'predicate-validator';
const validationResult = getObjectValidator().validate()

getMonoidValidator() => MonoidValidator

This will return a MonoidValidator that you can then chain predicate validation functions to, before validating it.

Example:

import { getMonoidValidator } from 'predicate-validator';
const validationResult = getMonoidValidator().validate();

Validator Functions

These functions are available on a Validator instance. You can invoke them in the following way:

import { getObjectValidator } from 'predicate-validator';

const validator = getObjectValidator()
  .add("test", val => val.length > 1, "the inputted value must have at least one character")
  .add("test", val => val.length < 30, "the inputted value must not exceed 30 characters")
  .validate();

add(inputVal: unknown, predicateFn: (val: unknown) => boolean, errorMessage: string) => Validator

Parameters:

  • inputVal: unknown - this can be any value that you wish to perform validation against.
  • predicateFn: (val: unknown) => boolean - this function will be executed using the input value to determine if it should pass validation.
  • errorMessage: string - this error message will be present in the errors array if the predicate function fails.

Returns:

  • Validator - The object that this function was invoked on will be returned, to allow for chaining.

validate() => ValidationResult

Returns:

  • ValidationResult - this will either be a success or error object, depending on the type of validator you have invoked this on. If it is a MonoidValidator then you will be returned with an Either object (as defined here). If it is an ObjectValidator then you will be returned with an object that has a discriminated union property (the property is _tag). You can also use the helper functions listed below to extract the values if you don't want to do anything else with the result.

If you want to work directly with the ObjectValidator/MonoidValidator API's, instead of using a function such as getObjectValidator() or getMonoidValidator() then you can import the individual files directly:

import { createValidator, isSuccess, getResults } from 'predicate-validation/lib/objectValidator'

OR

import { createValidator, isSuccess, getResults } from 'predicate-validation/lib/monoidValidator'

Helper Functions

These helper functions will work in the same way with both the Object Validator and the Monoid Validator.

isError(ValidationResult) => boolean

This is the same API in both objectValidator and monoidValidator so you must directly import the functions from the objectValidator/monoidValidator directly to use.

Examples:

import { createValidator, isError } from 'predicate-validation/lib/objectValidator';

const input = 10;
const validationResultSuccess = createValidator()
  .add(input, val => val > 0, "The inputted value should be a positive number")
  .validate()

console.log(isError(validationResultSuccess)) // => false
import { createValidator, isError } from 'predicate-validation/lib/objectValidator';

const input = 10;
const validationResultSuccess = createValidator()
  .add(input, val => val > 20, "The inputted value should be greater than 20")
  .validate()

console.log(isError(validationResultSuccess)) // => true

isSuccess(ValidationResult) => boolean

This is the same API in both objectValidator and monoidValidator so you must directly import the functions from the objectValidator/monoidValidator directly to use.

Examples:

import { createValidator, isSuccess } from 'predicate-validation/lib/objectValidator';

const input = 10;
const validationResultSuccess = createValidator()
  .add(input, val => val > 0, "The inputted value should be a positive number")
  .validate()

console.log(isSuccess(validationResultSuccess)) // => true
import { createValidator, isSuccess } from 'predicate-validation/lib/objectValidator';

const input = 10;
const validationResultSuccess = createValidator()
  .add(input, val => val > 20, "The inputted value should be greater than 20")
  .validate()

console.log(isSuccess(validationResultSuccess)) // => false

getErrors(ValidationResult) => string[]

This is the same API in both objectValidator and monoidValidator so you must directly import the functions from the objectValidator/monoidValidator directly to use.

If the passed validationResult is a success result then you will be returned an empty array, otherwise you will be returned an array of error messages that were encountered.

Example:

import { createValidator, getErrors } from 'predicate-validation/lib/objectValidator';

const input = 10;
const validationResult = createValidator()
  .add(input, val => val > 0, "The inputted value should be a positive number")
  .add(input, val => val > 20, "The inputted value should be greater than 20") // => ERROR
  .validate();

console.log(getErrors(validationResult)); // => ["The inputted value should be greater than 20"]

getResults(ValidationResult) => unknown[]

This is the same API in both objectValidator and monoidValidator so you must directly import the functions from the objectValidator/monoidValidator directly to use.

As defined by the Either pattern, a result can only be either a success or a failure. For this reason if there are any errors among the validation then this package will not return any results (even if there are successful checks within the chain).

Example:

import { createValidator, getResults } from 'predicate-validation/lib/objectValidator';

const input = 10;
const validationResult = createValidator()
  .add(input, val => val > 0, "The inputted value should be a positive number") // => SUCCESS
  .add(input, val => val > 20, "The inputted value should be greater than 20") // => ERROR
  .validate();

console.log(getResults(validationResult)); // => []

Example:

import { createValidator, getErrors } from 'predicate-validation/lib/objectValidator';

const name = "Angela";
const age = 28;

const validationResult = createValidator()
  .add(name, val => !!val, "A valid name of at least 1 character has to be passed") // => SUCCESS
  .add(age, val => val > 18, "A user has to be at least 18 to use the service") // => SUCCESS
  .validate();

console.log(getResults(validationResult)); // => ["Angela", 28]