Files
shields/services/dynamic-common.js
Paul Melnikow 382af10506 Rewrite [GithubManifest] and [GithubPackageJson] badges (#2470)
Pave the way for #2259 and rewrite #1721 along the way.

Ref: #2320
2018-12-19 16:33:20 -05:00

54 lines
1.2 KiB
JavaScript

'use strict'
const Joi = require('joi')
const validate = require('../lib/validate')
const { toArray } = require('../lib/badge-data')
const { InvalidResponse } = require('./errors')
const individualValueSchema = Joi.alternatives()
.try(Joi.string(), Joi.number())
.required()
const compoundValueSchema = Joi.alternatives().try(
individualValueSchema,
Joi.array()
.items(individualValueSchema)
.required(),
Joi.array().length(0)
)
function transformAndValidate({ data, key }) {
return validate(
{
ErrorClass: InvalidResponse,
prettyErrorMessage: 'invalid key value',
traceErrorMessage: 'Key value not valid for dynamic badge',
traceSuccessMessage: 'Key value after validation',
},
data[key],
compoundValueSchema
)
}
function renderDynamicBadge({
defaultLabel,
tag,
value,
prefix = '',
suffix = '',
}) {
const renderedValue =
value === undefined ? 'not specified' : toArray(value).join(', ')
return {
label: tag ? `${defaultLabel}@${tag}` : defaultLabel,
message: `${prefix}${renderedValue}${suffix}`,
color: 'blue',
}
}
module.exports = {
individualValueSchema,
transformAndValidate,
renderDynamicBadge,
}