Skip to content

rickschubert/jest-json-schema-extended

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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