Skip to content

Commit

Permalink
fix(platform-params): resolve dataPath when the data is a Query/Path …
Browse files Browse the repository at this point in the history
…params
  • Loading branch information
Romakita committed Apr 8, 2024
1 parent faa658b commit cf007e2
Show file tree
Hide file tree
Showing 55 changed files with 376 additions and 101 deletions.
4 changes: 2 additions & 2 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = {
coverageThreshold: {
global: {
statements: 98.75,
branches: 94.67,
functions: 95.41,
branches: 94,
functions: 94,
lines: 98.75
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/di/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
statements: 99.08,
branches: 96.79,
lines: 99.08,
functions: 99.17
functions: 99.04
}
}
};
2 changes: 1 addition & 1 deletion packages/orm/adapters-redis/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
...require("@tsed/jest-config"),
coverageThreshold: {
global: {
branches: 93.68,
branches: 93.54,
functions: 100,
lines: 100,
statements: 100
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/adapters/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
},
coverageThreshold: {
global: {
branches: 88.6,
branches: 87.5,
functions: 100,
lines: 100,
statements: 100
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/mikro-orm/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
roots: ["<rootDir>/src", "<rootDir>/test"],
coverageThreshold: {
global: {
branches: 87.17,
branches: 87.05,
functions: 97.56,
lines: 98.24,
statements: 98.24
Expand Down
8 changes: 4 additions & 4 deletions packages/orm/mongoose/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = {
},
coverageThreshold: {
global: {
statements: 98.95,
branches: 95,
functions: 99,
lines: 98.95
statements: 99.07,
branches: 95.02,
functions: 100,
lines: 99.07
}
}
};
4 changes: 2 additions & 2 deletions packages/orm/objection/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = {
},
coverageThreshold: {
global: {
branches: 96.87,
functions: 91.48,
branches: 96.2,
functions: 100,
lines: 98.26,
statements: 98.26
}
Expand Down
4 changes: 2 additions & 2 deletions packages/orm/prisma/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = {
coverageThreshold: {
global: {
statements: 98.8,
branches: 94.3,
functions: 96.96,
branches: 93.47,
functions: 95.65,
lines: 98.8
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/common/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
coverageThreshold: {
global: {
statements: 98.3,
branches: 91.52,
branches: 90.09,
functions: 95.78,
lines: 98.3
}
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/platform-cache/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
},
coverageThreshold: {
global: {
branches: 94.53,
branches: 94.26,
functions: 100,
lines: 100,
statements: 100
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/platform-exceptions/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
coverageThreshold: {
global: {
statements: 100,
branches: 95.34,
branches: 94.73,
functions: 100,
lines: 100
}
Expand Down
8 changes: 4 additions & 4 deletions packages/platform/platform-express/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = {
collectCoverageFrom: ["src/**/*.{ts,tsx}", "!src/**/*.d.ts"],
coverageThreshold: {
global: {
statements: 90,
branches: 79.66,
functions: 90,
lines: 90
statements: 98.37,
branches: 93.54,
functions: 100,
lines: 98.37
}
}
};
4 changes: 4 additions & 0 deletions packages/platform/platform-express/test/ajv-errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe("AJV Errors", () => {
data: {
foo: 1
},
requestPath: "body",
dataPath: "",
instancePath: "",
keyword: "errorMessage",
Expand Down Expand Up @@ -121,6 +122,7 @@ describe("AJV Errors", () => {
errors: [
{
data: [],
requestPath: "body",
dataPath: "",
instancePath: "",
keyword: "errorMessage",
Expand Down Expand Up @@ -161,6 +163,7 @@ describe("AJV Errors", () => {
foo: "foo",
test: "hey"
},
requestPath: "body",
dataPath: "",
instancePath: "",
keyword: "errorMessage",
Expand Down Expand Up @@ -198,6 +201,7 @@ describe("AJV Errors", () => {
errors: [
{
data: [],
requestPath: "body",
dataPath: "",
instancePath: "",
keyword: "errorMessage",
Expand Down
1 change: 1 addition & 0 deletions packages/platform/platform-express/test/array-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe("Array Body", () => {
expect(body).toEqual({
errors: [
{
requestPath: "body",
data: [
{
test: "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ describe("Discriminator", () => {
expect(body).toEqual({
errors: [
{
requestPath: "body",
data: {
type: "test",
url: "https://url.com",
Expand Down
1 change: 1 addition & 0 deletions packages/platform/platform-express/test/pageable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ describe("Pageable", () => {
errors: [
{
data: -1,
requestPath: "query",
dataPath: ".page",
instancePath: "/page",
keyword: "minimum",
Expand Down
172 changes: 172 additions & 0 deletions packages/platform/platform-express/test/validation-error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import {BodyParams, Controller, PlatformTest, Post, QueryParams} from "@tsed/common";
import {PlatformTestSdk} from "@tsed/platform-test-sdk";
import {email, Email, Required} from "@tsed/schema";
import SuperTest from "supertest";
import {PlatformExpress} from "../src/index";
import {rootDir, Server} from "./app/Server";

class Model {
@Required()
phone: string;
}

@Controller("/query-params-validation")
class TestQueryParamsCtrl {
@Post("/scenario-1")
testScenario1(
@QueryParams("email")
@Required()
@Email()
email: string,
@BodyParams(Model) @Required() body: Model
) {
return {email, body};
}

@Post("/scenario-2")
testScenario2(@BodyParams(Model) @Required() body: Model) {
return {body};
}
}

const utils = PlatformTestSdk.create({
rootDir,
platform: PlatformExpress,
server: Server
});

describe("QueryParamValidation", () => {
let request: SuperTest.Agent;

beforeEach(
utils.bootstrap({
mount: {
"/rest": [TestQueryParamsCtrl]
}
})
);
beforeEach(() => {
request = SuperTest(PlatformTest.callback());
});
afterEach(utils.reset);

describe("scenario 1", () => {
it("should validate the email", async () => {
const response = await request
.post("/rest/query-params-validation/[email protected]")
.send({
phone: "phone"
})
.expect(200);

expect(response.body).toEqual({email: "[email protected]", body: {phone: "phone"}});
});

it("should validate the email (REQUIRED)", async () => {
const response = await request.post("/rest/query-params-validation/scenario-1").send({}).expect(400);

expect(response.body).toEqual({
errors: [
{
requestPath: "query",
keyword: "required",
message: "It should have required parameter 'email'",
modelName: "query",
params: {
missingProperty: "email"
},
schemaPath: "#/required",
dataPath: ".email"
}
],
message: "Bad request on parameter \"request.query.email\".\nIt should have required parameter 'email'",
name: "REQUIRED_VALIDATION_ERROR",
status: 400
});
});

it("should validate the email (FORMAT)", async () => {
const response = await request.post("/rest/query-params-validation/scenario-1?email=wrong").send({}).expect(400);

expect(response.body).toEqual({
errors: [
{
data: "wrong",
requestPath: "query",
dataPath: ".email",
instancePath: "",
keyword: "format",
message: 'must match format "email"',
params: {
format: "email"
},
schemaPath: "#/format"
}
],
message: 'Bad request on parameter "request.query.email".\nValue must match format "email". Given value: "wrong"',
name: "AJV_VALIDATION_ERROR",
status: 400
});
});
});
describe("scenario 2", () => {
it("should validate the email", async () => {
const response = await request
.post("/rest/query-params-validation/[email protected]")
.send({
phone: "phone"
})
.expect(200);

expect(response.body).toEqual({email: "[email protected]", body: {phone: "phone"}});
});

it("should validate the email (REQUIRED)", async () => {
const response = await request.post("/rest/query-params-validation/scenario-2").send({}).expect(400);

expect(response.body).toEqual({
errors: [
{
dataPath: ".phone",
instancePath: "",
keyword: "required",
message: "must have required property 'phone'",
modelName: "Model",
params: {
missingProperty: "phone"
},
requestPath: "body",
schemaPath: "#/required"
}
],
message: 'Bad request on parameter "request.body".\nModel must have required property \'phone\'. Given value: "undefined"',
name: "AJV_VALIDATION_ERROR",
status: 400
});
});

it("should validate the email (FORMAT)", async () => {
const response = await request.post("/rest/query-params-validation/scenario-2?email=wrong").send({}).expect(400);

expect(response.body).toEqual({
errors: [
{
dataPath: ".phone",
instancePath: "",
keyword: "required",
message: "must have required property 'phone'",
modelName: "Model",
params: {
missingProperty: "phone"
},
requestPath: "body",
schemaPath: "#/required"
}
],
message: 'Bad request on parameter "request.body".\nModel must have required property \'phone\'. Given value: "undefined"',
name: "AJV_VALIDATION_ERROR",
status: 400
});
});
});
});
6 changes: 3 additions & 3 deletions packages/platform/platform-koa/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = {
},
coverageThreshold: {
global: {
statements: 99.48,
branches: 94.5,
statements: 99.49,
branches: 95.65,
functions: 100,
lines: 99.48
lines: 99.49
}
}
};
2 changes: 1 addition & 1 deletion packages/platform/platform-params/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
coverageThreshold: {
global: {
statements: 99.81,
branches: 93.04,
branches: 90.9,
functions: 100,
lines: 99.81
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe("ParseExpressionError", () => {
origin: {
errors: [
{
dataPath: "",
requestPath: "name",
dataPath: ".expression",
keyword: "required",
message: "It should have required parameter 'expression'",
modelName: "name",
Expand Down Expand Up @@ -101,6 +102,7 @@ describe("ParseExpressionError", () => {
origin: {
errors: [
{
requestPath: "name",
dataPath: "hello"
}
],
Expand Down
Loading

0 comments on commit cf007e2

Please sign in to comment.