0.0.4 • Published 4 years ago

wee-schema v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Wee Schema

npm.io

forthebadge forthebadge

Miniature schema validator for Typescript (with type guards)

Usage

import { weeschema, WeeSchema } from 'wee-schema'

interface MyType {
  foo: string
  bar: number | string
}

// === Create the schema

const schema : WeeSchema<MyType> = weeschema<MyType>({
  'foo': ['string'],
  'bar': ['number', 'string']
})

// === Use the schema

function receive(thing: unknown) {

  thing.foo // ❌ generates a TS error

  // --- Using a type guard

  if (schema.ok(thing)) {
    console.log(thing.foo); // ✅ compiles + intellisense
  }


  schema.assert(thing);

  console.log(thing.foo); // ✅ compiles + intellisense
}

Schema methods

  • ok Returns true if the object is of the correct type
  • assert Throws an error if the object is not of the right type
  • inspect Returns a list of error messages (if any)