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

object-shape-tester

v6.10.0

Published

Test object properties and value types.

Downloads

10,320

Readme

object-shape-tester

Create types, runtime type requirements, and runtime default values all at once.

Can be used in tests and at runtime (it does not depend on any testing libraries).

Full API docs: https://electrovir.github.io/object-shape-tester

Installation

npm i object-shape-tester

Simple example

See this simple example for defining a shape:

import {assertValidShape, defineShape} from 'object-shape-tester';

const userShape = defineShape({
    /**
     * This value is simplified to just a `string` required type, with `'empty name'` as the default
     * value.
     */
    name: 'empty name',
    /** This value is simplified to just a `number` required type, with `0` as the default value. */
    id: 0,
});

/** Access the TypeScript type with `.runtimeType`. */
export type User = typeof userShape.runtimeType;

/**
 * Access the default value with `.default`. For this shape, the default value is:
 *
 * `{name: 'empty name', id: 0}`
 */
export const emptyUser = userShape.default;

const myUser: User = {
    name: 'my name',
    id: 1000,
};

assertValidShape(myUser, userShape);

From a single object (passed into defineShape), we get a type definition, a default value, and an object which can be used for runtime object shape testing.

Complex example

For more complex types, this package provides type specifiers that you can use. The available type specifiers are currently the following:

  • or(valueA, valueB, ...): for allowing multiple types. This gets converted in TypeScript to a union.
  • and(valueA, valueB, ...): for creating a combined type. This gets converted in TypeScript to an intersection.
  • exact(valueA, valueB, ...): requires the provided value to be exactly matched. This gets converted in TypeScript to a literal const type. (Example: instead of a value of 'empty name' getting converted into a string type, exact('empty name') will only allow the value 'empty name'.)
  • enumShape(enumA): use this for enum types.
  • unknownShape(): use this to allow any type.

Here's a more complex user example that uses all of the above specifiers:

import {
    assertValidShape,
    defineShape,
    enumShape,
    exactShape,
    intersectShape,
    unionShape,
    unknownShape,
} from 'object-shape-tester';

enum AuthLevel {
    Basic = 'basic',
    Admin = 'admin',
}

const userShape = defineShape({
    firstName: 'first',
    middleInitial: unionShape('M', undefined),
    lastName: 'last',
    id: 0,
    tags: intersectShape({userTags: ['']}, {creatorTags: ['']}),
    primaryColor: unionShape(exactShape('red'), exactShape('green'), exactShape('blue')),
    authLevel: enumShape(AuthLevel),
    extraDetails: unknownShape(),
});

export type ComplexUser = typeof userShape.runtimeType;

export const emptyComplexUser = userShape.default;

const myUser: ComplexUser = {
    firstName: 'my first',
    middleInitial: undefined,
    lastName: 'last name',
    id: 1000,
    tags: {
        userTags: [],
        creatorTags: [],
    },
    primaryColor: 'blue',
    authLevel: AuthLevel.Admin,
    extraDetails: {
        whatever: 'you want',
    },
};

assertValidShape(myUser, userShape);

Additional shapes

There are many built-in shapes documented under Shapes: https://electrovir.github.io/object-shape-tester.

You can also use any schema from the @sinclair/typebox package as an input for defineShape().