americanexpress / jest-json-schema Goto Github PK
View Code? Open in Web Editor NEW✨ JSON schema matcher for Jest
License: Apache License 2.0
✨ JSON schema matcher for Jest
License: Apache License 2.0
Hello, Im able to validate a data file against a schema via ajv-cli but not with jest-json-schema@5
Any pointers?
The schema is https://json.schemastore.org/tsconfig
The test is:
import { matchersWithOptions } from 'jest-json-schema'
import schema from '../../../schemata/tsconfig.json'
import base from './base.json'
expect.extend(matchersWithOptions({
schemas: [base]
}));
it('should match schema', async () => {
expect(base).toMatchSchema(schema)
})
Which yields:
FAIL @librelabs/tsconfig src/base.spec.ts
✕ should match schema (8 ms)
● should match schema
no schema with key or ref "http://json-schema.org/draft-04/schema#"
8 |
9 | it('should match schema', async () => {
> 10 | expect(base).toMatchSchema(schema)
| ^
11 | })
And using ajv directly without using strict mode:
$ ajv validate -s ../../schemata/tsconfig.json -d ./src/base.json
strict mode: missing type "object" for keyword "properties" at "https://json.schemastore.org/tsconfig#/definitions/compilerOptionsDefinition" (strictTypes)
In the AJV, to use the standard Draft07
const Ajv = require("ajv")
but in the documentation of AJVm you can set to use the 2019 version?
const Ajv2019 = require("ajv/dist/2019")
Is this possible using this package?
If the schema to be matched against doesn't contain type: 'object'
at the root level, then if the received value is not JSON (e.g. a number), it won't fail.
Example:
it('should verify it has received an object'), () => {
expect(1).toMatchSchema({
properties: {
a: { type: 'number' }
}
});
}
// should fail because received value is not JSON
For this to work, add type: 'object'
at the root level of the schema
it('should verify it has received an object'), () => {
expect(1).toMatchSchema({
type: 'object', // THIS WORKS
properties: {
a: { type: 'number' }
}
});
}
// properly fails because received value is not JSON
I know that the type property at the root level is part of the JSON Schema specification, and is optional, but seeing that a JSON object is always an object, and not a number or string (although it can contain a property that is a number or string), the type property at the root level should not be present in order to validate that a JSON object is being passed in.
Proposal: The first example above (without the type property at the root level) should cause the test to fail, saying it's received a value that's not JSON (or object).
related to #29
Hi,
We found jest-json-schema does not work with missing field.
Endpoint : https://reqres.in/api/users/2
Method : GET
Actual json response :
{
id: 2,
first_name: 'Janet',
last_name: 'Weaver',
avatar:'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg'
}
The code:
const { matchers } = require('jest-json-schema');
expect.extend(matchers);
const supertest = require('supertest');
const env = require('dotenv').config();
const api = supertest(process.env.API_BASE_URL);
const getSingleUser = id => api.get(`/users/${id}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json');
let response;
describe('Test User', () => {
test('Get single user dengan id 2', async () => {
response = await getSingleUser(2);
expect(response.status, 'Ini harus response 200').toBe(200);
console.log(response.body.data);
const schemaDetailUser = {
type: 'object',
required: ['first_name', 'last_name', 'avatar'],
properties: {
first_name: { type: 'string' },
last_name: { type: 'string' },
avatar: { type: 'string' },
},
};
expect(response.body.data).toMatchSchema(schemaDetailUser);
});
});
We expect that there will be an error occured because the 'id' field is missed.
But actually it does not display a error.
Or do I missed something?
Thank you
Folks could load their own schemas and match against the result, but having help methods to do that and an example in the README would be A++ if you get the time. :)
Hi,
When test case is in typescript, the toMatchSchema option does work.
I have imported and exteded the matchers as instructed.
Getting the following error -
Property 'toMatchSchema' does not exist on type 'JestMatchersShape<Matchers<void, any>, Matchers<Promise, any>>'.ts(2339)
Although, it works perfectly if I change my file back to plain javascript.
There is couple of tools for JSON schema validation by @bahmutov:
That tools depends on nijikokun/generate-schema
.
In the essence, bahmutov/schema-shot
records snapshot in the first time and validates schema later on (by using bahmutov/validate-by-example
).
It would be great to introduce schema generation functionality into the jest-json-schema
.
What do you think?
When the schema itself contains a key "$schema": "https://json-schema.org/draft/2020-12/schema"
, this is thrown as an error.
it('should succeed', () => {
const schema = {
$id: 'https://my-schema.org/SomeObject.json',
$schema: 'https://json-schema.org/draft/2020-12/schema',
title: 'Some title',
description: 'Some description',
type: 'object',
additionalProperties: true,
properties: {
id: {
description: 'An id',
type: 'string',
},
},
}
expect({
$schema: 'https://my-schema.org/SomeObject.json',
id: 'some id',
}).toMatchSchema(schema)
})
The unit test should succeed
The unit test fails with this error:
no schema with key or ref "https://json-schema.org/draft/2020-12/schema"
Hello!
I hope you are doing well!
We are a security research team. Our tool automatically detected a vulnerability in this repository. We want to disclose it responsibly. GitHub has a feature called Private vulnerability reporting, which enables security research to privately disclose a vulnerability. Unfortunately, it is not enabled for this repository.
Can you enable it, so that we can report it?
Thanks in advance!
PS: you can read about how to enable private vulnerability reporting here: https://docs.github.com/en/code-security/security-advisories/repository-security-advisories/configuring-private-vulnerability-reporting-for-a-repository
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.