# @euriklis/validator

> A javascript package for testing and managing of type coonditions.

Latest version **4.2.1** (published 2023-11-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @euriklis/validator
pnpm add @euriklis/validator
yarn add @euriklis/validator
bun add @euriklis/validator
```

Provides the command `validator`.

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.2.1 |
| Published | 2023-11-21 |
| First published | 2021-01-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 235.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Euriklis |
| Maintainers | euriklis |
| Keywords | validator, javascript, "type, verification", "conditional, type, manipulation", testing |

## Links

- npm: https://www.npmjs.com/package/@euriklis/validator
- Repository: https://github.com/VelislavKarastoychev/euriklis-validator
- Homepage: https://github.com/VelislavKarastoychev/euriklis-validator#readme
- Issues: https://github.com/VelislavKarastoychev/euriklis-validator/issues
- npm.io page: https://npm.io/package/@euriklis/validator

## Dependencies (1)

- [@euriklis/message](https://npm.io/package/@euriklis/message.md) ^2.1.2

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 4.2.1 (latest) — 2023-11-21
- 4.2.0 — 2023-11-21
- 4.1.0 — 2023-11-10
- 4.0.1 — 2023-11-06
- 4.0.0 — 2023-11-06
- 3.1.0 — 2023-01-20
- 3.0.0 — 2022-11-28
- 2.1.4 — 2022-05-01
- 2.1.3 — 2022-04-27
- 2.1.2 — 2022-04-27
- 2.1.1 — 2022-04-27
- 2.1.0 — 2022-04-10
- 2.0.0 — 2022-02-04
- 1.2.2 — 2021-06-22
- 1.2.1 — 2021-06-22
- … 12 more at https://npm.io/package/@euriklis/validator/versions

## README

# @euriklis/validator

A versatile JavaScript library for conditional analysis, testing, and validation of JavaScript types.

## Installation

You can install the `@euriklis/validator` package using npm:

```sh
npm install @euriklis/validator
```
Alternatively, you can install a specific version using the --save-exact flag:

```sh
npm install @euriklis/validator@<version> --save-exact
```
This command will add the package to your node_modules folder.

# Usage

The **@euriklis/validator** library is designed for conditional testing, allowing you to create complex compositions of checking criteria and obtain the logical results of these tests. For example, if you have a user, a registration protocol, and a database, you can use the validator to test if the registration criteria are fulfilled:

```js
const user = {
    username: 'Harris',
    password: 'k12d87dc3A!43d',
    email: 'example@mail.com',
    age: 22
};

const users = DB.getUsers();
const has_payment = true;

new validator(user)
    .interface({
        username: usr => usr.isString.and.hasLengthEqualsOrGreaterThan(6),
        password: psw => psw.isString.and.hasLengthGreaterThan(6),
        email: mail => mail.isString.and.isEmail,
        age: age => age.isEqualOrGreaterThan(18)
    })
    .and.bind(new validator(user.not.isSameWithAny(users)))
    .and.bind(new validator(has_payment).isSame(true))
    .on(true, () => makeRegistration(user))
    .on(false, () => requireInfo(users, user, has_payment));

```

Starting from version 2.0.0, we've introduced getter methods for methods that don't require arguments (excluding copy() and absoluteCopy). These getter methods provide a more intuitive way to access various validation checks and conditions.

For example, you can use methods like isInteger, isArray, isNumber, isString, isObject, isUndefined, isEmpty, and more without parentheses. Additionally, the operators and, or, and not should now be written with lowercase letters (from version 4.0.0), while all methods follow the camel case convention. For example:

```js
import validator from '@euriklis/validator';

const array = [1, 2, 3, 4, 5, 8, 12];

new validator(array)
    .isIntegerArray
    .or.isStringArray
    .on(true, () => console.log('The array contains only integers or strings.'));
```
The library has been updated since version 3.0.0 with private methods for operands and the *not* property. We use again the **@euriklis/message** package (from version 4.0.0) for the test method, and new methods have been introduced, including isIntegerArray, isObjectArray, hasLengthGreater/LessThan(), hasLengthEqualsOrGreater/LessThan(), hasLengthInRange(), hasLengthInClosedRange(), isPositiveInteger, isNegativeInteger, isPositive, isNegative, isConvertibleToNumber (instead of the old version isNumberLike) and many others. From version 4.0.0 the **contains** and **interface** methods was deleted. The **interface2** method is now used as **interface** method.

Because of the fact that the package uses private methods, please note that private methods require Node.js version 12.0.0 or higher, Chrome version 74 or higher, and Firefox version 90 or higher. 

# Methods

The most of the methods of the **@euriklis/validator** return a validator instance, allowing you to chain methods. The result of the comparison (the answer) and the condition fulfillment are recorded in the "answer" property. Here's an example:

```js
import validator from '@euriklis/validator';

let a = 5, b = 12, c = [11, 13];

let result = new validator(a)
    .isInteger
    .and.isLessThan(6).or.isInClosedRange(5, 6)
    .and.bind(
        new validator(b).isInteger.and
            .isGreaterThan(10)
    )
    .and.bind(
        new validator(c).isNumberArray
            .and.not.forAny(number => {
                return number.isFloat();
            })
    )
    .answer;

console.log(result); // true

```

# Bugs and Tips

If you encounter any issues or have tips to share, please feel free to contact us. You can reach us via email at exel_mmm@abv.bg or euriklis@hotmail.com or to publish your suggestion in the issues section of the github repo of the package.

# License

The **@euriklis/validator** package is released under the MIT License. It is provided for free for personal and non-commercial use. The author of the package is not liable for errors in third-party software, libraries, packages, or source code used in conjunction with this library.

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