0.13.0 • Published 2 days ago

@sidewinder/validator v0.13.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 days ago

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

Install

$ npm install @sidewinder/validation

Example

The following shows general usual

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.

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.

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)
}

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.

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,
})
0.13.0

2 days ago

0.12.10

10 months ago

0.12.11

10 months ago

0.12.12

10 months ago

0.12.14

10 months ago

0.12.15

10 months ago

0.12.8

11 months ago

0.12.9

11 months ago

0.12.7

11 months ago

0.12.6

11 months ago

0.12.5

1 year ago

0.11.0

2 years ago

0.10.14

2 years ago

0.11.1

2 years ago

0.10.15

2 years ago

0.11.2

2 years ago

0.11.3

2 years ago

0.11.4

2 years ago

0.10.10

2 years ago

0.11.5

2 years ago

0.10.11

2 years ago

0.11.6

1 year ago

0.10.12

2 years ago

0.10.13

2 years ago

0.10.9

2 years ago

0.10.1

2 years ago

0.12.0

1 year ago

0.10.2

2 years ago

0.12.1

1 year ago

0.10.3

2 years ago

0.12.2

1 year ago

0.10.4

2 years ago

0.12.3

1 year ago

0.10.5

2 years ago

0.12.4

1 year ago

0.10.6

2 years ago

0.10.7

2 years ago

0.10.8

2 years ago

0.10.0

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.8.85

2 years ago

0.8.84

2 years ago

0.8.81

2 years ago

0.8.80

2 years ago

0.8.83

2 years ago

0.8.82

2 years ago

0.8.78

2 years ago

0.8.79

2 years ago

0.9.0

2 years ago

0.8.77

2 years ago

0.8.74

2 years ago

0.8.73

2 years ago

0.8.76

2 years ago

0.8.75

2 years ago

0.8.70

2 years ago

0.8.72

2 years ago

0.8.71

2 years ago

0.8.67

2 years ago

0.8.66

2 years ago

0.8.69

2 years ago

0.8.68

2 years ago

0.8.63

2 years ago

0.8.62

2 years ago

0.8.65

2 years ago

0.8.64

2 years ago

0.8.61

2 years ago

0.8.60

2 years ago

0.8.59

2 years ago

0.8.56

2 years ago

0.8.58

2 years ago

0.8.57

2 years ago

0.8.45

2 years ago

0.8.44

2 years ago

0.8.47

2 years ago

0.8.46

2 years ago

0.8.49

2 years ago

0.8.48

2 years ago

0.8.55

2 years ago

0.8.52

2 years ago

0.8.51

2 years ago

0.8.54

2 years ago

0.8.53

2 years ago

0.8.50

2 years ago

0.8.43

2 years ago

0.8.42

2 years ago

0.8.41

2 years ago

0.8.40

2 years ago

0.8.39

2 years ago

0.8.38

2 years ago

0.8.37

2 years ago

0.8.36

2 years ago

0.8.35

2 years ago

0.8.34

2 years ago

0.8.33

2 years ago

0.8.32

2 years ago

0.8.31

2 years ago

0.8.30

2 years ago