* update dependencies * (core) children --> keys * (core) fix/update BaseService validate * (core) update error messages in tests * (core) only Joi.attempt if we've got a Joi schema * (core) allow 'expected' to be a regex * (services) pass 2 schema to .alternatives() * (services) functions --> Joi schema * (services) update expected error message * (services) explicit check for color: undefined * re-bump joi * (services) wrap another regex * (core/services) remove use of array arguments * (core/services) when --> conditional * (services) remove more array arguments * fix spelling in var name * DRY up sonar helper
84 lines
2.1 KiB
JavaScript
84 lines
2.1 KiB
JavaScript
'use strict'
|
|
|
|
const Joi = require('@hapi/joi')
|
|
const validate = require('../core/base-service/validate')
|
|
const { InvalidResponse } = require('.')
|
|
|
|
const optionalStringWhenNamedLogoPresent = Joi.alternatives().conditional(
|
|
'namedLogo',
|
|
{
|
|
is: Joi.string().required(),
|
|
then: Joi.string(),
|
|
}
|
|
)
|
|
|
|
const optionalNumberWhenAnyLogoPresent = Joi.alternatives()
|
|
.conditional('namedLogo', { is: Joi.string().required(), then: Joi.number() })
|
|
.conditional('logoSvg', { is: Joi.string().required(), then: Joi.number() })
|
|
|
|
const endpointSchema = Joi.object({
|
|
schemaVersion: 1,
|
|
label: Joi.string()
|
|
.allow('')
|
|
.required(),
|
|
message: Joi.string().required(),
|
|
color: Joi.string(),
|
|
labelColor: Joi.string(),
|
|
isError: Joi.boolean().default(false),
|
|
namedLogo: Joi.string(),
|
|
logoSvg: Joi.string(),
|
|
logoColor: optionalStringWhenNamedLogoPresent,
|
|
logoWidth: optionalNumberWhenAnyLogoPresent,
|
|
logoPosition: optionalNumberWhenAnyLogoPresent,
|
|
style: Joi.string(),
|
|
cacheSeconds: Joi.number()
|
|
.integer()
|
|
.min(0),
|
|
})
|
|
// `namedLogo` or `logoSvg`; not both.
|
|
.oxor('namedLogo', 'logoSvg')
|
|
.required()
|
|
|
|
// Strictly validate according to the endpoint schema. This rejects unknown /
|
|
// invalid keys. Optionally it prints those keys in the message in order to
|
|
// provide detailed feedback.
|
|
function validateEndpointData(
|
|
data,
|
|
{ prettyErrorMessage = 'invalid response data', includeKeys = false } = {}
|
|
) {
|
|
return validate(
|
|
{
|
|
ErrorClass: InvalidResponse,
|
|
prettyErrorMessage,
|
|
includeKeys,
|
|
traceErrorMessage: 'Response did not match schema',
|
|
traceSuccessMessage: 'Response after validation',
|
|
allowAndStripUnknownKeys: false,
|
|
},
|
|
data,
|
|
endpointSchema
|
|
)
|
|
}
|
|
|
|
const anySchema = Joi.any()
|
|
|
|
async function fetchEndpointData(
|
|
serviceInstance,
|
|
{ url, errorMessages, validationPrettyErrorMessage, includeKeys }
|
|
) {
|
|
const json = await serviceInstance._requestJson({
|
|
schema: anySchema,
|
|
url,
|
|
errorMessages,
|
|
})
|
|
return validateEndpointData(json, {
|
|
prettyErrorMessage: validationPrettyErrorMessage,
|
|
includeKeys,
|
|
})
|
|
}
|
|
|
|
module.exports = {
|
|
validateEndpointData,
|
|
fetchEndpointData,
|
|
}
|