# ir-rh-validation

> validation object, class

Latest version **1.1.0** (published 2022-02-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install ir-rh-validation
pnpm add ir-rh-validation
yarn add ir-rh-validation
bun add ir-rh-validation
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-02-22 |
| First published | 2021-09-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 31.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Amir Valibeigi |
| Maintainers | amir_valibeigi |
| Keywords | Validation, validator, react, react-native, express, javascript, es6 |

## Links

- npm: https://www.npmjs.com/package/ir-rh-validation
- Repository: https://github.com/rhv79/ir-rh-validation
- Homepage: https://github.com/rhv79/ir-rh-validation#readme
- Issues: https://github.com/rhv79/ir-rh-validation/issues
- npm.io page: https://npm.io/package/ir-rh-validation

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2022-02-22
- 1.0.3 — 2021-10-02
- 1.0.2 — 2021-10-01
- 1.0.1 — 2021-09-27
- 1.0.0 — 2021-09-27

## README

# ir-rh-validation

## **installation**

```npm
npm install ir-rh-validation
```

## **Usage**

### import class and operators

```javascript
import Validation from "ir-rh-validation";
import { isEmail } from "ir-rh-validation/OperatorValidation";

const validation = new Validation();
const user = {
  id: 1,
  name: "amir",
  email: "amir@example.com",
  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

```javascript
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

```javascript
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

```javascript
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

```javascript
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

```javascript
import { minLength } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "email",
    matches: [minLength(5)], //true
  },
]);
```

### maxLength

check max length of string or array

```javascript
import { maxLength } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "email",
    matches: [maxLength(10)], //false
  },
]);
```

### minNum

check min number

```javascript
import { minNum } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "id",
    matches: [minNum(2)], //false
  },
]);
```

### maxNum

check max number

```javascript
import { maxNum } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "id",
    matches: [maxNum(2)], //true
  },
]);
```

### rangeNum

check number in range

```javascript
import { rangeNum } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "id",
    matches: [rangeNum(0, 1)], //true
  },
]);
```

### unique

unique item in sorted array

```javascript
import { unique } from "ir-rh-validation/OperatorValidation";

validation.validate(user, [
  {
    name: "colorSelect.*",
    matches: [unique()], //false
  },
]);
```

### check property in array

```javascript
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

```javascript
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

```javascript
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

```javascript
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)],
    },
  ];
}
```

```javascript
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

```javascript
validation.validate(user, ["email:isEmail,maxLength 16"]); //true
```

## methods

- object (array, number, string, ...)

  - [minLength](#minlength)
  - [maxLength](#maxlength)
  - empty
  - notEmpty
  - isArray
  - isBoolean
  - isNumber
  - isString
  - isFun
  - exists
  - notExists
  - [hasIn](#hasin)
  - [hasNotIn](#hasNotin)
  - isJson
  - isDate
  - [rangeDate](#rangedate)
  - isToday

- number

  - [rangeNum](#rangenum)
  - [minNum](#minnum)
  - [maxNum](#maxnum)

- string

  - [regex](#regex)
  - isEmail

- array
  - [unique](#unique)

---
_Source: https://npm.io/package/ir-rh-validation · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
