Skip to content

Commit

Permalink
Return plain object from defineValidation for Joi
Browse files Browse the repository at this point in the history
  • Loading branch information
jstayton committed Aug 22, 2019
1 parent 05de9b9 commit 8fe74df
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
```

Expand Down
4 changes: 3 additions & 1 deletion src/validators/querier/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions test/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand Down
14 changes: 6 additions & 8 deletions test/src/validators/querier/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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')
Expand Down

0 comments on commit 8fe74df

Please sign in to comment.