# @leonardosarmentocastro/validate

> validate constraints in anything

Latest version **2.1.3** (published 2024-02-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install @leonardosarmentocastro/validate
pnpm add @leonardosarmentocastro/validate
yarn add @leonardosarmentocastro/validate
bun add @leonardosarmentocastro/validate
```

## 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.1.3 |
| Published | 2024-02-12 |
| First published | 2021-05-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| Author | leonardosarmentocastro |
| Maintainers | leonardosarmentocastro |
| Keywords | validate, constraints |

## Links

- npm: https://www.npmjs.com/package/@leonardosarmentocastro/validate
- npm.io page: https://npm.io/package/@leonardosarmentocastro/validate

## Dependencies (4)

- [zxcvbn](https://npm.io/package/zxcvbn.md) ^4.4.2
- [mongoose](https://npm.io/package/mongoose.md) 6.5.0
- [validator](https://npm.io/package/validator.md) ^13.6.0
- [brazilian-values](https://npm.io/package/brazilian-values.md) ^0.10.1

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.1.3 (latest) — 2024-02-12
- 2.0.0 — 2023-12-07
- 1.3.0 — 2022-08-29
- 1.2.0 — 2022-08-29
- 1.1.1 — 2022-08-25
- 1.1.0 — 2022-08-25
- 1.0.0 — 2022-08-04
- 0.0.2 — 2021-05-13
- 0.0.1 — 2021-05-10

## README

# validate

validate constraints (specially) in mongoose models

e.g. for mongoose models:

```js
// model.js
const mongoose = require('mongoose');
const { validate, isRequiredValidator, isTooLongValidator } = require('@leonardosarmentocastro/validate');

// define your schema
const schema = new mongoose.Schema({
  email: String,
  username: String,
});

// set your validations
schema.post('validate', async (doc, next) => {
  const constraints = [
    ...['username'].map(field => isRequiredValidator(field)),
    isTooLongValidator('username', USERNAME_MAX_LENGTH),
  ];
  const error = await validate(constraints, doc);

  return next(error);
});

// use your model for crud operations
exports.UsersModel = mongoose.model('User', schema);
```

```js
// create.js
const { translate } = require('@leonardosarmentocastro/i18n');
const UsersModel = require('./model');

const userDoc = new UsersModel({ email: '', username: 'leonardo' });
try {
  const savedUser = await userDoc.save();
  const transformedUser = userDoc.toObject();

  return transformedUser;
} catch(err) {
  const transformedUser = userDoc.toObject();
  const error = translate.error(err, 'pt-br', transformedUser);

  // returns:
  // {
  //   code: 'VALIDATOR_ERROR_FIELD_IS_REQUIRED',
  //   message: 'O campo "{field}" é mandatório.',
  // }
  return error;
}
```

### validators

### `isAlreadyInUseValidator = (field = '') => (doc = {})`


Validate wheter a field is already present in some registry for this model.
E.g. checks if user1.email ("banana@email.com") is already being used by any user.

Translation code: `VALIDATOR_ERROR_FIELD_IS_ALREADY_IN_USE`

### `isPasswordStrongValidator = (doc = {})`

Validate if `doc.password` is considered somewhat guessable.

Translation code: `VALIDATOR_ERROR_PASSWORD_NOT_STRONG`

### `isRequiredValidator = (field = '') => (doc = {})`

Validate if a field is present/not empty.

Translation code: `VALIDATOR_ERROR_FIELD_IS_REQUIRED`

### `isTooLongValidator = (field = '', maxLength = 0) => (doc = {})`

Validate if a string field has exceeded a given max length.

Translation code: `VALIDATOR_ERROR_FIELD_IS_TOO_LONG`

### `isValidEmailValidator = (doc = {})`

Validate if field "doc.email" is a valid email.

Translation code: `VALIDATOR_ERROR_EMAIL_INVALID`

## translations

its mandatory to set these translations codes to their respective files:

* `VALIDATOR_ERROR_FIELD_IS_ALREADY_IN_USE`
* `VALIDATOR_ERROR_PASSWORD_NOT_STRONG`
* `VALIDATOR_ERROR_FIELD_IS_REQUIRED`
* `VALIDATOR_ERROR_FIELD_IS_TOO_LONG`
* `VALIDATOR_ERROR_EMAIL_INVALID`

e.g.

```yml
# ./translations/en-us.yml
VALIDATOR_ERROR_FIELD_IS_ALREADY_IN_USE: This "{field}" is already in use.
VALIDATOR_ERROR_PASSWORD_NOT_STRONG: Password is not strong enough.
VALIDATOR_ERROR_FIELD_IS_REQUIRED: The field "{field}" is required.
VALIDATOR_ERROR_FIELD_IS_TOO_LONG: The field "{field}" is too long (max length is {maxLength}).
VALIDATOR_ERROR_EMAIL_INVALID: The email "{email}" is invalid.

# ./translations/pt-br.yml
VALIDATOR_ERROR_FIELD_IS_ALREADY_IN_USE: Este "{field}" já está em uso.
VALIDATOR_ERROR_PASSWORD_NOT_STRONG: Senha não é forte o bastante.
VALIDATOR_ERROR_FIELD_IS_REQUIRED: O campo "{field}" é mandatório.
VALIDATOR_ERROR_FIELD_IS_TOO_LONG: O campo "{field}" é longo demais (máximo de caracteres é {maxLength}).
VALIDATOR_ERROR_EMAIL_INVALID: O email "{email}" é inválido.
```

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