# @sidewinder/validator

> Sidewinder Validator

Latest version **0.14.0** (published 2024-06-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sidewinder/validator
pnpm add @sidewinder/validator
yarn add @sidewinder/validator
bun add @sidewinder/validator
```

## Health

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

Positive: has types; no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.14.0 |
| Published | 2024-06-26 |
| First published | 2022-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 17.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 68 |
| Author | sinclairzx81 |

## Links

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

## Dependencies (1)

- [@sidewinder/type](https://npm.io/package/@sidewinder/type.md) 0.14.0

## Recent versions

- 0.14.0 (latest) — 2024-06-26
- 0.13.1 — 2024-06-05
- 0.13.0 — 2024-05-20
- 0.12.15 — 2023-08-07
- 0.12.14 — 2023-07-16
- 0.12.12 — 2023-07-14
- 0.12.11 — 2023-07-13
- 0.12.10 — 2023-07-12
- 0.12.9 — 2023-07-11
- 0.12.8 — 2023-07-03
- 0.12.7 — 2023-06-21
- 0.12.6 — 2023-06-17
- 0.12.5 — 2023-01-18
- 0.12.4 — 2022-11-28
- 0.12.3 — 2022-11-28
- … 85 more at https://npm.io/package/@sidewinder/validator/versions

## README

<div align='center'>

<h1>Sidewinder Validator</h1>

<p>Validation of Sidewinder Types</p>

[<img src="https://img.shields.io/npm/v/@sidewinder/validator?label=%40sidewinder%2Fvalidator">](https://www.npmjs.com/package/@sidewinder/validator)

</div>

## Overview

This package provides JSON schema validation for the `@sidewinder/type` package. It is built upon Ajv and provides additional validation support for `Uint8Array` as well as `void` used in Sidewinder Contracts.

License MIT

## Contents

- [Overview](#Overview)
- [Install](#Install)
- [Example](#Example)
- [Assert](#Assert)
- [Check](#Check)
- [Reference Types](#Reference-Types)

## Install

```bash
$ npm install @sidewinder/validation
```

## Example

The following shows general usual

```typescript
import { Validator } from '@sidewinder/validation'
import { Type } from '@sidewinder/type'

const T = Type.Object({
  a: Type.String(),
  b: Type.Number(),
  c: Type.Boolean(),
  d: Type.Uint8Array(),
  e: Type.Void(),
})

const validator = new Validator(T)

validator.assert({
  a: 'foo',
  b: 1,
  c: true,
  d: new Uint8Array(),
  e: null,
})
```

## Assert

The assert function will check the given data and throws with a `ValidatorError` if the data fails to check.

```typescript
import { Validator, ValidatorError } from '@sidewinder/validation'

const validator = new Validator(T)

try {
  validator.assert({
    a: 'foo',
    b: 1,
    c: true,
    d: new Uint8Array(),
    e: null,
  })
} catch (error) {
  if (error instanceof ValidatorError) {
    console.log(error.errors)
    console.log(error.message)
  }
}
```

## Check

The check function will check the given data and return a `ValidatorResult` object containing the result of the validation. This can be used
to test the value without throwing.

```typescript
import { Validator, ValidatorResult } from '@sidewinder/validation'

const validator = new Validator(T)

const result: ValidationResult = validator.check({
  a: 'foo',
  b: 1,
  c: true,
  d: new Uint8Array(),
  e: null,
})

if (!result.success) {
  console.log(result.errors)
  console.log(result.message)
}
```

<a name="Reference-Types"></a>

## Referenced Types

Sidewinder Validation supports schema referencing by appending the internal AJV schema compiler with additional schemas. Internally it maintains a singleton validation context that can be appended with additional schemas which allow the compiler to reference in downstream types. Because the compiler is singleton, each schema MUST have a unique `$id` across the entire application.

```typescript
import { Compiler, Validator } from '@sidewinder/validation'

// -------------------------------------------------------------------
// Referenceable Schema
// -------------------------------------------------------------------

const T = Type.Object(
  {
    a: Type.String(),
    b: Type.Number(),
    c: Type.Boolean(),
    d: Type.Uint8Array(),
    e: Type.Void(),
  },
  { $id: 'T' },
) // must be unique

Compiler.addSchema(T)

// -------------------------------------------------------------------
// Referenced Type
// -------------------------------------------------------------------

const R = Type.Ref(T)

const validator = new Validator(R)

// -------------------------------------------------------------------
// Check
// -------------------------------------------------------------------

const result = validator.check({
  a: 'foo',
  b: 1,
  c: true,
  d: new Uint8Array(),
  e: null,
})
```

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