Skip to content

Commit

Permalink
Merge pull request #202 from nzzdev/release-10.0.1
Browse files Browse the repository at this point in the history
Release 10.0.1
  • Loading branch information
romankaravia committed Feb 26, 2020
2 parents 4e89d89 + c1c7805 commit 008c923
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 16 deletions.
49 changes: 49 additions & 0 deletions helper/custom-joi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const Bourne = require("@hapi/bourne");
const Joi = require("@hapi/joi");

// As of Joi v16, strings are no longer automatically converted to objects/arrays,
// even if the convert option of any.validate() is "true" (which is the default).
// => A string such as '{"foo": "bar"}' is not valid for Joi.object().
// And '["foo", "bar"]' is not valid for Joi.array().
// This is a problem when passing objects/arrays in HTTP query strings, where they
// can only be represented as strings. This custom extension allows converting
// objects/arrays to strings as it was done in Joi v15 and before.
// Adapted from https://github.com/hapijs/joi/issues/2037
// (section "Array and object string coercion")
module.exports = Joi.extend(
{
type: "object",
base: Joi.object(),
coerce: {
from: "string",
method(value) {
if (value[0] !== "{" && !/^\s*\{/.test(value)) {
return;
}

try {
return { value: Bourne.parse(value) };
} catch (ignoreErr) {}
}
}
},
{
type: "array",
base: Joi.array(),
coerce: {
from: "string",
method(value) {
if (
typeof value !== "string" ||
(value[0] !== "[" && !/^\s*\[/.test(value))
) {
return;
}

try {
return { value: Bourne.parse(value) };
} catch (ignoreErr) {}
}
}
}
);
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nzz/q-server",
"version": "10.0.0",
"version": "10.0.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/base/routes/display-options-schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../../../helper/custom-joi.js");
const Bounce = require("@hapi/bounce");
const deepmerge = require("deepmerge");

Expand Down
2 changes: 1 addition & 1 deletion plugins/core/base/routes/item.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Boom = require("@hapi/boom");
const Joi = require("@hapi/joi");
const Joi = require("../../../../helper/custom-joi.js");
const Ajv = require("ajv");
const ajv = new Ajv({ schemaId: "id" });
// add draft-04 support explicit
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/base/routes/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../../../helper/custom-joi.js");

module.exports = {
path: "/search",
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/base/routes/tool-default.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../../../helper/custom-joi.js");

module.exports = {
getGetRoute: function(options) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/base/routes/tool-schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../../../helper/custom-joi.js");
const Boom = require("@hapi/boom");
const jsonSchemaRefParser = require("json-schema-ref-parser");

Expand Down
2 changes: 1 addition & 1 deletion plugins/core/editor/configSchemas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../../helper/custom-joi.js");

const target = Joi.object().pattern(
Joi.string(), // the key needs to be a string
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/editor/routes/locales.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../../../helper/custom-joi.js");

module.exports = {
getGetToolsRoute: function() {
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/rendering-info/configSchemas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../../helper/custom-joi.js");

const target = Joi.object().pattern(
Joi.string(), // the key needs to be a string
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/rendering-info/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Boom = require("@hapi/boom");
const Joi = require("@hapi/joi");
const Joi = require("../../../helper/custom-joi.js");
const Hoek = require("@hapi/hoek");

const querystring = require("querystring");
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/rendering-info/size-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../../helper/custom-joi.js");
const Boom = require("@hapi/boom");

// size, width and height are optional
Expand Down
2 changes: 1 addition & 1 deletion plugins/screenshot/routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Boom = require("@hapi/boom");
const Joi = require("@hapi/joi");
const Joi = require("../../helper/custom-joi.js");

const getScreenshotImage = require("./helpers.js").getScreenshotImage;
const getScreenshotInfo = require("./helpers.js").getScreenshotInfo;
Expand Down
2 changes: 1 addition & 1 deletion plugins/statistics/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Joi = require("@hapi/joi");
const Joi = require("../../helper/custom-joi.js");
const Boom = require("@hapi/boom");

module.exports = {
Expand Down
7 changes: 7 additions & 0 deletions test/e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,13 @@ lab.experiment("core rendering-info", () => {
}
);

it("accepts a toolRuntimeConfig object", async () => {
const response = await server.inject(
'/rendering-info/mock-item-active/pub1?toolRuntimeConfig={"foo":"bar"}'
);
expect(response.statusCode).to.be.equal(200);
});

it(
"returns an error if rendering-info tool endpoint returns one",
{ plan: 1 },
Expand Down
2 changes: 1 addition & 1 deletion test/mock/tool1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Hapi = require("@hapi/hapi");
const Joi = require("@hapi/joi");
const Joi = require("../../helper/custom-joi.js");

const server = Hapi.server({
port: 9999,
Expand Down
2 changes: 1 addition & 1 deletion test/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Hapi = require("@hapi/hapi");
const Joi = require("@hapi/joi");
const Joi = require("../helper/custom-joi.js");

function getServer() {
let server = Hapi.server({
Expand Down
18 changes: 18 additions & 0 deletions test/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ lab.experiment("rendering-info toolRuntimeConfig", () => {
}
);

it("validates a stringified object", async () => {
const Joi = require("../helper/custom-joi.js");

const schema = Joi.object({ foo: Joi.string() });
expect(schema.validate('{"foo": "bar"}')).to.equal({
value: { foo: "bar" }
});
});

it("validates a stringified array", async () => {
const Joi = require("../helper/custom-joi.js");

const schema = Joi.array().items(Joi.string());
expect(schema.validate('["foo", "bar"]')).to.equal({
value: ["foo", "bar"]
});
});

it("fails to validate invalid size object", { plan: 10 }, async () => {
const validateSize = require("../plugins/core/rendering-info/size-helpers.js")
.validateSize;
Expand Down

0 comments on commit 008c923

Please sign in to comment.