Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bakerface/schema

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bakerface/schema

Schemas and validation in TypeScript

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 10 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc