# objection-validator

> An Objection.js model mixin plugin that provides an alternate system for validation

Latest version **0.0.3** (published 2021-04-26) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2021-04-26 |
| First published | 2019-07-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Gency Digital |
| Maintainers | ttoohey |
| Keywords | validation, objection, knex, orm |

## Links

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

## 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

- 0.0.3 (latest) — 2021-04-26
- 0.0.2 — 2019-09-17
- 0.0.1 — 2019-07-03

## README

# validator-creator plugin for Objection.js

This is an [Objection.js](https://vincit.github.io/objection.js/) model mixin
plugin that provides an alternate system for validation.

Validation is configured using rule collections from
[validator-creator](https://github.com/ttoohey/validator-creator).

## class Validator:

Validator is a [class mixin](https://vincit.github.io/objection.js/guide/plugins.html#_3rd-party-plugins)

```js
// Person.js
import { Model } from "objection";
import { Validator } from "objection-validator";
import { required, email } from "./rules";

class Person extends Validator(Model) {
  static tablename = "person";

  static rules = {
    name: [required],
    email: [required, email]
  };

  static defaultAttributes = {
    // force 'required' rule to apply during insert() even if not provided
    name: ""
  };
}
```

## API

#### `static rules`

A [validator-creator](https://github.com/ttoohey/validator-creator) _rules collection_ that lists the validation rules to
apply to each field.

#### `static defaultAttributes`

Set default values for model attributes during an insert. This is a useful way
to ensure the attribute is validated even if not supplied.

#### `$beforeValidatorValidate(rules, json, modelOptions, queryContext)`

Before validation hook. Allows modifying the `rules` object before processing.

`json` contains the external representation of the model attributes to validate.

`modelOptions` contains update options if an update operation is being performed.
For insert operations it is null. [details](https://vincit.github.io/objection.js/api/types/#type-modeloptions)

`queryContext` contains the query context and transaction instance.
[details](https://vincit.github.io/objection.js/api/query-builder/other-methods.html#context)

Return `rules` or `undefined` to make no change.

Return a new rule collection object to alter the rules.

#### `$afterValidatorValidate(json, modelOptions, queryContext)`

After validation hook

#### `$validatorValidate(json, modelOptions, queryContext)`

Called automatically from `Model.$beforeInsert()` and `Model.$beforeUpdate()`

#### `$validatorToJson(modelOptions, queryContext)`

Allows to override the json data to validate. The default implementation
returns the model's `$toJson()` response.

## class ValidatorError

If validation fails a ValidatorError exception is thrown.

```js
import { ValidatorError } from "objection-validator";
try {
  await Person.query().insert({ name: "" });
} catch (error) {
  if (error instanceof ValidatorError) {
    // --> error.validation: [ {type: "required", prop: "title" }]
  }
}
```

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