# basic-validator

> Validator creator to add rules to validation

Latest version **2.0.0** (published 2019-05-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install basic-validator
pnpm add basic-validator
yarn add basic-validator
bun add basic-validator
```

## 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 | 2.0.0 |
| Published | 2019-05-09 |
| First published | 2019-01-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Nathan Green |
| Maintainers | nath-green |
| Keywords | js, npm, module, validation, validator, javascript |

## Links

- npm: https://www.npmjs.com/package/basic-validator
- Repository: https://github.com/nath-green/basic-validator
- Homepage: https://github.com/nath-green/basic-validator#readme
- npm.io page: https://npm.io/package/basic-validator

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2019-05-09
- 1.0.0 — 2019-01-31

## README

## basic-validator

### Installation

**`yarn add basic-validator`** or `npm install basic-validator`

### Usage

#### Import

Import or require **`Validator`** to be consumed:

```js
import { Validator } from 'basic-validator';
```

#### Create Validator

Once imported, instantiate with:

```js
const password = new Validator();
```

#### Validator methods

| method                                                    | argument              | description                                                                                           |
| --------------------------------------------------------- | --------------------- | ----------------------------------------------------------------------------------------------------- |
| [`rule()`](#user-content-adding-rules-with-ruleobj)       | Object `{}`           | Add a validation rule                                                                                 |
| [`validate()`](#user-content-validating-with-validateval) | Value to be validated | Loop over all validation rules, returning an object containing `valid` (boolean) and `errors` (array) |

#### Adding rules with `.rule({...obj})`

To add a rule to a validator, call `rule({})` on the Validator instance, passing the following object `{}`:

| param     | required | type     | description                                      |
| --------- | -------- | -------- | ------------------------------------------------ |
| `fn`      | yes      | Function | Function to be called when validating            |
| `message` | yes      | String   | Message to be added to errors array upon failure |
| `params`  | no       | Array    | Parameters (in order) to be passed to function   |

##### ⭐ Rule expectations

- The function (`fn`) passed to the rule must return `true` or `false`. These can be custom validation functions or imported from many of the validation libraries.
- The function (`fn`) must accept the testing value as its first parameter.
- If parameters (`params`) are passed to the rule, they must be in order as per the functions documentation.

Example usage:

```js
password.rule({
  fn: containsSpecialChar,
  message: 'Must contain special character'
});
```

```js
password.rule({
  fn: charsBetween,
  message: 'Characters must be between 10 and 15',
  params: [10, 15]
});
```

Rules can also be chained:

```js
password
  .rule({
    fn: charsBetween,
    params: [10, 15],
    message: 'Chars need to be between 10 and 15'
  })
  .rule({
    fn: containsUnderscore,
    message: 'Must contain an underscore'
  })
  .rule({
    fn: containsDash,
    message: 'Must contain a dash'
  });
```

#### Validating with `.validate(val)`

Once the `Validator` has been created and rules added, running validation can be called with `.validate(val)` - passing the value to be validated. The validate function will return an object containing two keys; `valid` and `errors`.

The key `valid` will be a boolean, `errors` will be an array of messages defined from the rules.

For example:

```js
password.validate('hello');
// {
//   valid: false,
//   errors: ['Must contain an underscore', 'Must contain a dash']
// }
password.validate('pass-word_here');
// {
//   valid: true,
//   errors: []
// }
```

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