* chore(deps-dev): bump prettier from 2.8.8 to 3.0.0 Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0. - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0) --- updated-dependencies: - dependency-name: prettier dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * reformat all the things (prettier 3) * update tests to await calls to prettier.format() --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: chris48s <git@chris-shaw.dev>
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
import emojic from 'emojic'
|
|
import Joi from 'joi'
|
|
import trace from './trace.js'
|
|
|
|
function validate(
|
|
{
|
|
ErrorClass,
|
|
prettyErrorMessage = 'data does not match schema',
|
|
includeKeys = false,
|
|
traceErrorMessage = 'Data did not match schema',
|
|
traceSuccessMessage = 'Data after validation',
|
|
allowAndStripUnknownKeys = true,
|
|
},
|
|
data,
|
|
schema,
|
|
) {
|
|
if (!schema || !Joi.isSchema(schema)) {
|
|
throw Error('A Joi schema is required')
|
|
}
|
|
const options = { abortEarly: false }
|
|
if (allowAndStripUnknownKeys) {
|
|
options.allowUnknown = true
|
|
options.stripUnknown = true
|
|
}
|
|
const { error, value } = schema.validate(data, options)
|
|
if (error) {
|
|
trace.logTrace(
|
|
'validate',
|
|
emojic.womanShrugging,
|
|
traceErrorMessage,
|
|
error.message,
|
|
)
|
|
|
|
let prettyMessage = prettyErrorMessage
|
|
if (includeKeys) {
|
|
const keys = error.details.map(({ path }) => path)
|
|
if (keys) {
|
|
prettyMessage = `${prettyErrorMessage}: ${keys.join(', ')}`
|
|
}
|
|
}
|
|
|
|
throw new ErrorClass({ prettyMessage, underlyingError: error })
|
|
} else {
|
|
trace.logTrace('validate', emojic.bathtub, traceSuccessMessage, value, {
|
|
deep: true,
|
|
})
|
|
return value
|
|
}
|
|
}
|
|
|
|
export default validate
|