Skip to content

Commit

Permalink
Merge pull request #617 from Tornquist/joi-16
Browse files Browse the repository at this point in the history
feature: Joi 16 compatibility
  • Loading branch information
robmcguinness authored Nov 11, 2019
2 parents 9bb39ea + 3921811 commit 74fb259
Show file tree
Hide file tree
Showing 33 changed files with 1,807 additions and 1,365 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ in a project.

| Version | [hapi.js](https://github.com/hapijs/hapi) | Node | Release Notes |
| ------- | ----------------------------------------- | ----- | ------------------------------------------------------------- |
| `v11.x` | `>=18.4.0 @hapi/hapi` && >=16.0 @hapi/joi | `>=8` | |
| `v10.x` | `>=18.3.1 @hapi/hapi` | `>=8` | [#587](https://github.com/glennjones/hapi-swagger/issues/587) |
| `9.x` | `>=17 hapi` | `>=8` | [#487](https://github.com/glennjones/hapi-swagger/issues/487) |
| `7.x` | `<17 hapi` | ??? | [#325](https://github.com/glennjones/hapi-swagger/issues/325) |
Expand Down Expand Up @@ -115,7 +116,7 @@ so the the full URL for the above options would be `http://localhost:3000/docume

## Contributing

Read the [contributing guidelines](https://github.com/glennjones/hapi-swagger/blob/master/.github/CONTRIBUTING.md) for details.
Read the [contributing guidelines](./.github/CONTRIBUTING.md) for details.

## Thanks

Expand Down
67 changes: 39 additions & 28 deletions examples/assets/extendedjoi.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
const Joi = require('@hapi/joi');
const customJoi = Joi.extend(joi => ({
type: 'number',
base: joi.number(),
name: 'number',
language: {
messages: {
round: 'needs to be a rounded number', // Used below as 'number.round'
dividable: 'needs to be dividable by {{q}}'
},
pre(value, state, options) {
if (options.convert && this._flags.round) {
return Math.round(value); // Change the value
}
coerce(value, helpers) {

// Only called when prefs.convert is true

return value; // Keep the value as it was
if (helpers.schema.$_getRule('round')) {
return { value: Math.round(value) };
}
},
/*eslint-disable */
rules: [
{
name: 'round',
setup(params) {
this._flags.round = true; // Set a flag for later use
rules: {
round: {
convert: true, // Dual rule: converts or validates
method() {

return this.$_addRule('round');
},
validate(params, value, state, options) {
validate(value, helpers, args, options) {

// Only called when prefs.convert is false (due to rule convert option)

if (value % 1 !== 0) {
// Generate an error, state and options need to be passed
return this.createError('number.round', { v: value }, state, options);
return helpers.error('number.round');
}

return value; // Everything is OK
}
},
{
name: 'dividable',
params: {
q: joi.alternatives([joi.number().required(), joi.func().ref()])
dividable: {
multi: true, // Rule supports multiple invocations
method(q) {

return this.$_addRule({ name: 'dividable', args: { q } });
},
validate(params, value, state, options) {
if (value % params.q !== 0) {
// Generate an error, state and options need to be passed, q is used in the language
return this.createError('number.dividable', { v: value, q: params.q }, state, options);
args: [
{
name: 'q',
ref: true,
assert: (value) => typeof value === 'number' && !isNaN(value),
message: 'must be a number'
}
],
validate(value, helpers, args, options) {

if (value % args.q === 0) {
return value; // Value is valid
}

return value; // Everything is OK
return helpers.error('number.dividable', { q: args.q });
}
}
]
}
/*eslint-enable */
}));
}))

module.exports = customJoi;
68 changes: 34 additions & 34 deletions examples/assets/routes-complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ module.exports = [
description: 'String properties',
tags: ['api'],
validate: {
query: {
query: Joi.object({
a: Joi.string()
.min(5)
.description('min'),
Expand Down Expand Up @@ -244,7 +244,7 @@ module.exports = [
r: Joi.string()
.regex(/^[a-zA-Z0-9]{3,30}/)
.description('regex')
}
})
}
}
},
Expand All @@ -256,7 +256,7 @@ module.exports = [
description: 'Number properties',
tags: ['api'],
validate: {
query: {
query: Joi.object({
a: Joi.number()
.min(5)
.description('min'),
Expand Down Expand Up @@ -293,7 +293,7 @@ module.exports = [
.integer()
.positive()
.allow(0)
}
})
}
}
},
Expand All @@ -305,7 +305,7 @@ module.exports = [
description: 'Array properties',
tags: ['api'],
validate: {
query: {
query: Joi.object({
a: Joi.array()
.min(5)
.description('min'),
Expand All @@ -324,7 +324,7 @@ module.exports = [
g: Joi.array()
.unique()
.description('unique')
}
})
}
}
},
Expand All @@ -345,15 +345,15 @@ module.exports = [
},
auth: 'bearer',
validate: {
params: {
params: Joi.object({
a: Joi.number()
.required()
.description('the first number'),

b: Joi.number()
.required()
.description('the second number')
}
})
}
}
},
Expand All @@ -371,15 +371,15 @@ module.exports = [
}
},
validate: {
params: {
params: Joi.object({
a: Joi.number()
.required()
.description('the first number'),

b: Joi.number()
.required()
.description('the second number')
}
})
}
}
},
Expand All @@ -398,15 +398,15 @@ module.exports = [
}
},
validate: {
params: {
params: Joi.object({
a: Joi.number()
.required()
.description('the first number - can NOT be 0'),

b: Joi.number()
.required()
.description('the second number - can NOT be 0')
}
})
}
}
},
Expand All @@ -425,15 +425,15 @@ module.exports = [
},
tags: ['api'],
validate: {
params: {
params: Joi.object({
a: Joi.number()
.required()
.description('the first number'),

b: Joi.number()
.required()
.description('the second number')
}
})
}
}
},
Expand All @@ -451,11 +451,11 @@ module.exports = [
},
tags: ['api', 'reduced', 'one'],
validate: {
query: {
query: Joi.object({
page: Joi.number().description('the page number'),

pagesize: Joi.number().description('the number of items to a page')
}
})
}
}
},
Expand All @@ -473,11 +473,11 @@ module.exports = [
},
tags: ['api', 'reduced', 'two'],
validate: {
params: {
params: Joi.object({
id: Joi.string()
.required()
.description('the id of the sum in the store')
}
})
}
}
},
Expand All @@ -498,7 +498,7 @@ module.exports = [
},
tags: ['api', 'reduced', 'three'],
validate: {
payload: {
payload: Joi.object({
a: Joi.number()
.required()
.description('the first number')
Expand All @@ -511,13 +511,13 @@ module.exports = [
operator: Joi.string()
.required()
.default('+')
.valid(['+', '-', '/', '*'])
.valid('+', '-', '/', '*')
.description('the i.e. + - / or *'),

equals: Joi.number()
.required()
.description('the result of the sum')
}
})
}
}
},
Expand All @@ -536,12 +536,12 @@ module.exports = [
},
tags: ['api'],
validate: {
params: {
params: Joi.object({
id: Joi.string()
.required()
.description('the id of the sum in the store')
},
payload: {
}),
payload: Joi.object({
a: Joi.number()
.required()
.description('the first number'),
Expand All @@ -553,13 +553,13 @@ module.exports = [
operator: Joi.string()
.required()
.default('+')
.valid(['+', '-', '/', '*'])
.valid('+', '-', '/', '*')
.description('the operator i.e. + - / or *'),

equals: Joi.number()
.required()
.description('the result of the sum')
}
})
}
}
},
Expand All @@ -577,11 +577,11 @@ module.exports = [
},
tags: ['api'],
validate: {
params: {
params: Joi.object({
id: Joi.string()
.required()
.description('the id of the sum in the store')
}
})
}
}
},
Expand All @@ -598,7 +598,7 @@ module.exports = [
}
},
tags: ['api', 'reduced', 'three'],
validate: {
validate: Joi.object({
payload: Joi.object({
a: Joi.number()
.required()
Expand All @@ -611,7 +611,7 @@ module.exports = [
operator: Joi.string()
.required()
.default('+')
.valid(['+', '-', '/', '*'])
.valid('+', '-', '/', '*')
.description('the operator i.e. + - / or *'),

equals: Joi.number()
Expand All @@ -621,10 +621,10 @@ module.exports = [
headers: Joi.object({
accept: Joi.string()
.required()
.valid(['application/json', 'application/vnd.api+json'])
.valid('application/json', 'application/vnd.api+json')
.default('application/vnd.api+json')
}).unknown()
}
})
}
},
{
Expand All @@ -644,12 +644,12 @@ module.exports = [
},
tags: ['api', 'reduced', 'three'],
validate: {
payload: {
payload: Joi.object({
file: Joi.any()
.meta({ swaggerType: 'file' })
.required()
.description('json file with object containing: a, b, operator and equals')
}
})
},
payload: {
maxBytes: 1048576,
Expand Down
Loading

0 comments on commit 74fb259

Please sign in to comment.