Comments (3)
Hi Artur,
Thank you for sharing your example and point of view. From my perspective, I think that schema validation will help you more to achieve your goals for this particular use case. So, I'd recommend you use some additional tools for that. For instance, ajv. Even more, as far as I know, there's no such ability to accomplish it with expect.any()
. However, I believe that it will be a good idea to add a schema validation tool to odottaa
. and have a new method toHaveSchema()
or something like that.
from odottaa.
However, I believe that it will be a good idea to add a schema validation tool to odottaa. and have a new method toHaveSchema() or something like that.
That would be really nice 👍
So, I'd recommend you use some additional tools for that. For instance, ajv
I followed Your advice and I was able to check the schema using ajv. I'm posting the solution If anyone would ever need it.
import { expect } from "@playwright/test";
import Ajv, { JSONSchemaType } from "ajv";
import { test } from "../../../fixtures/loginFixture";
type Condition = "New" | "As good as new" | "Good" | "Used" | "Damaged";
type Guitar = {
guitar: {
manufacturer: string;
model: string;
buildYear: number;
signedByFamousGuitarist: boolean;
signedBy?: string | null; // optional parameter, once present can be string or null
condition: {
keys: Condition;
fretboard: Condition;
front: Condition;
back: Condition;
};
};
};
/*
* Create JSON schema based on provided Type in our case an Array of Guitars.
* When schema is not matching Guitars type definition code editor highlights and error
*/
export const schema: JSONSchemaType<Guitar[]> = {
type: "array",
items: {
type: "object",
required: ["guitar"],
additionalProperties: false,
properties: {
guitar: {
type: "object",
additionalProperties: false,
required: [
"manufacturer",
"model",
"signedByFamousGuitarist",
"buildYear",
"condition",
],
properties: {
signedBy: { type: "string", nullable: true },
manufacturer: { type: "string" },
model: { type: "string" },
signedByFamousGuitarist: { type: "boolean" },
buildYear: { type: "number" },
condition: {
additionalProperties: false,
required: ["keys", "fretboard", "front", "back"],
type: "object",
properties: {
keys: { type: "string" },
fretboard: { type: "string" },
front: { type: "string" },
back: { type: "string" },
},
},
},
},
},
},
};
test.describe("/get my guitars endpoint", () => {
test("fetches all guitars and check schema", async ({ request }) => {
const ajv = new Ajv();
const validate = await ajv.compile(schema);
const response = await request.get("/my/guitars");
const responseBody = await response.json();
expect(response).toHaveStatusCode(200);
if (validate(responseBody)) {
// whatever You want to do if schema assertion is successful
} else {
// when schema assertion fails
throw new Error("JSON Schema FATAL ERROR");
}
});
});
Make sure to include additionalProperties: false
to the schema - for strict assertions. Otherwise You might risk a false positive If API schema includes new params.
from odottaa.
Awesome, thank you for sharing!
from odottaa.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from odottaa.