wee-schema
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

Wee Schema

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)

Package Sidebar

Install

npm i wee-schema

Weekly Downloads

4

Version

0.0.4

License

MIT

Unpacked Size

20 kB

Total Files

12

Last publish

Collaborators

  • patrixr