jest-expect-jsonschema
v1.1.4
Published
Jest/Vitest matcher for asserting valid JSON Schema schemas
Maintainers
Readme
jest-expect-jsonschema
A Vitest and Jest custom matcher for asserting valid JSON Schema objects.
Installation
npm install jest-expect-jsonschema --save-devUsage
import { toBeValidJSONSchema } from 'jest-expect-jsonschema';
import { expect, test } from 'vitest';
expect.extend({ toBeValidJSONSchema });
test('should be valid JSON Schema', async () => {
await expect({
type: 'string',
}).toBeValidJSONSchema();
});
test('should not be valid JSON Schema', () => {
await expect({
type: 'invalid-type',
}).not.toBeValidJSONSchema();
});The usage is nearly identical in Jest:
import { toBeValidJSONSchema } from 'jest-expect-jsonschema';
expect.extend({ toBeValidJSONSchema });
test('should be valid JSON Schema', () => {
await expect({
type: 'string',
}).toBeValidJSONSchema();
});
test('should not valid JSON Schema', async () => {
await expect({
type: 'invalid-type',
}).not.toBeValidJSONSchema();
});There is also a toBeValidJSONSchemas matcher that can be used with an array of objects:
import { toBeValidJSONSchemas } from 'jest-expect-jsonschema';
import { expect, test } from 'vitest';
expect.extend({ toBeValidJSONSchemas });
test('should be valid JSON Schemas', async () => {
await expect([
{
type: 'string',
},
{
type: 'object',
required: ['name'],
properties: {
name: {
type: 'string',
},
},
},
]).toBeValidJSONSchemas();
});