This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional JOI schema for sequence (#101)
* Cleanup: remove unused fields from recipe schema * Add possibility to set a JOI schema for a sequence This will validate the `attributes` field in the request body. * Bump version
- Loading branch information
1 parent
6bb2ae8
commit a5d2853
Showing
7 changed files
with
190 additions
and
16 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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Joi from "joi"; | ||
|
||
export default function validate(body, schema) { | ||
const fullSchema = Joi.object({ | ||
type: Joi.string().required(), | ||
id: Joi.string().required(), | ||
attributes: schema, | ||
data: Joi.array().optional(), | ||
}).unknown(true); | ||
|
||
const { error, value } = fullSchema.validate(body, { abortEarly: false }); | ||
if (error) { | ||
const details = error.details.map((e) => e.message).join(", "); | ||
const err = new Error(details); | ||
err.validation = true; | ||
throw err; | ||
} | ||
return value; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@bonniernews/b0rker", | ||
"version": "10.2.1", | ||
"version": "10.3.0", | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
|
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,153 @@ | ||
import { fakeCloudTasks, fakePubSub } from "@bonniernews/lu-test"; | ||
import Joi from "joi"; | ||
|
||
import { route, start } from "../../index.js"; | ||
|
||
const schema = Joi.object({ foo: Joi.string().required() }); | ||
|
||
Feature("Sequence with validation", () => { | ||
afterEachScenario(() => { | ||
fakeCloudTasks.reset(); | ||
fakePubSub.reset(); | ||
}); | ||
|
||
Scenario("Validation succeeds", () => { | ||
let broker; | ||
Given("broker is initiated with a recipe with a schema", () => { | ||
broker = start({ | ||
startServer: false, | ||
recipes: [ | ||
{ | ||
namespace: "sequence", | ||
name: "test-order", | ||
sequence: [ | ||
route(".perform.step-1", () => { | ||
return { type: "step-1", id: "step-1-was-here" }; | ||
}), | ||
], | ||
schema, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
const triggerMessage = { | ||
type: "test-order", | ||
id: "some-order-id", | ||
attributes: { foo: "bar" }, | ||
}; | ||
|
||
let response; | ||
When("a trigger message is received", async () => { | ||
response = await fakeCloudTasks.runSequence(broker, "/v2/sequence/test-order", triggerMessage); | ||
}); | ||
|
||
Then("the status code should be 201 Created", () => { | ||
response.firstResponse.statusCode.should.eql(201, response.text); | ||
}); | ||
|
||
And("the sequence should be processed", () => { | ||
response.messages | ||
.map(({ url }) => url) | ||
.should.eql([ "/v2/sequence/test-order/perform.step-1", "/v2/sequence/test-order/processed" ]); | ||
}); | ||
}); | ||
|
||
Scenario("Order is missing type and id", () => { | ||
let broker; | ||
Given("broker is initiated with a recipe with a schema", () => { | ||
broker = start({ | ||
startServer: false, | ||
recipes: [ | ||
{ | ||
namespace: "sequence", | ||
name: "test-order", | ||
sequence: [ | ||
route(".perform.step-1", () => { | ||
return { type: "step-1", id: "step-1-was-here" }; | ||
}), | ||
], | ||
schema, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
And("we can publish pubsub messages", () => { | ||
fakePubSub.enablePublish(broker); | ||
}); | ||
|
||
const triggerMessage = { attributes: { foo: "bar" } }; | ||
|
||
let response; | ||
When("a trigger message is received", async () => { | ||
response = await fakeCloudTasks.runSequence(broker, "/v2/sequence/test-order", triggerMessage, { "correlation-id": "some-epic-id" }); | ||
}); | ||
|
||
Then("the status code should be 201 Created", () => { | ||
response.firstResponse.statusCode.should.eql(201, response.text); | ||
}); | ||
|
||
And("the message should have been sent to the DLX", () => { | ||
fakePubSub.recordedMessages().length.should.eql(1); | ||
fakePubSub.recordedMessages()[0].message.error.message.should.eql('Validation error: "type" is required, "id" is required'); | ||
}); | ||
|
||
And("the sequence should not be processed", () => { | ||
response.messages | ||
.map(({ url }) => url) | ||
.should.eql([ "/v2/sequence/test-order/perform.step-1" ]); | ||
}); | ||
}); | ||
|
||
Scenario("Attribute validation fails", () => { | ||
let broker; | ||
Given("broker is initiated with a recipe with a schema", () => { | ||
broker = start({ | ||
startServer: false, | ||
recipes: [ | ||
{ | ||
namespace: "sequence", | ||
name: "test-order", | ||
sequence: [ | ||
route(".perform.step-1", () => { | ||
return { type: "step-1", id: "step-1-was-here" }; | ||
}), | ||
], | ||
schema, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
And("we can publish pubsub messages", () => { | ||
fakePubSub.enablePublish(broker); | ||
}); | ||
|
||
const triggerMessage = { | ||
type: "test-order", | ||
id: "some-order-id", | ||
attributes: { foo: 42, iShouldNotBeHere: "nope" }, | ||
}; | ||
|
||
let response; | ||
When("a trigger message is received", async () => { | ||
response = await fakeCloudTasks.runSequence(broker, "/v2/sequence/test-order", triggerMessage, { "correlation-id": "some-epic-id" }); | ||
}); | ||
|
||
Then("the status code should be 201 Created", () => { | ||
response.firstResponse.statusCode.should.eql(201, response.text); | ||
}); | ||
|
||
And("the message should have been sent to the DLX", () => { | ||
fakePubSub.recordedMessages().length.should.eql(1); | ||
fakePubSub.recordedMessages()[0].message.error.message.should.eql('Validation error: "attributes.foo" must be a string, "attributes.iShouldNotBeHere" is not allowed'); | ||
}); | ||
|
||
And("the sequence should not be processed", () => { | ||
response.messages | ||
.map(({ url }) => url) | ||
.should.eql([ "/v2/sequence/test-order/perform.step-1" ]); | ||
}); | ||
}); | ||
}); |