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

input-validation-util

v0.2.0

Published

Use declarative rules to validate and transform input value

Downloads

3

Readme

input-validation-util

A library to validate and transform input value using declarative rules

Installation

npm i input-validation-util

Usage

Import,

import {createInputValidator} from 'input-validation-util';

Example 1: using correct input

Define rules,

const validate = createInputValidator([
    {
        ruleName: 'is / can be number',
        test: value => !isNaN(value),
        transformValue: value => Number(value)
    },
    {
        ruleName: 'round number',
        transformValue: (value, transformedValue) =>
            Math.round(transformedValue)
    },
    {
        ruleName: 'meet minimum value',
        test: (value, transformedValue) => transformedValue >= 10,
        resetInvalidInput: (value, transformedValue) => `Reset to 10`,
        errorMessage: '5 is less than 10'
    }
]);
  • ruleName is arbitrary and is the only compulsory option
  • test callback
    • returns a boolean based on your custom validation logic
    • returns true for valid input
    • first failed rule will be available in the validation result as firstFailedRule
    • all failed rules will be available in the validation result as allFailedRules
  • transformValue callback
    • returns a value using your custom transformation logic
    • the transformed value will be available as the second parameter in test, transformValue and resetInvalidInput callbacks of subsequent rules
    • it is run in all conditions when
      • test callback returns true
      • test callback is not provided
      • except: test callback returns false
    • transformed value returned from the last rule will be available in the validation result as transformedValue
  • resetInvalidInput callback
    • returns a custom value
    • it is run only when test callback returns false
    • value returned from the first failed rule will be available in the validation result as resetInputValue
    • it is named as such because it is intended to provide a value to reset invalid input
  • errorMessage
    • custom error message string

Validate input,

const result = validate('20.5');

Use the validation result,

if (result.valid) {
    // result.inputValue === '20.5'
    // result.transformedValue === 21
    // result.firstFailedRule === null
    // result.allFailedRules.length === 0
    // result.resetInputValue === null
}

Example 2: using incorrect input

Validate input,

const result = validate(5);

Use the validation result,

if (!result.valid) {
    // result.inputValue === 5
    // result.transformedValue === 5
    // result.firstFailedRule.ruleName === 'meet minimum value'
    // result.allFailedRules.length === 1
    // result.resetInputValue === 'Reset to 10'
	// result.firstFailedRule.errorMessage === '5 is less than 10'
}

API Reference

  • createInputValidator(rules: Rule | Rule[]): (value: any) => Result

  • Rule

interface Rule {
    test?: (value: any, transformedValue: any) => boolean;
    resetInvalidInput?: (value: any, transformedValue: any) => any;
    transformValue?: (value: any, transformedValue: any) => any;
    ruleName: string;
    errorMessage?: string;
}
  • Result
interface Result {
    firstFailedRule: Rule | null;
    allFailedRules: Rule[];
    valid: boolean;
    inputValue: any;
    transformedValue: any;
    resetInputValue: any;
}