2.3.2 • Published 2 months ago

object-shape-tester v2.3.2

Weekly downloads
-
License
(MIT or CC0 1.0)
Repository
github
Last release
2 months ago

object-shape-tester

Create types, run-time type requirements, and run-time default values with a single definition.

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

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

Installation

npm i object-shape-tester

Usage

This package allows you to define an object's shape in a manner which can be used for testing objects at run time to make sure they conform to your defined shapes. Because it also generates TS types from your defined shape and a default run-time value from your defined shape, you will have one single source of truth for all types, defaults, and shape tests.

Simple example

See this simple example for defining a simple user type:

import {defineShape, isValidShape} from '..';

const userShapeDefinition = defineShape({
    name: 'empty name',
    id: 0,
});

export type User = typeof userShapeDefinition.runTimeType;

export const emptyUser = userShapeDefinition.defaultValue;

export function isUser(input: unknown): input is User {
    // you don't NEED to wrap isValidShape in a type guard as it is already a type guard itself
    return isValidShape(input, userShapeDefinition);
}

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

From a single object (passed into defineShape), we get a type definition, a default value, and an object which can be used for run-time 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 {and, defineShape, enumShape, exact, isValidShape, or, unknownShape} from '..';

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

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

export type ComplexUser = typeof complexUserShapeDefinition.runTimeType;

export const emptyComplexUser = complexUserShapeDefinition.defaultValue;

export function isComplexUser(input: unknown): input is ComplexUser {
    // you don't NEED to wrap isValidShape in a type guard as it is already a type guard itself
    return isValidShape(input, complexUserShapeDefinition);
}

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

2 months ago

2.3.0

3 months ago

2.3.1

3 months ago

2.2.2

3 months ago

2.2.1

3 months ago

2.2.0

3 months ago

2.1.0

4 months ago

2.0.0

4 months ago

1.0.3

4 months ago

1.0.2

6 months ago

1.0.1

7 months ago

1.0.0

7 months ago

0.4.3

8 months ago

0.3.0

11 months ago

0.2.7

11 months ago

0.2.6

11 months ago

0.4.1

9 months ago

0.2.3

12 months ago

0.4.0

10 months ago

0.2.5

11 months ago

0.4.2

9 months ago

0.2.4

11 months ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.0

1 year ago