npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@gallant/schema

v0.1.4

Published

Library to handle schema validation by re-using existing Typescript interface and type declarations

Downloads

7

Readme

@gallant/schema

Library to handle schema validation by re-using existing Typescript interface and type declarations

Installation

npm install --save @gallant/schema

Usage

import * as schema from '@gallant/schema';

const bodySchema = schema.parse( `{
    username: string;
    password: string & schema.MinLen<8>;
    email: string & schema.Email;
    birthdate: Date;
}` );

const bodyErrors = bodySchema.validate({
    username: 'foo',
    password: 'bar',
    email: 'foobar.com',
    birthdate: new Date()
});

if ( bodyErrors != null ) {
    // Convert the error(s) to a string
    console.error( schema.errorsToString( bodyErrors ) );
} else {
    console.log( 'Schema validated with success.' );
}

The output of this program should be:

password.length: Expected >= 8, got 3 instead.
email: Expected email format, got invalid format instead.

This is all fine and dandy, but nothing extraordinary, because our type is just a string, which means we cannot use it to typecheck during compile-time. What would really spark joy would be to be to have a single source of truth: have the interface be usable by TypeScript at compile time for type-checking and by the schema library at runtime for validation. And we can!

First let's create a small file httpSchema.ts with the following contents:

import * as schema from '@gallant/schema';

export interface Request {
    username: string;
    password: string & schema.MinLen<8>;
    email: string & schema.Email;
    birthdate: Date;
}

Note that the interface schema we declared is totally valid TypeScript. Obviously the constraints such as schema.MinLen and schema.Email cannot be validated at compile time by TypeScript, since the values will only be known at runtime, but they do not interfere with the type declarations. And they enable us to parse the file at runtime to generate the validation code:

import * as schema from '@gallant/schema';
// Import the file so we can use it in typescript as a type
import * as HttpSchema from './httpSchema';

// Read the file (if not found, tries to look up a .ts and then a .d.ts file as with the same name)
// The explicit type is required by Typescript for using the `assert` method later on
const bodySchema: schema.Type<HttpSchema.Request> = schema.parseFileSync( __dirname, 'httpSchema' ).get( 'Request' );

// Let's declare our obj as unknown to simulate the usecase of receiving input from the user: we won't know what it is
const obj: unknown = {
    username: 'foo',
    password: 'bar',
    email: 'foobar.com',
    birthdate: new Date()
};

// Try commening this line and verify that TypeScript no longer recognizes the line 
// `obj.username` as valid
bodySchema.assert( obj );

console.log( 'Schema validated with success (an exception would have been thrown otherwise!)' );
console.log( 'Registered username ' + obj.username);