Registry / serialization / object-shape-tester

object-shape-tester

JSON →
library6.11.0jsnpmunverified

object-shape-tester is a TypeScript-first utility library designed to define, validate, and provide default values for JavaScript objects at both compile-time (via TypeScript types) and runtime. The library's current stable version is 6.11.0, with minor and patch releases occurring frequently, indicating active maintenance and development. Its core differentiator lies in generating TypeScript types, runtime validation logic, and default object values from a single, concise shape definition, reducing redundancy and ensuring consistency across an application. It supports complex type definitions through specifiers like `unionShape`, `intersectShape`, and `enumShape`, and can even integrate with `@sinclair/typebox` schemas for advanced use cases. This approach ensures that objects conform to expected structures throughout their lifecycle, suitable for both testing environments and production data processing, especially in Node.js applications (requiring Node.js >=22).

npm install object-shape-tester
INSTALL
IMPORT
SIG · OBJECT-SHAPE-TESTE
O
object-shape-tester
serializationjavascriptv6.11.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

defineShape
import { defineShape } from 'object-shape-tester';
const { defineShape } = require('object-shape-tester');
The package requires Node.js >=22 and is primarily designed for ES Modules.
assertValidShape
import { assertValidShape } from 'object-shape-tester';
import assertValidShape from 'object-shape-tester/assertValidShape';
All core functionalities are named exports from the main package entry point.
ShapeMismatchError
import { ShapeMismatchError } from 'object-shape-tester';
Used for catching and handling validation failures gracefully.
unionShape
import { unionShape } from 'object-shape-tester';
Shape specifiers like `unionShape` are also named exports.

This example demonstrates defining an object shape, deriving a TypeScript type and a default value, and then validating an object against that 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); console.log('User is valid:', myUser);
Debug
Known issues
breakingThe `value` property was removed from `ShapeMismatchError` to prevent accidental logging of sensitive information.
fix
Review error handling logic and remove any reliance on the `value` property of `ShapeMismatchError` instances.
affects: >=6.10.1
gotchaThis package requires Node.js version 22 or higher. Earlier Node.js versions are not supported.
fix
Upgrade your Node.js environment to version 22 or later. Using a Node.js version manager like `nvm` is recommended.
affects: >=6.0.0
gotchaThe library primarily uses ES Modules. Attempting to use `require()` for imports in a CommonJS context without proper configuration (e.g., `type: "module"` in `package.json` and `.mjs` file extensions) may lead to errors.
fix
Ensure your project is configured for ES Modules or use a bundler that correctly handles ES Modules for CommonJS output.
affects: >=6.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Attempting to use ES module `import` syntax in a CommonJS file or environment without `type: "module"` in `package.json`.
fix
Ensure your `package.json` contains `"type": "module"` or change your file extension to `.mjs`. Alternatively, configure your build system to transpile ES Modules.
ShapeMismatchError: Expected property 'propertyName' to be of type 'expectedType' but received type 'receivedType'.
The object being validated does not conform to the defined shape for a specific property, either in type or structure.
fix
Inspect the full `ShapeMismatchError` message for details on the path and specific mismatch. Adjust the input object to match the `defineShape` definition.
Upgrade
Version history
6.11.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
object-shape-tester — npm install object-shape-tester · libregistry