Skip to content

Commit

Permalink
Update enjoi and @hapi/joi to Joi 17 (#176)
Browse files Browse the repository at this point in the history
* Fixed failing `unknown` call for array schema

After commit 429bfbf Joi object unknown method was called for all input schemas. This results in a runtime error for array input schema. This commit only invokes unknown method for object schema type. For convenience VS Code launch file to debug tape tests is also included.

* Update enjoi and @hapi/joi to Joi 17

* Changed validator to return unwraped value

* Updated enjoi dependency to 7.0.0
  • Loading branch information
sprootshift authored May 11, 2020
1 parent de1b158 commit 4dc71ca
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const requireApi = function (path) {

const register = async function (server, options, next) {

const validation = Joi.validate(options, optionsSchema);
const validation = optionsSchema.validate(options);

Hoek.assert(!validation.error, validation.error);

Expand Down
41 changes: 21 additions & 20 deletions lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
const Enjoi = require('enjoi');
const Joi = require('@hapi/joi');

const types = {
int64: Joi.string().regex(/^\d+$/),
byte: Joi.string().base64(),
binary: Joi.binary(),
date: Joi.date(),
'date-time': Joi.date().iso(),
file: Joi.object({
value: Joi.binary().required(true),
consumes: Joi.array().items([
Joi.string().regex(/multipart\/form-data|application\/x-www-form-urlencoded/)
]).required(true),
in: Joi.string().regex(/formData/).required(true)
})
};
const extensions = [
{ type: 'int64', base: Joi.string().regex(/^\d+$/) },
{ type: 'byte', base: Joi.string().base64() },
{ type: 'date-time', base: Joi.date().iso() },
{
type: 'file',
base: Joi.object({
value: Joi.binary().required(true),
consumes: Joi.array().items(
Joi.string().regex(/multipart\/form-data|application\/x-www-form-urlencoded/)
).required(true),
in: Joi.string().regex(/formData/).required(true)
})
}
];

const refineType = function (type, format) {
if (type === 'integer') {
Expand All @@ -35,7 +36,7 @@ const refineType = function (type, format) {
}
};

const enjoi = Enjoi.defaults({ types, refineType });
const enjoi = Enjoi.defaults({ extensions, refineType });

const create = function (options = {}) {

Expand All @@ -46,7 +47,7 @@ const create = function (options = {}) {

if ((parameter.in === 'body' || parameter.in === 'formData') && parameter.schema) {
schema = enjoi.schema(parameter.schema);
if (schema.schemaType === 'object') {
if (schema.type === 'object') {
schema = schema.unknown(allowUnknownProperties);
}
}
Expand Down Expand Up @@ -84,7 +85,7 @@ const create = function (options = {}) {
schema = schema.required();
}

if (parameter.in !== 'body' && parameter.allowEmptyValue){
if (parameter.in !== 'body' && parameter.allowEmptyValue) {
schema = schema.allow('').optional();
}

Expand Down Expand Up @@ -112,7 +113,7 @@ const create = function (options = {}) {
throw result.error;
}

return result;
return result.value;
}
};
};
Expand Down Expand Up @@ -196,7 +197,7 @@ const create = function (options = {}) {
}

for (const [key, value] of Object.entries(validate)) {
if (typeof value === 'object' && !value.isJoi) {
if (typeof value === 'object' && !Joi.isSchema(value)) {
validate[key] = Joi.object(value);
}
}
Expand Down Expand Up @@ -233,7 +234,7 @@ const coercion = function (parameter, consumes) {
let fn;

switch (parameter.type) {
case 'array' :
case 'array':
fn = function (data) {
if (Array.isArray(data)) {
return data;
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
},
"dependencies": {
"@hapi/hoek": "^9.0.4",
"@hapi/joi": "^14.5.0",
"dot-prop": "^4.2.0",
"enjoi": "^6.0.1",
"@hapi/joi": "^17.1.0",
"dot-prop": "^5.2.0",
"enjoi": "^7.0.0",
"js-yaml": "^3.11.0",
"merge-object-files": "^2.0.0",
"swagger-parser": "^9.0.1"
},
"devDependencies": {
"@hapi/boom": "^9.1.0",
"@hapi/eslint-config-hapi": "^13.0.2",
"@hapi/eslint-plugin-hapi": "^4.3.5",
"@hapi/hapi": "^19.1.1",
"eslint": "^4.19.1",
"eslint-config-hapi": "^11.1.0",
"eslint-plugin-hapi": "^4.1.0",
"eslint": "^6.8.0",
"nyc": "^15.0.0",
"tape": "^4.9.0"
"tape": "^5.0.0"
},
"scripts": {
"test": "tape test/*.js",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/handlers/pets.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Store = require('../lib/store');
const Store = require('../lib/store');

module.exports = {
get: function (req, h) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/handlers/pets/{id}.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Store = require('../../lib/store');
const Store = require('../../lib/store');

module.exports = {
get: [
Expand Down
2 changes: 1 addition & 1 deletion test/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Test('validator special types', function(t) {

const v = validator.makeAll(api.paths['/test/{foo*}'].get.parameters);

const keys = Object.keys(v.validate.params.describe().children);
const keys = Object.keys(v.validate.params.describe().keys);

if (keys.length === 1 && keys[0] === 'foo') {
return t.pass(`${keys.join(', ')} are valid.`);
Expand Down

0 comments on commit 4dc71ca

Please sign in to comment.