# @agentframework/validation

> Zero-configuration validation framework

Latest version **1.0.0-rc.20210814** (published 2021-08-14) · 0 weekly downloads

## Install

```sh
npm install @agentframework/validation
pnpm add @agentframework/validation
yarn add @agentframework/validation
bun add @agentframework/validation
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-rc.20210814 |
| Published | 2021-08-14 |
| First published | 2018-12-20 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=8.5.0 |
| Dependencies | 3 |
| Unpacked size | 23.7 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Ling Zhang |
| Maintainers | agentframework-admin |

## Links

- npm: https://www.npmjs.com/package/@agentframework/validation
- npm.io page: https://npm.io/package/@agentframework/validation

## Dependencies (3)

- [ajv](https://npm.io/package/ajv.md) 6.6.2
- [@attributes/common](https://npm.io/package/@attributes/common.md) 1.0.0-rc.20210814
- [@typescript-decorators/common](https://npm.io/package/@typescript-decorators/common.md) 1.0.0-rc.20210814

## Recent versions

- 1.0.0-rc.20210814 (latest) — 2021-08-14
- 0.15.5 — 2021-04-08
- 0.15.3 — 2021-04-08
- 0.15.1 — 2021-01-21
- 0.13.31 — 2020-03-16
- 0.13.28 — 2020-03-05
- 0.13.26 — 2020-03-02
- 0.13.23 — 2020-02-28
- 0.13.17 — 2020-02-25
- 0.13.16 — 2020-02-25
- 0.13.13 — 2020-02-18
- 0.13.12 — 2020-02-18
- 0.13.10 — 2019-12-19
- 0.13.9 — 2019-11-21
- 0.13.7 — 2019-11-20
- … 31 more at https://npm.io/package/@agentframework/validation/versions

## README

# @agentframework/validation

### What is it?

A zero-configuration validation framework [Demo](https://github.com/agentframework/validation-example)

### Support Types

- boolean
- number
- string
- null
- Nested class

### How it works?

This framework will analysis your class and create a validator using `ajv` for you.

### Steps

1. Use class to define your data and mark @mandatory or @optional for the field.

2. Create a Validator `const validator = Validate(YOUR_CLASS)`

3. Validate object using `validator.test({ id: 1, name: 1 })`

### API

#### Validate

- function Validate(yourClassType: any): Validator - Create a validator, this validator can be reuse.

- IValidator.test(obj: any): boolean - validate the object, return true if the object match the Class definition.
- IValidator.validate(obj: any): Array<ValidateError> - validate the object, return validation errors, if object match, return empty array[]

```ts
// Data Models
class ProjectModel {
  @mandatory()
  name: string
}

class DataModel {
  owner: string
  project: ProjectModel
}


// User input
const body = {
                owner: 'Peter',
                project: {
                   name: 1
                }
             }

// Validation Result
const validator = Validate(DataModel)
const results = validator.validate(body)

/*
 results = [
   {
     dataPath: '.project.name',
     params: { type: 'string' },
     message: 'should be string'
   }
 ]
*/
```

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