# object-shape-tester

> Define very fast runtime validations and types at the same time.

Latest version **6.15.3** (published 2026-11-07) · (MIT OR CC0-1.0) license · 0 weekly downloads

## Install

```sh
npm install object-shape-tester
pnpm add object-shape-tester
yarn add object-shape-tester
bun add object-shape-tester
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.15.3 |
| Published | 2026-11-07 |
| First published | 2023-04-17 |
| Weekly downloads | 0 |
| License | (MIT OR CC0-1.0) |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 3 |
| Unpacked size | 87.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | electrovir |
| Maintainers | electrovir |
| Keywords | object, shape, comparison, verification, type, runtime |

## Links

- npm: https://www.npmjs.com/package/object-shape-tester
- Repository: https://github.com/electrovir/object-shape-tester
- Issues: https://github.com/electrovir/object-shape-tester/issues
- npm.io page: https://npm.io/package/object-shape-tester

## Dependencies (3)

- [@sinclair/typebox](https://npm.io/package/@sinclair/typebox.md) ^0.34.52
- [@augment-vir/assert](https://npm.io/package/@augment-vir/assert.md) ^32.7.0
- [@augment-vir/common](https://npm.io/package/@augment-vir/common.md) ^32.7.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 6.15.3 (latest) — 2026-11-07
- 6.15.2 — 2026-10-22
- 6.15.1 — 2026-09-25
- 6.15.0 — 2026-08-15
- 6.14.1 — 2026-07-28
- 6.14.0 — 2026-06-09
- 6.13.0 — 2026-05-19
- 6.12.1 — 2026-04-22
- 6.12.0 — 2026-04-20
- 6.11.0 — 2025-12-26
- 6.10.1 — 2025-12-20
- 6.10.0 — 2025-11-22
- 6.9.3 — 2025-10-22
- 6.9.2 — 2025-10-08
- 6.9.1 — 2025-10-08
- … 70 more at https://npm.io/package/object-shape-tester/versions

## 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

```sh
npm i object-shape-tester
```

## Simple example

See this simple example for defining a shape:

<!-- example-link: src/readme-examples/simple-user-shape.example.ts -->

```TypeScript
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:

<!-- example-link: src/readme-examples/complex-user-shape.example.ts -->

```TypeScript
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](https://www.npmjs.com/package/@sinclair/typebox) package as an input for `defineShape()`.

---
_Source: https://npm.io/package/object-shape-tester · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
