-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(platform-params): resolve dataPath when the data is a Query/Path …
…params
- Loading branch information
Showing
55 changed files
with
376 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ module.exports = { | |
statements: 99.08, | ||
branches: 96.79, | ||
lines: 99.08, | ||
functions: 99.17 | ||
functions: 99.04 | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
packages/platform/platform-express/test/validation-error.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.