jest-json-schema-extended =========================
jest-json-schema-extended
=========================
An extension of the popular jest-json-schema package - JSON schema matching for jest.
js
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-extendedUsage
- 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):`js
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. || Param |
| --- |
| itemSchemas |
| Param | Type |
| --- | --- |
| regex | RegExp |
| Param | Type |
| --- | --- |
| expStr | String |
| Param | Type |
| --- | --- |
| minimum | Number |
| Param | Type |
| --- | --- |
| maximum | Number |
| Param | Type |
| --- | --- |
| valuesExpected | Array.<any> |
| Param | Type |
| --- | --- |
| length | Number |
| Param | Type |
| --- | --- |
| valueExpected | any |
| Param | Type |
| --- | --- |
| toBeDetermined | Object |