0.14.0 • Published 1 year ago

@sidewinder/validator v0.14.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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.1

1 year ago

0.14.0

1 year ago

0.13.0

1 year ago

0.12.10

2 years ago

0.12.11

2 years ago

0.12.12

2 years ago

0.12.14

2 years ago

0.12.15

2 years ago

0.12.8

2 years ago

0.12.9

2 years ago

0.12.7

2 years ago

0.12.6

2 years ago

0.12.5

2 years ago

0.11.0

3 years ago

0.10.14

3 years ago

0.11.1

3 years ago

0.10.15

3 years ago

0.11.2

3 years ago

0.11.3

3 years ago

0.11.4

3 years ago

0.10.10

3 years ago

0.11.5

3 years ago

0.10.11

3 years ago

0.11.6

3 years ago

0.10.12

3 years ago

0.10.13

3 years ago

0.10.9

3 years ago

0.10.1

3 years ago

0.12.0

3 years ago

0.10.2

3 years ago

0.12.1

3 years ago

0.10.3

3 years ago

0.12.2

3 years ago

0.10.4

3 years ago

0.12.3

3 years ago

0.10.5

3 years ago

0.12.4

3 years ago

0.10.6

3 years ago

0.10.7

3 years ago

0.10.8

3 years ago

0.10.0

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.8.85

3 years ago

0.8.84

3 years ago

0.8.81

3 years ago

0.8.80

3 years ago

0.8.83

3 years ago

0.8.82

3 years ago

0.8.78

3 years ago

0.8.79

3 years ago

0.9.0

3 years ago

0.8.77

3 years ago

0.8.74

3 years ago

0.8.73

3 years ago

0.8.76

3 years ago

0.8.75

3 years ago

0.8.70

3 years ago

0.8.72

3 years ago

0.8.71

3 years ago

0.8.67

3 years ago

0.8.66

3 years ago

0.8.69

3 years ago

0.8.68

3 years ago

0.8.63

3 years ago

0.8.62

3 years ago

0.8.65

3 years ago

0.8.64

3 years ago

0.8.61

3 years ago

0.8.60

3 years ago

0.8.59

3 years ago

0.8.56

3 years ago

0.8.58

3 years ago

0.8.57

3 years ago

0.8.45

3 years ago

0.8.44

3 years ago

0.8.47

3 years ago

0.8.46

3 years ago

0.8.49

3 years ago

0.8.48

3 years ago

0.8.55

3 years ago

0.8.52

3 years ago

0.8.51

3 years ago

0.8.54

3 years ago

0.8.53

3 years ago

0.8.50

3 years ago

0.8.43

3 years ago

0.8.42

3 years ago

0.8.41

3 years ago

0.8.40

3 years ago

0.8.39

3 years ago

0.8.38

3 years ago

0.8.37

3 years ago

0.8.36

3 years ago

0.8.35

3 years ago

0.8.34

3 years ago

0.8.33

3 years ago

0.8.32

3 years ago

0.8.31

3 years ago

0.8.30

3 years ago