Files
shields/lib/validate.js
Paul Melnikow 282520041d Add [GitlabPipeline] badge (#2325)
There's a lot of demand for the Gitlab badges (#541) and the PR has been lingering, so I thought I'd start off one of the simple ones as a new-style service. This one is SVG-based, so it shouldn't require the API-token logic which could use some more testing and will require us to create an app and configure it on our server.

We don't have any validation in place for `queryParams`. Probably this should be added to BaseService, though for the time being I extracted a helper function.

Thanks to @LVMBDV for getting this work started in #1838!
2018-11-18 10:06:47 -05:00

44 lines
934 B
JavaScript

'use strict'
const emojic = require('emojic')
const Joi = require('joi')
const trace = require('../services/trace')
function validate(
{
ErrorClass,
prettyErrorMessage = 'data does not match schema',
traceErrorMessage = 'Data did not match schema',
traceSuccessMessage = 'Data after validation',
},
data,
schema
) {
if (!schema || !schema.isJoi) {
throw Error('A Joi schema is required')
}
const { error, value } = Joi.validate(data, schema, {
allowUnknown: true,
stripUnknown: true,
})
if (error) {
trace.logTrace(
'validate',
emojic.womanShrugging,
traceErrorMessage,
error.message
)
throw new ErrorClass({
prettyMessage: prettyErrorMessage,
underlyingError: error,
})
} else {
trace.logTrace('validate', emojic.bathtub, traceSuccessMessage, value, {
deep: true,
})
return value
}
}
module.exports = validate