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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@minstack/schema

v1.0.3

Published

Composable [type predicates](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates) for runtime type checking.

Downloads

1

Readme

Schema

Composable type predicates for runtime type checking.

Getting Started

Import the schema namespace. Using $ is recommended for brevity.

import * as $ from './schema.js';
// Or, as an NPM package.
import * as $ from '@minstack/schema';

Construct a new custom schema from pre-existing schemas.

const isPerson = $.object({
  name: $.string(),
  age: $.number(),
});

Infer the type from the custom schema (if required).

type Person = $.SchemaType<typeof isPerson>;

Use the custom schema to narrow the type of a variable.

if (isPerson(value)) {
  // Value type is narrowed to: Person
}

Included Schemas

Simple schemas match the basic TS types.

  • string()
  • number()
  • bigint()
  • boolean()
  • symbol()
  • callable() - Match functions or constructors.
  • notDefined()
  • defined
  • nul()
  • notNul()
  • nil() - Match null or undefined.
  • notNil()
  • any() - Match anything as type any.
  • unknown() - Match anything as type unknown.

Configurable schemas accept values for matching.

  • enumeration(enumType: EnumLike)
  • literal(...primitives: Primitive[])
  • instance(...constructors: AnyConstructor[])

Composition schemas merge other schemas (or predicates) into more complex schemas.

  • union(...predicates: AnyPredicate[])
  • intersection(...predicates: AnyPredicate[])
  • object(shape: Record<string, AnyPredicate>)
  • tuple(...shape: AnyPredicate[])
  • record(type?: AnyPredicate)
  • array(type?: AnyPredicate)

Utilities which are less commonly used, or normally only used internally.

  • schema<T>(predicate: (value: unknown) => value is T)
    • Create a custom schema.
  • predicate<T>(predicate: (value: unknown) => value is T)
    • Create a predicate (copy).
  • lazy(resolve: () => AnyPredicate)
    • Delay resolving a predicate until needed (for recursive types).
  • assert(predicate: AnyPredicate, value: unknown, error?: ErrorLike)
    • Throw if the predicate does not match the value.

Custom Schema

Use the schema utility to create custom schemas with arbitrary validation logic. Creating a factory function which returns a new schema is recommended.

const isNumericString = () => {
  return $.schema<`${number}`>((value) => {
    return (
      typeof value === 'string' &&
      value.trim() !== '' &&
      !Number.isNaN(value as unknown as number);
  });
};

Use the custom schema like any other schema.

const isNumeric = $.union($.number(), isNumericString());

if (isNumeric(value)) {
  // Value type is narrowed to: number | `${number}`
}

Extension Methods

All schemas have basic extension methods.

  • .or(predicate: AnyPredicate)
    • Union.
  • .and(predicate: AnyPredicate)
    • Intersection.
  • .optional()
    • Union with undefined.

An optional string schema could be created as follows.

const isOptionalString = $.string().optional();

All collection schemas (object, tuple, record, array) have additional extension methods.

  • .partial()
    • Make all entries optional.
  • .required()
    • Make all entries required.

The array schema has an additional extension method.

  • .nonEmpty()
    • Match arrays with length > 0.

The object schema has an additional extension method.

  • .extend(shape: Record<string, AnyPredicate>)
    • Add new properties or additional constraints (intersections) on existing properties.

Type Assertions

It can be useful to throw an error when a predicate does not match a value.

$.assert($.string(), value, 'value is not a string');

After the assert, the type of value will be narrowed to the predicate type.

The above assertion is equivalent to the following conditional throw.

if (!$.string()(value)) {
  throw new TypeError('value is not a string');
}

Recursive Types

Recursive (self referential) types are possible, but require a few extra steps because type inference doesn't handle self references (Errors: TS7022, TS2454).

First, define the non-recursive part of the schema.

const isNode = $.object({ name: $.string() });

Then, derive the recursive type. This type is needed to explicitly type the recursive schema.

type Node = $.SchemaType<typeof isNode>;
type Tree = Node & { children: Tree[] };

And finally, extend the non-recursive schema to add recursion, and assign the final schema to an explicitly typed variable.

const isTree: $.ObjectSchema<Tree> = isNode.extend({
  children: $.array($.lazy(() => isTree)),
});

Wrong ways to make recursive types

A TS2454 error is raised without the $.lazy wrapper around the self reference. This is because the schema is used before it is defined.

const isTree: $.ObjectSchema<Tree> = isNode.extend({
  // Error: Variable 'isTree' is used before being assigned. ts(2454)
  children: $.array(isTree),
});

A TS7022 error is raised when defining the recursive type in a single step. This is because typescript cannot automatically infer a type which references itself.

// Error: 'isTree' implicitly has type 'any' because it does not have a
//        type annotation and is referenced directly or indirectly in its
//        own initializer. ts(7022)
const isTree = $.object({
  name: $.string(),
  children: $.array($.lazy(() => isTree)),
});