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

@lucania/schema

v2.0.3

Published

A schema module for compile-time and runtime type checking.

Downloads

13

Readme

Schema

| TECHNICAL | | | :-------: | -------------------------------------------------------------------------- | | noun | a representation of a plan or theory in the form of an outline or model. |

This library allows you to specify the schema for your data, and get compile time typings (help from your IDE) and runtime validation (throw errors if your data isn't in the right format).

With this library, you create objects that serve as a blueprint for what your data should look like. These objects are called Schemas.

| Schema Type | Description | Usage | Example Data | | :-------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------- | | StringSchema | Used to represent a string. | $.String(required?: boolean, default?: StringSource) | "Moaaz", "The cow jumped over the moon." | | NumberSchema | Used to represent a number. | $.Number(required?: boolean, default?: NumberSource) | -30, 0, 10 | | BooleanSchema | Used to represent a boolean. | $.Boolean(required?: boolean, default?: BooleanSource) | true, false | | DateSchema | Used to represent a Date. | $.Date(required?: boolean, default?: DateSource) | new Date(2000, 9, 29), new Date("1998-09-04") | | ObjectSchema | Used to represent an Object. | $.Object(subschema: { <key>: Schema }, required?: boolean, default?: ObjectSource) | <depends on subschema>, { name: "Jeremy", age: 23 }, { make: "Toyota", model: "Sienna", year: 2011 } | | ArraySchema | Used to represent an array | $.Array(subschema: Schema, required?: boolean, default?: ArraySource) | <depends on subschema>, [], [1, 2, 3], ["Ben", "Amit", "Dean"] | | EnumerationSchema | Used to represent an enumeration value, otherwise known as a set of possible strings values. | $.Enumeration(members: Members, required?: boolean, default?: EnumerationSource) | <depends on members>, "MAGENTA", "CA", "male" | | OrSetSchema | Used to represent a value that should be validated by one of many possible schemas. This is used to represent a value that is allowed to be multiple types. | $.OrSet(members: Members, required?: boolean, default?: OrSetSource) | <depends on members> | | DynamicObjectSchema | Used to represent an object that could have many different keys. | $.DynamicObject(subschema: Schema, required?: boolean, default?: DynamicObjectSource) | { <any key>: <depends on subschema> } | | AnySchema | Used to represent a value that could be any type. | $.Any(required?: boolean, default?: AnySource) | 1, "Omar", false, {}, [], <any type> |

Validation

The simplest possible validation:

import { $ } from "@lucania/schema";

// Create a Schema to represent a number.
const numberSchema = $.Number();

// Some data with an unknown type, likely coming from a file, network or untyped library.
const dataOfUnknownType: any = 123;

// Using our number Schema to validate that our untyped data is what we expect it to be, a number. "dataOfNumberType" now has the "number" type.
const dataOfNumberType = numberSchema.validate(dataOfUnknownType);

With this collection of Schemas, we can now start building more complex data structures. There are multiple way to represent different scenarios. This is done with the following constructs.

Creating Hierarchical Schema

These schemas can come together to allow you to define a blueprint for more complex data structures.

import { $ } from "@lucania/schema";

const WeaponSchema = $.Object({
    damage: $.Number(true).clamp(0, 100),
    forged: $.Date(false),
    affixtures: $.DynamicObject($.String())
});

const PersonSchema = $.Object({
    name: $.Object({
        first: $.String(true),
        middle: $.String(false),
        last: $.String(true)
    }),
    favoriteNumber: $.Number(true).ensure((data, pass) => data % 2 === 0, "Your favorite number must be a multiple of 2!"),
    age: $.Number(true).min(16, "You must be at least 16 years old!").max(100, "You must be at most 100 years old!"),
    weapon: WeaponSchema
});

Using Schema

With the schema definitions created, they can be used to validate data where the types are unknown. This data might come from fetch requests, information read from disk, or even JavaScript libraries that don't export their own type definitions, just to name a few examples.

import fs from "fs";

const personData = JSON.parse(fs.readFileSync("person.json", "utf8"));
const person = PersonSchema.validate(personData);

person now has the following compile-time type annotation based on PersonSchema.

At runtime, the object parsed from the person.json file will be validated to match this generated typing. If it does not match, a Schema.ValidationError will be thrown.

Additional Validation Passes

Sometimes it's necessary to validate not only a type, but also specifics about the value. I.E. a value is a $.Number and is between 0 and 100. You can add additional type-specific validation passes (I.E. .clamp(...)) or custom ones (.custom(...)):

const numberData: any = ...;

const ScoreSchema = $.Number().clamp(0, 100).custom((data, pass) => {
    pass.assert(data % 2 === 0, "Your score must be even!");
    return data;
});

// The above custom() validation pass can alternately be defined using the .ensure() shorthand.

const ScoreSchema = $.Number().clamp(0, 100).ensure((data, pass) => data % 2 === 0, "Your score must be even!");

const number: number = ScoreSchema.validate(numberData);

Custom Schema Types

You can define your own Schema types by creating a subclass of BaseSchema. But first, BaseSchema relies on 4 generic parameters for TypeScript's type checker to understand your Schema at compile time. These parameters are as follows:

  • Source - Represents the typing for a source input to your schema.
  • Model - Represents the typing for an output value from your schema.
  • Required - Represents the presence optionality of your schema's Source/Model. (Is undefined a valid Source and Model)
  • Default - Represents the typing for default values.

Typically, when developing your own Schema types, you'll hardcode Source and Model for your specific Schema's requirements, but pass over Required and Default generics to BaseSchema to allow them to be inferred from your Schema definitions.

export type CowModel = { name: string, numberOfSpots: number };

export type CowSource = string | CowModel;

export class CowSchema<
    Required extends boolean, // Taking in "Required" generic.
    Default extends DefaultValue<CowSource> // Taking in "Default" generic.

    // Handing over hardcoded Source, Model and CowSchema generics to BaseSchema.
> extends BaseSchema<CowSource, CowModel, Required, Default> { ... }

These declarations are enough to have the TypeScript type checker understand the compile-time typing for CowSchema. Next, lets implement our runtime checks.