Skip to content

Commit

Permalink
fix issue with custom logo generation (#9644)
Browse files Browse the repository at this point in the history
* allow `logo`, `logoPosition`, `logoWidth`, and `links` as expected keys

* add validation for `logo`, `logoPosition`, `logoWidth`, and `links` keys

* add validation tests
  • Loading branch information
zavoloklom committed Oct 8, 2023
1 parent 075ead0 commit d481672
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
4 changes: 4 additions & 0 deletions badge-maker/index.d.ts
Expand Up @@ -4,6 +4,10 @@ interface Format {
labelColor?: string
color?: string
style?: 'plastic' | 'flat' | 'flat-square' | 'for-the-badge' | 'social'
logo?: string
logoPosition?: number
logoWidth?: number
links?: Array<string>
}

export declare class ValidationError extends Error {}
Expand Down
39 changes: 36 additions & 3 deletions badge-maker/lib/index.js
Expand Up @@ -16,13 +16,32 @@ function _validate(format) {
throw new ValidationError('Field `message` is required')
}

const stringFields = ['labelColor', 'color', 'message', 'label']
const stringFields = ['labelColor', 'color', 'message', 'label', 'logo']
stringFields.forEach(function (field) {
if (field in format && typeof format[field] !== 'string') {
throw new ValidationError(`Field \`${field}\` must be of type string`)
}
})

const numberFields = ['logoWidth', 'logoPosition']
numberFields.forEach(function (field) {
if (field in format && typeof format[field] !== 'number') {
throw new ValidationError(`Field \`${field}\` must be of type number`)
}
})

if ('links' in format) {
if (!Array.isArray(format.links)) {
throw new ValidationError('Field `links` must be an array of strings')
} else {
format.links.forEach(function (field) {
if (typeof field !== 'string') {
throw new ValidationError('Field `links` must be an array of strings')
}
})
}
}

const styleValues = [
'plastic',
'flat',
Expand All @@ -38,7 +57,17 @@ function _validate(format) {
}

function _clean(format) {
const expectedKeys = ['label', 'message', 'labelColor', 'color', 'style']
const expectedKeys = [
'label',
'message',
'labelColor',
'color',
'style',
'logo',
'logoPosition',
'logoWidth',
'links',
]

const cleaned = {}
Object.keys(format).forEach(key => {
Expand All @@ -65,7 +94,11 @@ function _clean(format) {
* @param {string} format.message (Required) Badge message (e.g: 'passing')
* @param {string} format.labelColor (Optional) Label color
* @param {string} format.color (Optional) Message color
* @param {string} format.style (Optional) Visual style e.g: 'flat'
* @param {string} format.style (Optional) Visual style (e.g: 'flat')
* @param {string} format.logo (Optional) Logo data URL
* @param {number} format.logoPosition (Optional) Logo position (e.g: 40)
* @param {number} format.logoWidth (Optional) Logo width (e.g: 40)
* @param {Array} format.links (Optional) Links array (e.g: ['https://example.com', 'https://example.com'])
* @returns {string} Badge in SVG format
* @see https://github.com/badges/shields/tree/master/badge-maker/README.md
*/
Expand Down
28 changes: 28 additions & 0 deletions badge-maker/lib/index.spec.js
Expand Up @@ -25,6 +25,19 @@ describe('makeBadge function', function () {
style: 'flat',
}),
).to.satisfy(isSvg)
expect(
makeBadge({
label: 'build',
message: 'passed',
color: 'green',
style: 'flat',
labelColor: 'blue',
logo: 'data:image/svg+xml;base64,PHN2ZyB4bWxu',
logoWidth: 20,
logoPosition: 10,
links: ['https://example.com', 'https://example.com'],
}),
).to.satisfy(isSvg)
})

it('should throw a ValidationError with invalid inputs', function () {
Expand All @@ -46,6 +59,21 @@ describe('makeBadge function', function () {
expect(() =>
makeBadge({ label: 'build', message: 'passed', labelColor: 7 }),
).to.throw(ValidationError, 'Field `labelColor` must be of type string')
expect(() =>
makeBadge({ label: 'build', message: 'passed', logo: 7 }),
).to.throw(ValidationError, 'Field `logo` must be of type string')
expect(() =>
makeBadge({ label: 'build', message: 'passed', logoPosition: '7' }),
).to.throw(ValidationError, 'Field `logoPosition` must be of type number')
expect(() =>
makeBadge({ label: 'build', message: 'passed', logoWidth: '7' }),
).to.throw(ValidationError, 'Field `logoWidth` must be of type number')
expect(() =>
makeBadge({ label: 'build', message: 'passed', links: 'test' }),
).to.throw(ValidationError, 'Field `links` must be an array of strings')
expect(() =>
makeBadge({ label: 'build', message: 'passed', links: [1] }),
).to.throw(ValidationError, 'Field `links` must be an array of strings')
expect(() =>
makeBadge({ label: 'build', message: 'passed', format: 'png' }),
).to.throw(ValidationError, "Unexpected field 'format'")
Expand Down

0 comments on commit d481672

Please sign in to comment.