# last-validator

> Asynchronous validation tool

Latest version **1.4.2** (published 2022-06-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install last-validator
pnpm add last-validator
yarn add last-validator
bun add last-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 | 1.4.2 |
| Published | 2022-06-01 |
| First published | 2021-05-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 41 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Matúš Duchoň |
| Maintainers | greegus |

## Links

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

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## Recent versions

- 1.4.2 (latest) — 2022-06-01
- 2.0.0-beta.0 (beta) — 2022-09-16
- 1.4.1 — 2022-04-26
- 1.4.0 — 2022-04-26
- 1.3.2 — 2022-04-10
- 1.1.0 — 2021-11-29
- 1.0.1-beta.1 — 2021-05-14
- 1.0.1-beta.0 — 2021-05-14
- 1.0.0 — 2021-05-14

## README

# Last Validation Service

Simple and extendable asynchronous object validation tool.

## Install

```
npm i last-validator
```

```
yarn add last-validator
```

## Usage

```typescript
import { validate, isRequired, isEmail, isGreaterOrEqualThan } from 'last-validator'

const data = {
  fullName: 'Too Young',
  address: ''
  age: 12
  email: 'invalid_email_format(at)me.com'
}

const rules = {
  fullName: [ isRequired() ],
  address: [ isRequired('Address is required') ],
  age: [ isRequired(), isGreaterOrEqualThan(15, "*Sigh*, you're too young to drive this thing!") ],
  email: [ isEmail() ]
}

const { isValid, errors } = await validate(data, rules)

/*
  isValid: false
  errors: {
    address: 'Address is required',
    age: '*Sigh*, you're too young to drive this thing!',
    email: true
  }
*/
```

### Validate arrays

```typescript
import { validateAll, isRequired } from 'last-validator'

const jobPositions = [
  {
    position: '...',
    company: '...',
    startDate: '...',
    endDate: '...'
  },

  {
    position: '...',
    company: '...',
    startDate: '...',
    endDate: '...'
  },

  // ...
]

const rules = {
  position: [isRequired()],
  startDate: [isRequired()]
}

const { isValid, errors } = await validateAll(jobPositions, rules)
```

If you need to adjust the rules dynamically, you can just pass a function that will generate them


```typescript
const rules = (job) => ({
  endDate: [isLessOrEqualThan(job.startDate)]

  // ...
})
```

## Build-in validators

- `isRequired(errorMessage?: string)`
- `isNumeric(errorMessage?: string)`
- `isGreaterThan(minValue: number | Date, errorMessage?: string)`
- `isGreaterOrEqualThan(minValue: number | Date, errorMessage?: string)`
- `isLessThan(maxValue: number | Date, errorMessage?: string)`
- `isLessOrEqualThan(maxValue: number | Date, errorMessage?: string)`
- `hasMinLength(minLength: number, errorMessage?: string)`
- `hasMaxLength(maxLength: number, errorMessage?: string)`
- `isEmail(errorMessage?: string)`
- `test(condition: (value: any) => Promise<boolean> | boolean, errorMessage?: string)`

## Custom validators

```typescript
import { validate, Validator } from 'last-validator'

const isValidAddress: Validator = async ({ value, resolve, reject }) => {
  const result = await validateAddressOnBackend(value)
  result ? resolve() : reject('Invalid address')
}

const address = { ... }
const { isValid, errors } = await validate({ address }, { address: [isValidAddress] })
```

### `test` validator helper

In order to streamline usage of custom validations you can also leverage the `test` inline validator.

```typescript
const rules = {
  cake: [isRequired(), test(cake => cake.flavour === 'chocolate', 'Only chocolate cakes are accepted here')]

  // ...
}
```

### Default error messages

You can use `setDefaultErrorMessages` to set a default error message for each built-in validator.

```typescript
import { setDefaultErrorMessages } from 'last-validator'

setDefaultErrorMessages({
  isRequired: 'Field is required.',
  ...
})
```

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