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

@wyntine/verifier

v1.0.0-beta.3

Published

Input verification module

Downloads

15

Readme

Verifier

This library provides a set of verifier classes designed to validate different data types such as strings, numbers, arrays, objects, and booleans. Each verifier allows you to set specific validation criteria and perform checks on input data.

Table of Contents

Installation

You can install this library via your package manager:

npm install @wyntine/verifier
pnpm install @wyntine/verifier
yarn install @wyntine/verifier

Usage

StringVerifier

Validates input strings based on various criteria such as length, regex pattern, and expected value.

Methods

  • setLength(length: number): Sets the exact length that the string should have.
  • setMaxLength(maxLength: number): Sets the maximum length allowed for the string.
  • setMinLength(minLength: number): Sets the minimum length required for the string.
  • setRegex(regex: RegExp): Sets a regular expression pattern that the string should match.
  • setExpectedString(expectedString: string): Sets the expected value that the string should equal to.

NumberVerifier

Validates input numbers based on criteria such as value range, divisibility, and integer type.

Methods

  • setExpectedNumber(expectedNumber: number): Sets the expected number value.
  • setMinValue(minValue: number): Sets the minimum allowed value for the number.
  • setMaxValue(maxValue: number): Sets the maximum allowed value for the number.
  • setSafeIntegerOnly(safeInteger: boolean): Sets whether the number should be a safe integer.
  • setIntegerOnly(integer: boolean): Sets whether the number should be an integer only.
  • setAllowedSigns({ positive, zero, negative }: AllowedSigns): Sets which signs (positive, zero, negative) are allowed for the number.
  • setAllowedRanges(allowedRanges: NumberRange[]): Sets the allowed ranges of numbers.
  • setExcludedRanges(excludedRanges: NumberRange[]): Sets the excluded ranges of numbers.
  • setDividableNumbers(dividableBy: number[]): Sets the numbers by which the input number should be divisible.

ArrayVerifier

Validates input arrays, checking their structure, length, items, and exactness.

Methods

  • setMaxLength(maxLength: number): Sets the maximum length allowed for the array.
  • setMinLength(minLength: number): Sets the minimum length required for the array.
  • setLength(length: number): Sets the exact length that the array should have.
  • setExact(exact: boolean): Sets whether the array should match exactly.
  • addString(data?): Adds a string item validator to the array verifier.
  • addBoolean(data?): Adds a boolean item validator to the array verifier.
  • addNumber(data?): Adds a number item validator to the array verifier.
  • addArray(data?): Adds an array item validator to the array verifier.
  • addObject(data?): Adds an object item validator to the array verifier.

ObjectVerifier

Validates input objects, checking their keys, items, allowed/not allowed keys, and exactness.

Methods

  • setExact(exact: boolean): Sets whether the object should match exactly.
  • setGeneralType(verifierType, verifierData): Sets a general type validator for the object.
  • setNotAllowedKeys(keys: string[]): Sets the keys that are not allowed in the object.
  • addNotAllowedKeys(keys: string[]): Adds additional keys to the list of not allowed keys.
  • addString(key: string, data?): Adds a string item validator to the object verifier under the specified key.
  • addBoolean(key: string, data?): Adds a boolean item validator to the object verifier under the specified key.
  • addNumber(key: string, data?): Adds a number item validator to the object verifier under the specified key.
  • addArray(key: string, data?): Adds an array item validator to the object verifier under the specified key.
  • addObject(key: string, data?): Adds an object item validator to the object verifier under the specified key.

BooleanVerifier

Validates input booleans, checking if they match the expected boolean value.

Methods

  • setExpectedBoolean(expectedBoolean: boolean): Sets the expected boolean value.

Enums

  • VerifierOutputType: Represents the result of verification operations.
    • Keys: Success, Fail, Error
  • ItemTypes: Enum defining data types used in verification.
    • Keys: String, Number, Boolean, Array, Object

Functions

  • setLang(lang: "tr" | "en"): Sets the language for error messages during verification.
  • isString(input): Checks if the input is a string.
  • isNumber(input): Checks if the input is a number.
  • isBoolean(input): Checks if the input is a boolean.
  • isArray(input): Checks if the input is an array.
  • isObject(input): Checks if the input is an object.

Examples

Note: Verifier classes does not mutate itself on actions, they return a new class.

import { StringVerifier, NumberVerifier } from "@wyntine/verifier";

const stringValidator = new StringVerifier().setMaxLength(10).setRegex(/^\d+$/);

const numberValidator = new NumberVerifier()
  .setMinValue(0)
  .setMaxValue(100)
  .setDividableNumbers([2, 5]);

const stringResult = stringValidator.verify("12345");
console.log(stringResult); // Output: { status: 'success' }

const numberResult = numberValidator.verify(50);
console.log(numberResult); // Output: { status: 'success' }

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

License

This library is licensed under the MIT License.