From 8fe74df9e052373addb0d9becad50320b54371fe Mon Sep 17 00:00:00 2001 From: Justin Stayton Date: Thu, 22 Aug 2019 09:32:25 -0400 Subject: [PATCH] Return plain object from defineValidation for Joi --- README.md | 4 ++-- src/validators/querier/joi.js | 4 +++- test/src/index.js | 8 +++----- test/src/validators/querier/joi.js | 14 ++++++-------- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8cb191a..58e6757 100644 --- a/README.md +++ b/README.md @@ -394,10 +394,10 @@ function in your querier that returns the validation schema: ```js defineValidation(schema) { - return schema.object().keys({ + return { 'filter:status[=]': schema.string().valid('open', 'closed'), 'page:size': schema.number().max(100) - }) + } } ``` diff --git a/src/validators/querier/joi.js b/src/validators/querier/joi.js index 4fb34be..590b1b1 100644 --- a/src/validators/querier/joi.js +++ b/src/validators/querier/joi.js @@ -17,7 +17,9 @@ class JoiValidator extends BaseValidator { return true } - const { error } = this.schema.validate(this.values, { allowUnknown: true }) + const { error } = Joi.object() + .keys(this.schema) + .validate(this.values, { allowUnknown: true }) if (error) { throw this.buildError(error) diff --git a/test/src/index.js b/test/src/index.js index e98d45c..77ee2af 100644 --- a/test/src/index.js +++ b/test/src/index.js @@ -149,11 +149,9 @@ describe('run', () => { test('throws `ValidationError` if querier-defined schema invalid', () => { const defineValidation = jest .spyOn(TestQuerier.prototype, 'defineValidation') - .mockImplementation(schema => - schema.object().keys({ - 'filter:test[=]': schema.string(), - }) - ) + .mockImplementation(schema => ({ + 'filter:test[=]': schema.string(), + })) const querier = new TestQuerier({ filter: { test: 123 } }, knex('test')) diff --git a/test/src/validators/querier/joi.js b/test/src/validators/querier/joi.js index 857dab1..a5662e7 100644 --- a/test/src/validators/querier/joi.js +++ b/test/src/validators/querier/joi.js @@ -49,10 +49,9 @@ describe('validate', () => { knex('test') ) - querier.defineValidation = schema => - schema.object().keys({ - 'filter:test[=]': schema.number(), - }) + querier.defineValidation = schema => ({ + 'filter:test[=]': schema.number(), + }) expect(new JoiValidator(querier).validate()).toBe(true) }) @@ -79,10 +78,9 @@ describe('validate', () => { knex('test') ) - querier.defineValidation = schema => - schema.object().keys({ - 'filter:test[=]': schema.number(), - }) + querier.defineValidation = schema => ({ + 'filter:test[=]': schema.number(), + }) expect(() => new JoiValidator(querier).validate()).toThrow( new ValidationError('filter:test[=] must be a number')