I'm building a JOI extension to allow me to blacklist certain people from sending certain API values, if they are missing certain roles from their JWT scope.
So far I've go this:
const Joi = require('joi')
const { string, number, ref, required, only, lazy } = Joi.extend(joi => ({
name: 'string',
base: Joi.string(),
language: {
permits: 'you are not allowed to edit {{key}}'
},
pre (value, state, options) {
this.permissions = options.context.auth.credentials.scope
},
rules: [{
name: 'whitelist',
params: {
permits: Joi.array()
},
validate(params, value, state, options) {
const permitted = params.permits.find(value => this.permissions.includes(value))
return permitted ? value : this.createError('string.permits', {}, state, options)
}
}]
}))
Which works perfectly.
However, Note the name and base are set to 'string'. I want this to work for numbers, arrays, objects, you name it.
I've tried this:
name: 'any',
base: Joi.any()
but it doesn't seem to work:
/home/ant/Projects/roles-example/routes/validation.routes.js:55
reference: string().whitelist(['all-loans']),
^
TypeError: string(...).whitelist is not a function
I would assume that any would allow me to append the function to any other type within JOI. But it seems that I can't.
Does anyone have any pointers for me, before I have to start adding this to all of the JOI base types?