@bakerface/schema

Schemas and validation in TypeScript

Usage no npm install needed!

<script type="module">
  import bakerfaceSchema from 'https://cdn.skypack.dev/@bakerface/schema';
</script>

README

Schemas and validation in TypeScript

The purpose of this package is to ease input validation in TypeScript projects.

import * as Schema from "@bakerface/schema";

export type User = Schema.TypeOf<typeof User>;

export const User = Schema.shape({
  id: Schema.string,
  username: Schema.string,
  age: Schema.number,
  bio: Schema.optional(Schema.string),
  interests: Schema.arrayOf(Schema.string),
  permissions: Schema.recordOf(Schema.boolean),
  role: Schema.oneOf([
    Schema.constant("guest"),
    Schema.constant("moderator"),
    Schema.constant("admin"),
  ]),
});

export const parseUser = Schema.parser(User);
export const isUser = Schema.guard(User);

// some user input that needs to be validated
const user = {};

// simple type guards
if (isUser(user)) {
  console.log(user.permissions.canCreateArticle);
}

// simple parsing
const john = parseUser("john", user);

// SchemaParseError: Attempt to parse john resulted in the following errors:
// john.id: Expected the value to be a string
// john.username: Expected the value to be a string
// john.age: Expected the value to be a number
// john.interests: Expected the value to be an array
// john.permissions: Expected the value to be an object
// john.role: Expected the value to match one of the schemas

console.log(john.interests.length);