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

ir-rh-validation

v1.1.0

Published

validation object, class

Downloads

6

Readme

ir-rh-validation

installation

npm install ir-rh-validation

Usage

import class and operators

import Validation from "ir-rh-validation";
import { isEmail } from "ir-rh-validation/OperatorValidation";

const validation = new Validation();
const user = {
  id: 1,
  name: "amir",
  email: "[email protected]",
  code: "1234",
  created_at: "9/15/2021, 5:30:00 AM",
  colorSelect: ["red", "red", "green"],
  history: [
    { id: 1, site: "example.com", timeSecond: 100, urlClick: ["url1"] },
    { id: 2, site: "example1.com", timeSecond: 5000, urlClick: ["url2"] },
    { id: 3, site: "example2.com", timeSecond: 20000, urlClick: ["url3"] },
  ],
};

validation
  .validatePromise(user, [
    {
      name: "email",
      matches: [isEmail()], //true
    },
  ])
  .then(
    () => {
      //true
    },
    (err) => {
      //false
    }
  );

example

regex

check regex on String

import { regex } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "code",
    matches: [regex(/^[0-9]*$/g)], //true
  },
]);

hasIn

check that object in values is exists

import { hasIn } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "name",
    matches: [hasIn("amir", "ali")], //true
  },
]);

hasNotIn

check that object in values is not exists

import { hasNotIn } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "name",
    matches: [hasNotIn("amir", "ali")], //false
  },
]);

rangeDate

check that date has between two date

import { rangeDate } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "created_at",
    matches: [rangeDate(1630458000000, 1632963600000)], //true
  },
]);

minLength

check min length of string or array

import { minLength } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "email",
    matches: [minLength(5)], //true
  },
]);

maxLength

check max length of string or array

import { maxLength } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "email",
    matches: [maxLength(10)], //false
  },
]);

minNum

check min number

import { minNum } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "id",
    matches: [minNum(2)], //false
  },
]);

maxNum

check max number

import { maxNum } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "id",
    matches: [maxNum(2)], //true
  },
]);

rangeNum

check number in range

import { rangeNum } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "id",
    matches: [rangeNum(0, 1)], //true
  },
]);

unique

unique item in sorted array

import { unique } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "colorSelect.*",
    matches: [unique()], //false
  },
]);

check property in array

import { rangeNum, hasNotIn } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "history.*.timeSecond",
    matches: [rangeNum(100, 100000)], //true
  },
  {
    name: "history.*.urlClick.*",
    matches: [hasNotIn("url4")], //true
  },
  {
    name: "history.*.site",
    matches: [hasNotIn("example2.com")], //false
  },
]); /// result false

customize method

import { OperatorValidation } from "ir-rh-validation";

/**
 *
 * @returns {OperatorValidation<Array<String>>}
 */
const hasColor = (color) =>
  new OperatorValidation(
    (obj) => {
      return obj.includes(color);
    },
    "hasColor",
    { color }
  );

validation.validate(user, [
  {
    name: "colorSelect",
    matches: [hasColor("green")], //true
  },
]);

Customize Message

const validation = new Validation("fa", {
  validations: {
    string: {
      max: "طول متن :attribute باید کمتر از :length باشد.",
    },
  },
  attributes: {
    history: {
      "*": {
        id: "شماره یکتا",
        site: "هر سایت",
      },
    },
    colorSelect: "رنگ انتخابی",
  },
});

if (
  validation.validate(user, [
    {
      name: "history.*.site",
      matches: [maxLength(5)], //false
    },
  ]) == false
) {
  console.log(validation.getMessageError());
}

if (
  validation.validate(user, [
    {
      name: "colorSelect",
      matches: [hasIn("yellow")], //false
    },
  ]) == false
) {
  console.log(validation.getMessageError());
}

output 1:

طول متن هر سایت باید کمتر از 5 باشد.

output 2:

مقدار رنگ انتخابی باید شامل (yellow) باشد.

usage on class

import Validation from "ir-rh-validation";
import {
  isString,
  maxLength,
  minLength,
} from "ir-rh-validation/OperatorValidation";

export default class UserModel extends Validation {
  name = "amir";

  /**
   * @type {Array<import("ir-rh-validation").Rule<UserModel>>}
   */
  rules = [
    {
      name: "name",
      matches: [isString(), minLength(1)],
    },
  ];

  /**
   * @type {Array<import("ir-rh-validation").Rule<UserModel>>}
   */
  rules1 = [
    {
      name: "name",
      matches: [isString(), maxLength(3)],
    },
  ];
}
import {
  exists,
  maxLength,
  minLength,
} from "ir-rh-validation/OperatorValidation";

const user = new UserModel();
user.validateClass(); //true

user.validateClass("rules1"); //false

user.validateClass([
  {
    name: "email",
    matches: [exists()], //false
  },
]);

String Rules

validation.validate(user, ["email:isEmail,maxLength 16"]); //true

methods