Skip to content

Commit

Permalink
Merge pull request #2 from matejdr/joi-16-fix
Browse files Browse the repository at this point in the history
- fixed joi validation issues
  • Loading branch information
Tornquist authored Nov 10, 2019
2 parents 1fdd3c7 + cde0205 commit 2ea3b34
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 32 deletions.
1 change: 1 addition & 0 deletions examples/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ let swaggerOptions = {
};

const ser = async () => {
// eslint-disable-next-line no-useless-catch
try {
const server = Hapi.Server({
host: 'localhost',
Expand Down
2 changes: 1 addition & 1 deletion lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internals.paths.prototype.build = function(routes) {
// has different structure, just pass straight through
routeData.payloadParams = Hoek.reach(routeOptions, 'validate.payload');
// if its a native javascript object convert it to JOI
if (!routeData.payloadParams.isJoi) {
if (!Utilities.isJoi(routeData.payloadParams)) {
routeData.payloadParams = Joi.object(routeData.payloadParams);
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ internals.properties.prototype.parsePropertyMetadata = function(property, name,
parent.optional.push(name);
}
}
// interdependencies are not yet supported https://github.com/OAI/OpenAPI-Specification/issues/256
if (describe.whens && describe.whens.length > 0) {
describe.whens.forEach(test => {
if (Hoek.reach(test, 'then.flags.presence') === 'required') {
if (parent.required === undefined) {
parent.required = [];
}
parent.required.push(name);
}
});
}
}

property.default = Hoek.reach(joiObj, '_flags.default');
Expand Down Expand Up @@ -356,6 +367,7 @@ internals.properties.prototype.parseDate = function(property, joiObj) {
internals.properties.prototype.parseObject = function(property, joiObj, name, parameterType, useDefinitions, isAlt) {
property.properties = {};

// eslint-disable-next-line no-unused-vars
joiObj._ids._byKey.forEach((obj, key) => {
let keyName = obj.id;
let itemName = obj.id;
Expand Down
4 changes: 2 additions & 2 deletions lib/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internals.responses.prototype.optionOverride = function(discoveredSchemas, userD
let out = Hoek.clone(userDefindedSchemas[key]);

// test for any JOI objects
if (Hoek.reach(userDefindedSchemas[key], 'schema.isJoi') && userDefindedSchemas[key].schema.isJoi === true) {
if (Hoek.reach(userDefindedSchemas[key], 'schema') && Utilities.isJoi(userDefindedSchemas[key].schema) === true) {
out = this.getResponse(key, userDefindedSchemas[key].schema, useDefinitions, isAlt);
out.description = userDefindedSchemas[key].description;

Expand Down Expand Up @@ -139,7 +139,7 @@ internals.responses.prototype.getResponse = function(statusCode, joiObj, useDefi
//name, joiObj, parent, parameterType, useDefinitions, isAlt
let outProperties = this.properties.parseProperty(null, joiObj, null, 'body', useDefinitions, false);
out = {
description: Hoek.reach(joiObj, '_description'),
description: Hoek.reach(joiObj, '_flags.description'),
schema: outProperties
};

Expand Down
10 changes: 5 additions & 5 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ utilities.startsWith = function(str, test) {
utilities.hasProperties = function(obj) {
let key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return true;
}
}
Expand All @@ -88,7 +88,7 @@ utilities.hasProperties = function(obj) {
utilities.deleteEmptyProperties = function(obj) {
let key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
// delete properties undefined values
if (obj[key] === undefined || obj[key] === null) {
delete obj[key];
Expand Down Expand Up @@ -167,7 +167,7 @@ utilities.replaceValue = function(array, current, replacement) {
*/
utilities.hasKey = function(obj, findKey) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (typeof obj[key] === 'object') {
if (this.hasKey(obj[key], findKey) === true) {
return true;
Expand All @@ -191,7 +191,7 @@ utilities.hasKey = function(obj, findKey) {
*/
utilities.findAndRenameKey = function(obj, findKey, replaceKey) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (typeof obj[key] === 'object') {
this.findAndRenameKey(obj[key], findKey, replaceKey);
}
Expand All @@ -216,7 +216,7 @@ utilities.findAndRenameKey = function(obj, findKey, replaceKey) {
*/
utilities.removeProps = function(obj, listOfProps) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (listOfProps.indexOf(key) === -1 && this.startsWith(key, 'x-') === false) {
delete obj[key];
//console.log('Removed property: ' + key + ' from object: ', JSON.stringify(obj));
Expand Down
43 changes: 29 additions & 14 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"dependencies": {
"@hapi/boom": "^7.4.10",
"@hapi/hoek": "^8.2.4",
"@hapi/joi": "^16.1.4",
"handlebars": "^4.3.1",
"@hapi/joi": "^16.1.7",
"handlebars": "^4.5.1",
"http-status": "^1.0.1",
"json-schema-ref-parser": "^6.1.0",
"swagger-parser": "4.0.2",
Expand Down
22 changes: 22 additions & 0 deletions test/Integration/alternatives-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,28 @@ lab.experiment('alternatives', () => {
}
},
type: 'object'
},
'Model2': {
properties: {
data: {
type: 'string'
},
extra: {
'$ref': '#/definitions/Dimensions'
},
key: {
type: 'string'
},
type: {
enum: [
'string',
'number',
'image'
],
type: 'string'
}
},
type: 'object'
}
});

Expand Down
16 changes: 10 additions & 6 deletions test/Integration/file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ lab.experiment('file', () => {
});

lab.test('upload with binary file type', async () => {
routes.options.validate.payload.file = Joi.binary()
.meta({ swaggerType: 'file' })
.required();
routes.options.validate.payload = Joi.object({
file: Joi.binary()
.meta({ swaggerType: 'file' })
.required()
});

const server = await Helper.createServer({}, routes);

Expand All @@ -80,9 +82,11 @@ lab.experiment('file', () => {
});

lab.test('file type not fired on other meta properties', async () => {
routes.options.validate.payload.file = Joi.any()
.meta({ anything: 'test' })
.required();
routes.options.validate.payload = Joi.object({
file: Joi.any()
.meta({ anything: 'test' })
.required()
});

const server = await Helper.createServer({}, routes);

Expand Down
3 changes: 1 addition & 2 deletions test/Integration/path-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,9 @@ lab.experiment('path', () => {
});

lab.test('a user set content-type header removes consumes', async () => {
let consumes = ['application/json', 'application/json;charset=UTF-8', 'application/json; charset=UTF-8'];
let testRoutes = Hoek.clone(routes);
testRoutes.options.validate.headers = Joi.object({
'content-type': Joi.string().valid(consumes)
'content-type': Joi.string().valid('application/json', 'application/json;charset=UTF-8', 'application/json; charset=UTF-8')
}).unknown();

const server = await Helper.createServer({}, testRoutes);
Expand Down
1 change: 1 addition & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const helper = (module.exports = {});
helper.createServer = async (swaggerOptions, routes, serverOptions = {}) => {
const server = new Hapi.Server(serverOptions);

// eslint-disable-next-line no-useless-catch
try {
await server.register([
Inert,
Expand Down

0 comments on commit 2ea3b34

Please sign in to comment.