Skip to content

Commit

Permalink
Replace joi with validate
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse authored and cjihrig committed Aug 10, 2020
1 parent f7e9193 commit f46f864
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 261 deletions.
450 changes: 225 additions & 225 deletions lib/config.js

Large diffs are not rendered by default.

30 changes: 5 additions & 25 deletions lib/route.js
Expand Up @@ -6,8 +6,8 @@ const Boom = require('@hapi/boom');
const Bounce = require('@hapi/bounce');
const Catbox = require('@hapi/catbox');
const Hoek = require('@hapi/hoek');
const Joi = require('@hapi/joi');
const Subtext = require('@hapi/subtext');
const Validate = require('@hapi/validate');

const Auth = require('./auth');
const Config = require('./config');
Expand Down Expand Up @@ -192,10 +192,8 @@ exports = module.exports = internals.Route = class {

this._assert(!validation.params || this.params.length, 'Cannot set path parameters validations without path parameters');

const validator = this._validator();

for (const type of ['headers', 'params', 'query', 'payload', 'state']) {
validation[type] = Validation.compile(validation[type], validator);
validation[type] = Validation.compile(validation[type], this.settings.validate.validator, this.realm, this._core);
}

if (this.settings.response.schema !== undefined ||
Expand All @@ -213,9 +211,9 @@ exports = module.exports = internals.Route = class {
this.settings.response._validate = false;
}
else {
this.settings.response.schema = Validation.compile(rule, validator);
this.settings.response.schema = Validation.compile(rule, this.settings.validate.validator, this.realm, this._core);
for (const code of statuses) {
this.settings.response.status[code] = Validation.compile(this.settings.response.status[code], validator);
this.settings.response.status[code] = Validation.compile(this.settings.response.status[code], this.settings.validate.validator, this.realm, this._core);
}
}
}
Expand Down Expand Up @@ -375,24 +373,6 @@ exports = module.exports = internals.Route = class {
stackStartFunction: this._assert
});
}

_validator() {

if (this.settings.validate.validator) {
return this.settings.validate.validator;
}

let realm = this.realm;
while (realm) {
if (realm.validator) {
return realm.validator;
}

realm = realm.parent;
}

return this._core.validator;
}
};


Expand Down Expand Up @@ -528,7 +508,7 @@ internals.rules = function (rules, info, server) {
let realm = server.realm;
while (realm) {
if (realm._rules) {
const source = !realm._rules.settings.validate ? rules : Joi.attempt(rules, realm._rules.settings.validate.schema, realm._rules.settings.validate.options);
const source = !realm._rules.settings.validate ? rules : Validate.attempt(rules, realm._rules.settings.validate.schema, realm._rules.settings.validate.options);
const config = realm._rules.processor(source, info);
if (config) {
configs.unshift(config);
Expand Down
3 changes: 1 addition & 2 deletions lib/server.js
@@ -1,7 +1,6 @@
'use strict';

const Hoek = require('@hapi/hoek');
const Joi = require('@hapi/joi');
const Shot = require('@hapi/shot');
const Somever = require('@hapi/somever');
const Teamwork = require('@hapi/teamwork');
Expand Down Expand Up @@ -534,7 +533,7 @@ internals.Server = class {
const settings = Config.apply('rules', options);
if (settings.validate) {
const schema = settings.validate.schema;
settings.validate.schema = Joi.compile(schema);
settings.validate.schema = Validation.compile(schema, null, this.realm, this._core);
}

this.realm._rules = { processor, settings };
Expand Down
22 changes: 19 additions & 3 deletions lib/validation.js
Expand Up @@ -2,7 +2,7 @@

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


const internals = {};
Expand All @@ -17,12 +17,14 @@ exports.validator = function (validator) {
};


exports.compile = function (rule, validator) {
exports.compile = function (rule, validator, realm, core) {

validator = validator || internals.validator(realm, core);

// false - nothing allowed

if (rule === false) {
return Joi.object({}).allow(null);
return Validate.object({}).allow(null);
}

// Custom function
Expand Down Expand Up @@ -50,6 +52,20 @@ exports.compile = function (rule, validator) {
};


internals.validator = function (realm, core) {

while (realm) {
if (realm.validator) {
return realm.validator;
}

realm = realm.parent;
}

return core.validator;
};


exports.headers = function (request) {

return internals.input('headers', request);
Expand Down
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -27,15 +27,15 @@
"@hapi/catbox-memory": "5.x.x",
"@hapi/heavy": "7.x.x",
"@hapi/hoek": "9.x.x",
"@hapi/joi": "17.x.x",
"@hapi/mimos": "5.x.x",
"@hapi/podium": "4.x.x",
"@hapi/shot": "5.x.x",
"@hapi/somever": "3.x.x",
"@hapi/statehood": "^7.0.2",
"@hapi/subtext": "^7.0.3",
"@hapi/teamwork": "4.x.x",
"@hapi/topo": "5.x.x"
"@hapi/topo": "5.x.x",
"@hapi/validate": "^1.1.0"
},
"devDependencies": {
"@hapi/code": "8.x.x",
Expand All @@ -44,7 +44,8 @@
"@hapi/lab": "22.x.x",
"@hapi/wreck": "17.x.x",
"@hapi/vision": "6.x.x",
"handlebars": "4.x.x"
"handlebars": "4.x.x",
"joi": "17.x.x"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L -m 5000",
Expand Down
2 changes: 1 addition & 1 deletion test/request.js
Expand Up @@ -9,7 +9,7 @@ const Boom = require('@hapi/boom');
const Code = require('@hapi/code');
const Hapi = require('..');
const Hoek = require('@hapi/hoek');
const Joi = require('@hapi/joi');
const Joi = require('joi');
const Lab = require('@hapi/lab');
const Teamwork = require('@hapi/teamwork');
const Wreck = require('@hapi/wreck');
Expand Down
2 changes: 1 addition & 1 deletion test/route.js
Expand Up @@ -5,7 +5,7 @@ const Path = require('path');
const Code = require('@hapi/code');
const Hapi = require('..');
const Inert = require('@hapi/inert');
const Joi = require('@hapi/joi');
const Joi = require('joi');
const Lab = require('@hapi/lab');
const Subtext = require('@hapi/subtext');

Expand Down
2 changes: 1 addition & 1 deletion test/validation.js
Expand Up @@ -4,7 +4,7 @@ const Boom = require('@hapi/boom');
const Code = require('@hapi/code');
const Hapi = require('..');
const Inert = require('@hapi/inert');
const Joi = require('@hapi/joi');
const Joi = require('joi');
const JoiLegacy = require('@hapi/joi-legacy-test');
const Lab = require('@hapi/lab');

Expand Down

0 comments on commit f46f864

Please sign in to comment.