Skip to content

Commit

Permalink
Validate input to BadgeFactory.create() (#3875)
Browse files Browse the repository at this point in the history
* validate input to create()
  • Loading branch information
chris48s committed Mar 8, 2020
1 parent 1d90e10 commit edbad19
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
5 changes: 3 additions & 2 deletions gh-badges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ badge build passed :green .png > mybadge.png
### As a library

```js
const { BadgeFactory } = require('gh-badges')
const { BadgeFactory, ValidationError } = require('gh-badges')

const bf = new BadgeFactory()

Expand All @@ -31,7 +31,8 @@ const format = {
template: 'flat',
}

const svg = bf.create(format)
bf.create(format) //<svg...
bf.create({}) // ValidationError: Field `text` is required
```

### Node version support
Expand Down
53 changes: 53 additions & 0 deletions gh-badges/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

const makeBadge = require('./make-badge')

class ValidationError extends Error {}

/**
* BadgeFactory
*/
Expand All @@ -17,6 +19,56 @@ class BadgeFactory {
}
}

_validate(format) {
if (!('text' in format)) {
throw new ValidationError('Field `text` is required')
}

if (
!Array.isArray(format.text) ||
format.text.length !== 2 ||
typeof format.text[0] !== 'string' ||
typeof format.text[1] !== 'string'
) {
throw new ValidationError('Field `text` must be an array of 2 strings')
}

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

const formatValues = ['svg', 'json']
if ('format' in format && !formatValues.includes(format.format)) {
throw new ValidationError(
`Field \`format\` must be one of (${formatValues.toString()})`
)
}

const templateValues = [
'plastic',
'flat',
'flat-square',
'for-the-badge',
'popout',
'popout-square',
'social',
]
if ('template' in format && !templateValues.includes(format.template)) {
throw new ValidationError(
`Field \`template\` must be one of (${templateValues.toString()})`
)
}
}

/**
* Create a badge
*
Expand All @@ -34,6 +86,7 @@ class BadgeFactory {
* @see https://github.com/badges/shields/tree/master/gh-badges/README.md
*/
create(format) {
this._validate(format)
return makeBadge(format)
}
}
Expand Down
39 changes: 38 additions & 1 deletion gh-badges/lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

const { expect } = require('chai')
const isSvg = require('is-svg')
const { BadgeFactory } = require('.')
const { BadgeFactory, ValidationError } = require('.')

const bf = new BadgeFactory()

describe('BadgeFactory class', function() {
it('should produce badge with valid input', function() {
expect(
bf.create({
text: ['build', 'passed'],
})
).to.satisfy(isSvg)
expect(
bf.create({
text: ['build', 'passed'],
Expand All @@ -16,5 +21,37 @@ describe('BadgeFactory class', function() {
template: 'flat',
})
).to.satisfy(isSvg)
expect(
bf.create({
text: ['build', 'passed'],
foo: 'bar', // extra key
})
).to.satisfy(isSvg)
})

it('should throw a ValidationError with invalid inputs', function() {
expect(() => bf.create({})).to.throw(
ValidationError,
'Field `text` is required'
)
expect(() => bf.create({ text: ['build'] })).to.throw(
ValidationError,
'Field `text` must be an array of 2 strings'
)
expect(() =>
bf.create({ text: ['build', 'passed', 'something else'] })
).to.throw(ValidationError, 'Field `text` must be an array of 2 strings')
expect(() =>
bf.create({ text: ['build', 'passed'], labelColor: 7 })
).to.throw(ValidationError, 'Field `labelColor` must be of type string')
expect(() =>
bf.create({ text: ['build', 'passed'], format: 'png' })
).to.throw(ValidationError, 'Field `format` must be one of (svg,json)')
expect(() =>
bf.create({ text: ['build', 'passed'], template: 'something else' })
).to.throw(
ValidationError,
'Field `template` must be one of (plastic,flat,flat-square,for-the-badge,popout,popout-square,social)'
)
})
})

0 comments on commit edbad19

Please sign in to comment.