Skip to content

Commit

Permalink
warn on unsupported formats
Browse files Browse the repository at this point in the history
  • Loading branch information
laat committed Sep 27, 2019
1 parent f1e03e3 commit dbaac9c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
33 changes: 33 additions & 0 deletions src/__fixtures__/test.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,28 @@
}
}
}
},
"/unknown": {
"get": {
"operationId": "getUnknwon",
"summary": "Get metadata from the root of the API",
"tags": ["Metadata"],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UnknownFormat"
}
}
}
}
}
}
}
},

"components": {
"schemas": {
"Int32Number": {
Expand Down Expand Up @@ -100,6 +120,19 @@
}
},
"required": ["nullable"]
},
"UnknownFormat": {
"type": "object",
"properties": {
"unknown": {
"type": "string",
"format": "programId"
},
"url": {
"type": "string",
"format": "url"
}
}
}
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/ajvCompile.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import Ajv, { NumberFormatDefinition } from "ajv";
import Ajv, { NumberFormatDefinition, Options } from "ajv";

/**
* @param schema dereferenced schema
*/
export const ajvCompile = (schema: any) => {
export const ajvCompile = (
schema: any,
{
unknownFormats = "ignore",
nullable = true,
jsonPointers = true,
...rest
}: Options = {}
) => {
const ajv = new Ajv({
/**
* TODO: Add custom format supports for following formats.
* Maybe add formats from ajv-oai? https://git.io/fj4gs
*/
unknownFormats: ["float", "double", "byte", "binary", "password"],
nullable: true,
jsonPointers: true
unknownFormats,
nullable,
jsonPointers,
...rest
});
// custom formats
ajv.addFormat("int32", isInt32);
ajv.addFormat("int64", isInt64);
ajv.addFormat("url", ""); // the url format is wrong and deprecated

const clone = JSON.parse(JSON.stringify(schema));
transformNullable(clone);
Expand Down
8 changes: 8 additions & 0 deletions src/toMatchApiResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ describe("api responses", () => {
await expect(response).toMatchApiResponse(testApi, "get", "/number");
await expect(response).not.toMatchApiResponse(testApi, "get", "/");
});
it("GET /unkown", async () => {
const response: IApiResponse = {
body: { unknown: "xxx000000", url: "/something" },
status: 200,
type: "application/json"
};
await expect(response).toMatchApiResponse(testApi, "get", "/unknown");
});
});

0 comments on commit dbaac9c

Please sign in to comment.