Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update lab and switch joi to validate #148

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Path = require('path');
const Boom = require('@hapi/boom');
const Bounce = require('@hapi/bounce');
const Hoek = require('@hapi/hoek');
const Joi = require('@hapi/joi');
const Validate = require('@hapi/validate');

const File = require('./file');
const Fs = require('./fs');
Expand All @@ -14,16 +14,16 @@ const Fs = require('./fs');
const internals = {};


internals.schema = Joi.object({
path: Joi.alternatives(Joi.array().items(Joi.string()).single(), Joi.func()).required(),
index: Joi.alternatives(Joi.boolean(), Joi.array().items(Joi.string()).single()).default(true),
listing: Joi.boolean(),
showHidden: Joi.boolean(),
redirectToSlash: Joi.boolean(),
lookupCompressed: Joi.boolean(),
lookupMap: Joi.object().min(1).pattern(/.+/, Joi.string()),
etagMethod: Joi.string().valid('hash', 'simple').allow(false),
defaultExtension: Joi.string().alphanum()
internals.schema = Validate.object({
path: Validate.alternatives(Validate.array().items(Validate.string()).single(), Validate.func()).required(),
index: Validate.alternatives(Validate.boolean(), Validate.array().items(Validate.string()).single()).default(true),
listing: Validate.boolean(),
showHidden: Validate.boolean(),
redirectToSlash: Validate.boolean(),
lookupCompressed: Validate.boolean(),
lookupMap: Validate.object().min(1).pattern(/.+/, Validate.string()),
etagMethod: Validate.string().valid('hash', 'simple').allow(false),
defaultExtension: Validate.string().alphanum()
});


Expand All @@ -47,7 +47,7 @@ internals.resolvePathOption = function (result) {

exports.handler = function (route, options) {

const settings = Joi.attempt(options, internals.schema, 'Invalid directory handler options (' + route.path + ')');
const settings = Validate.attempt(options, internals.schema, 'Invalid directory handler options (' + route.path + ')');
Hoek.assert(route.path[route.path.length - 1] === '}', 'The route path for a directory handler must end with a parameter:', route.path);

const paramName = /\w+/.exec(route.path.slice(route.path.lastIndexOf('{')))[0];
Expand Down
30 changes: 15 additions & 15 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Ammo = require('@hapi/ammo');
const Boom = require('@hapi/boom');
const Bounce = require('@hapi/bounce');
const Hoek = require('@hapi/hoek');
const Joi = require('@hapi/joi');
const Validate = require('@hapi/validate');

const Etag = require('./etag');
const Fs = require('./fs');
Expand All @@ -20,27 +20,27 @@ internals.defaultMap = {
};


internals.schema = Joi.alternatives([
Joi.string(),
Joi.func(),
Joi.object({
path: Joi.alternatives(Joi.string(), Joi.func()).required(),
confine: Joi.alternatives(Joi.string(), Joi.boolean()).default(true),
filename: Joi.string(),
mode: Joi.string().valid('attachment', 'inline').allow(false),
lookupCompressed: Joi.boolean(),
lookupMap: Joi.object().min(1).pattern(/.+/, Joi.string()),
etagMethod: Joi.string().valid('hash', 'simple').allow(false),
start: Joi.number().integer().min(0).default(0),
end: Joi.number().integer().min(Joi.ref('start'))
internals.schema = Validate.alternatives([
Validate.string(),
Validate.func(),
Validate.object({
path: Validate.alternatives(Validate.string(), Validate.func()).required(),
confine: Validate.alternatives(Validate.string(), Validate.boolean()).default(true),
filename: Validate.string(),
mode: Validate.string().valid('attachment', 'inline').allow(false),
lookupCompressed: Validate.boolean(),
lookupMap: Validate.object().min(1).pattern(/.+/, Validate.string()),
etagMethod: Validate.string().valid('hash', 'simple').allow(false),
start: Validate.number().integer().min(0).default(0),
end: Validate.number().integer().min(Validate.ref('start'))
})
.with('filename', 'mode')
]);


exports.handler = function (route, options) {

let settings = Joi.attempt(options, internals.schema, 'Invalid file handler options (' + route.path + ')');
let settings = Validate.attempt(options, internals.schema, 'Invalid file handler options (' + route.path + ')');
settings = (typeof options !== 'object' ? { path: options, confine: '.' } : settings);
settings.confine = settings.confine === true ? '.' : settings.confine;
Hoek.assert(typeof settings.path !== 'string' || settings.path[settings.path.length - 1] !== '/', 'File path cannot end with a \'/\':', route.path);
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';

const Hoek = require('@hapi/hoek');
const Joi = require('@hapi/joi');
const Validate = require('@hapi/validate');

const Directory = require('./directory');
const Etag = require('./etag');
const File = require('./file');


const internals = {
schema: Joi.object({
etagsCacheMaxSize: Joi.number().integer().min(0).default(1000)
schema: Validate.object({
etagsCacheMaxSize: Validate.number().integer().min(0).default(1000)
}).required()
};

Expand Down Expand Up @@ -41,7 +41,7 @@ exports.plugin = {
register(server, options) {

Hoek.assert(Object.keys(options).length === 0, 'Inert does not support registration options');
const settings = Joi.attempt(Hoek.reach(server.settings.plugins, 'inert') || {}, internals.schema, 'Invalid "inert" server options');
const settings = Validate.attempt(Hoek.reach(server.settings.plugins, 'inert') || {}, internals.schema, 'Invalid "inert" server options');

server.expose('_etags', settings.etagsCacheMaxSize > 0 ? new Etag.Cache(settings.etagsCacheMaxSize) : null);

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"@hapi/boom": "9.x.x",
"@hapi/bounce": "2.x.x",
"@hapi/hoek": "9.x.x",
"@hapi/joi": "17.x.x",
"@hapi/validate": "1.x.x",
"lru-cache": "5.x.x"
},
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/file": "2.x.x",
"@hapi/hapi": "19.x.x",
"@hapi/lab": "22.x.x"
"@hapi/lab": "23.x.x"
},
"scripts": {
"test": "lab -f -a @hapi/code -t 100 -L",
Expand Down