Skip to content

Commit

Permalink
validation working with separate response and data schema
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Jan 7, 2025
1 parent 7200013 commit d50bb5b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 175 deletions.
113 changes: 0 additions & 113 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"uid": "^2.0.2"
},
"devDependencies": {
"@hyperjump/json-schema": "^1.10.0",
"@types/cors": "^2.8.17",
"@types/express": "^5.0.0",
"@types/node": "^22.9.3",
Expand Down
21 changes: 7 additions & 14 deletions schema/IndexResponse.schema.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/grout-index",
"$defs": {
"index-data": {
"$dynamicAnchor": "TData",
"type": "object",
"properties": {
"versionx": {
"type": "string",
"pattern": "^[0-9]+.[0-9]+.[0-9]+$"
}
},
"additionalProperties": false,
"required": ["versionx"]
"type": "object",
"properties": {
"version": {
"type": "string",
"pattern": "^[0-9]+.[0-9]+.[0-9]+$"
}
},
"$ref": "https://example.com/grout-response"
"additionalProperties": false,
"required": ["version"]
}
38 changes: 16 additions & 22 deletions schema/MetadataResponse.schema.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"data": {
"$dynamicAnchor": "TData",
"type": "object",
"properties": {
"datasets": {
"type": "object",
"properties": {
"datasets": {
"tile": {
"type": "object",
"properties": {
"tile": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"levelsx": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["levelsx"]
"additionalProperties": {
"type": "object",
"properties": {
"levels": {
"type": "array",
"items": { "type": "string" }
}
}
},
"additionalProperties": false,
"required": ["tile"]
},
"required": ["levels"]
}
}
},
"additionalProperties": false,
"required": ["datasets"]
"required": ["tile"]
}
},
"$ref": "grout-response"
"additionalProperties": false,
"required": ["datasets"]
}
12 changes: 4 additions & 8 deletions schema/Response.schema.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/grout-response",
"$defs": {
"content": {
"$dynamicAnchor": "TData",
"not": true
}
},
"type": "object",
"properties": {
"status": {
Expand Down Expand Up @@ -42,7 +35,10 @@
}
},
"data": {
"$dynamicRef": "#TData"
"oneOf": [
{ "type": "null" },
{ "type": "object" }
]
}
},
"additionalProperties": false,
Expand Down
29 changes: 12 additions & 17 deletions tests/integration/integrationTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import request from "supertest";
import Ajv from "ajv/dist/2020"

Check failure on line 2 in tests/integration/integrationTest.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
import { registerSchema, validate } from "@hyperjump/json-schema/draft-2020-12";
import { expect } from "vitest";

export const grout = request("http://localhost:5000");
Expand All @@ -16,26 +15,22 @@ export const getData = async (url: string, schemaName: string) => {

const ajv = new Ajv();

const schema = await getSchema(schemaName);
ajv.addSchema(schema);
const responseSchema = await getSchema("Response"); // outer Response schema
//ajv.addSchema(responseSchema);

const responseSchema = await getSchema("Response"); // We always need the outer Response schema
ajv.addSchema(responseSchema);
const dataSchema = await getSchema(schemaName);
//ajv.addSchema(dataSchema);

registerSchema(responseSchema, "https://example.com/grout-response");
registerSchema(schema, "https://example.com/grout-index");
// First validate that the entire response conforms to the Response schema...
const responseValidate = ajv.compile(responseSchema);
expect(responseValidate(response.body), `Response schema errors: \n ${ajv.errorsText(responseValidate.errors)} \nfor response: \n${JSON.stringify(response.body, null, 2)}`).toBe(true);

Check failure on line 26 in tests/integration/integrationTest.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 188. Maximum allowed is 120

Check failure on line 26 in tests/integration/integrationTest.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `responseValidate(response.body),·`Response·schema·errors:·\n·${ajv.errorsText(responseValidate.errors)}·\nfor·response:·\n${JSON.stringify(response.body,·null,·2)}`` with `⏎········responseValidate(response.body),⏎········`Response·schema·errors:·\n·${ajv.errorsText(responseValidate.errors)}·\nfor·response:·\n${JSON.stringify(response.body,·null,·2)}`⏎····`

const output = await validate("https://example.com/grout-index", response.body);


const valid = ajv.validate(schema, response.body);
//const validate = ajv.compile(schema);
//const valid = validate(response.body);
expect(valid, `Schema errors: \n ${JSON.stringify(ajv.errors)} \nfor response: \n${JSON.stringify(response.body, null, 2)}`).toBe(true);

expect(output.valid, `Schema errors: ${JSON.stringify(output)}`).toBe(true);
// ...then that the data part matches the expected data schema
const dataValidate = ajv.compile(dataSchema);
const data = response.body.data;
expect(dataValidate(data), `Data schema errors: \n${ajv.errorsText(dataValidate.errors)} \nfor data: \n${JSON.stringify(data, null, 2)}`).toBe(true)

Check failure on line 31 in tests/integration/integrationTest.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 152. Maximum allowed is 120

Check failure on line 31 in tests/integration/integrationTest.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `dataValidate(data),·`Data·schema·errors:·\n${ajv.errorsText(dataValidate.errors)}·\nfor·data:·\n${JSON.stringify(data,·null,·2)}`).toBe(true)` with `⏎········dataValidate(data),⏎········`Data·schema·errors:·\n${ajv.errorsText(dataValidate.errors)}·\nfor·data:·\n${JSON.stringify(data,·null,·2)}`⏎····).toBe(true);`

expect(response.body.status).toBe("success");
expect(response.body.errors).toBe(null);
return response.body.data;
return data;
};

0 comments on commit d50bb5b

Please sign in to comment.