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

easy-validation-service

v1.0.5

Published

Easy Validation Service is a utility library for performing data validation tasks in Node.js applications. It provides a set of methods for validating various types of data, such as numbers, strings, emails, and objects.

Downloads

16

Readme

Easy Validation Service

Easy Validation Service is a utility library for performing data validation tasks in Node.js applications. It provides a set of methods for validating various types of data, such as numbers, strings, emails, and objects.

Installation

You can install Easy Validation Service via npm:

npm install easy-validation-service

Example Usage

const ValidationService = require("easy-validation-service");

const registerUserValidators = {
  email: (value) => ValidationService.validateEmail(value),
  name: (value) => ValidationService.validateString({ value, min: 2, max: 25 }),
  password: (value) =>
    ValidationService.validateString({
      value,
      min: 8,
      max: 16,
      noWhiteSpace: true,
    }),
  verifyPassword: (value, data) => value === data.password,
  role: {
    name: (value) =>
      ValidationService.isOneOf({ value, options: ["role1", "role2"] }),
  },
  isBusiness: (value) => ValidationService.isBoolean(value),
  address: (value) =>
    ValidationService.isNullOrUndefinedOrEmpty(value) ||
    ValidationService.validateString({ value, min: 1 }),
};

const registerUserValidatorsWithErrors = {
  email: (value) => ({
    isValid: ValidationService.validateEmail(value),
    errors: ["Please provide a valid email"],
  }),

  role: {
    name: (value) => ({
      isValid: ValidationService.isOneOf({
        value,
        options: ["role1", "role2"],
      }),
      errors: ["Please provide role1 or role2"],
    }),
  },
};

const firstUser = {
  name: "DimTzilop",
  email: "[email protected]",
  password: "12345678",
  verifyPassword: "1234567",
  role: {
    name: "role2",
  },
  address: null,
};

const secondUser = {
  name: "DimTzilop",
  email: "[email protected]",
  password: "12345678",
  verifyPassword: "12345678",
  role: {
    name: "role1",
  },
  address: null,
  isBusiness: false,
};

const thirdUser = {
  name: "DimTzilop",
  email: "[email protected]",
  password: "12345678",
  verifyPassword: "12345678",
  role: {
    name: "admin",
  },
  address: null,
  isBusiness: false,
};

console.log(ValidationService.validateBody(firstUser, registerUserValidators));
// false

console.log(ValidationService.validateBody(secondUser, registerUserValidators));
// true

console.log(ValidationService.validateBody(thirdUser, registerUserValidators));
// false

console.log(
  ValidationService.validateBodyWithErrors(
    thirdUser,
    registerUserValidatorsWithErrors
  )
);
// { isValid: false, errors: { role: { name: ["Please provide role1 or role2"] } } }

Methods

validateNumber({ value, min = -Infinity, max = Infinity })

  • Validates if a given value is a number within a specified range.

validateString({ value, min = 0, max = Infinity, noWhiteSpace = false })

  • Validates if a given value is a string within a specified length range, optionally with no white spaces.

isIntOrStringInt(value)

  • Checks if a given value is an integer or a string representing an integer.

isOneOf({ value, options = [] })

  • Checks if a given value is one of the specified options.

isEveryOf({ value, options = [] })

  • Checks if a given value is every one of the specified options.

isNumber(value)

  • Checks if a given value is a number.

isString(value)

  • Checks if a given value is a string.

isUndefined(value)

  • Checks if a given value is undefined.

isFalsy(value)

  • Checks if a given value is falsy.

isNull(value)

  • Checks if a given value is null.

isNullOrUndefined(value)

  • Checks if a given value is null or undefined.

isNullOrUndefinedOrEmpty(value)

  • Checks if a given value is null, undefined, or an empty string.

isObject(value)

  • Checks if a given value is an object.

isBoolean(value)

  • Checks if a given value is a boolean.

validateEmail(email)

  • Validates if a given value is a valid email address.

validateBody(data, validators, parentData)

  • Validates a nested object against a set of validation rules.

validateBodyWithErrors(data, validators, parentData)

  • Validates a nested object against a set of validation rules and preserves errors.

isNotEmptyArray(fieldSet)

  • Checks if a given value is a non-empty array.

validateUniqueFieldSet(fieldSet, getValue = (x) => x)

  • Validates if a given array contains unique elements based on a custom extraction function.

isArrayOfType(fieldSet, type)

  • Checks if all elements in a given array are of a specified type.

isFunction(value)

  • Checks if a given value is a function.