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

jest-json-schema-extended

v1.0.1

Published

jest-json-schema-extended =========================

Downloads

23,812

Readme

jest-json-schema-extended

An extension of the popular jest-json-schema package - JSON schema matching for jest.

Example

const {
    dateTime,
    strictObject,
    uuidType,
    numberType,
    booleanType,
    arrayOfItems,
    anyOf,
    exactly,
    stringType,
    stringTypeCanBeEmpty,
    expectToMatchSchema,
    objectWithRequiredProps,
} = require("jest-json-schema-extended")

const objectToTest = {
    id: "47bddd19-e142-45ee-8679-463be8763022",
    enabled: false,
    created: "2019-06-12T15:08:40.292Z",
    requestors: [],
    owners: [
        {
            id: 1,
            lastName: "Robson",
            firstName: "",
            additionalInfo: {
                favouriteTvShow: "The Simpsons",
                goodAtCooking: false,
                propertyThatCanHaveVaryingTypes: "Hello",
            },
        },
        {
            id: 2,
            lastName: "Haroldson",
            firstName: "Doris",
            additionalInfo: {
                favouriteTvShow: "Parks & Recreation",
                married: true,
                propertyThatCanHaveVaryingTypes: 42,
            },
        },
    ],
}

const schema = strictObject({
    id: uuidType,
    enabled: booleanType,
    created: dateTime,
    requestors: exactly([]),
    owners: arrayOfItems(
        strictObject({
            id: numberType,
            lastName: stringType,
            firstName: stringTypeCanBeEmpty,
            additionalInfo: objectWithRequiredProps({
                favouriteTvShow: stringType,
                propertyThatCanHaveVaryingTypes: anyOf([
                    stringType,
                    numberType,
                ]),
            }),
        })
    ),
})

expectToMatchSchema(objectToTest, schema)

Install

npm i --save-dev jest-json-schema-extended

Usage

  • In order to be able to use expectToMatchSchema inside jest, you need to call the following inside your test file (or you place it inside a test framework setup file):
const {setup} = require("jest-json-schema-extended");
setup()

Content

objectType

Asserts that the property is an object.

stringType

Asserts that the property is a string. The string is not allowed to be empty - use stringTypeCanBeEmpty instead if emptiness is needed.

stringTypeCanBeEmpty

Asserts that the property is a string. Allows the string to be empty.

urlType

Asserts that the property is a string looking like an URL.

dateTime

Asserts that the property is a string in date-time format.

uuidType

Asserts that the property is a string looking like a UUID.

stringTypePath

Asserts that the property is a string looking like a UNIX path.

numberType

Asserts that the property is a number.

nullType

Asserts that the property holds the value null.

booleanType

Asserts that the property is a boolean.

arrayType

Asserts that the property is an array.

arrayOfObjectsType

Loosely asserts that the property is an array of objects.

expectToMatchSchema(value, schema)

Assert that a value matches a JSON schema. Prints out errors if mismatches found.

| Param | Type | Description | | --- | --- | --- | | value | Object | The JSON object or value (string, boolean etc.) to test. | | schema | Object | The JSON schema to test the object against. |

strictObject(properties, [options])

Asserts that the object contains all the properties specified - additional properties are not allowed.

| Param | Type | Description | | --- | --- | --- | | properties | Object | Asserts for any key on the object that the property exists. Example: {propOne: stringType, propTwo: numberType} | | [options] | Object | Accepts a property optionalProps which can be a list of optional properties that don't need to be present. |

objectWithRequiredProps(properties)

Asserts that the object contains all the properties specified - additional properties are allowed.

| Param | Type | Description | | --- | --- | --- | | properties | Object | Asserts for any key on the object that the property exists. Example: {propOne: stringType, propTwo: numberType} |

arrayOfItems(itemSchema, options)

Assert a JSON schema against each array item.

| Param | Type | Description | | --- | --- | --- | | itemSchema | | | | options | Object | Accepts an option property minItems which can be used to check that the array contains at least x amount of items. |

anyOf(itemSchemas)

Assert that the property is one of the provided JSON schemas.

| Param | | --- | | itemSchemas |

stringTypeMatching(regex)

Asserts that the property is a string matching the regular expression provided.

| Param | Type | | --- | --- | | regex | RegExp |

stringTypeExact(expStr)

Asserts that the property is exactly the string as specified.

| Param | Type | | --- | --- | | expStr | String |

numberTypeGreaterThan(minimum)

Asserts that the property is a number greater than the given number.

| Param | Type | | --- | --- | | minimum | Number |

numberTypeLessThan(maximum)

Asserts that the property is a number less than the given number.

| Param | Type | | --- | --- | | maximum | Number |

oneOf(valuesExpected)

Asserts that the property received is one of the values given in the array.

| Param | Type | | --- | --- | | valuesExpected | Array.<any> |

arrayTypeOfLength(length)

Asserts that the property is an array of the exactly specified length.

| Param | Type | | --- | --- | | length | Number |

exactly(valueExpected)

Asserts that the property is exactly the value as specified. Can be anything - an object, an array, a string, a boolean, ...

| Param | Type | | --- | --- | | valueExpected | any |

isJsonSchema(toBeDetermined)

Helper function to check whether the passed in object is a JSON schema or a plain object.

| Param | Type | | --- | --- | | toBeDetermined | Object |