Skip to content

Commit

Permalink
feat(validate-greenkeeper-json): validation with joi
Browse files Browse the repository at this point in the history
  • Loading branch information
Realtin authored and hulkoba committed Mar 26, 2018
1 parent 78d1a8b commit 0932e84
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lib/validate-greenkeeper-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@ const Joi = require('joi')
// a package path is either the term 'package.json' or
// a releative path that ends in package.json
// alternitive regex: ^([^/]|p).*ackage\.json$
// const packagePathSchema = Joi.string().regex(/^(\w+\/[\w/]*)?package\.json$/)
//
// const packagesSchema = Joi.object().keys({
// packages: Joi.array().items(
// Joi.string().regex(/^(\w+\/[\w/]*)?package\.json$/)
// )
// })
const packagePathSchema = Joi.string().regex(/^(\w+\/[\w/]*)?package\.json$/)

const schema = Joi.object().keys({
groups: Joi.object().pattern(/[a-zA-Z0-9_]+/,
groups: Joi.object().pattern(/^[a-zA-Z0-9_]+$/,
Joi.object().keys({
packages: Joi.array().items(
Joi.string().regex(/^(\w+\/[\w/]*)?package\.json$/)
)
})
packages: Joi.array().items(packagePathSchema).required(),
ignore: Joi.array()
}).optionalKeys(['ignore'])
).required(),
ignore: Joi.array()
})
}).optionalKeys(['ignore'])

function validate (file) {
return Joi.validate(file, schema)
Expand Down
86 changes: 86 additions & 0 deletions test/lib/validate-greenkeeper-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,90 @@ test('valid', () => {
expect(result.error).toBeFalsy()
})

test('valid with subgroup level ignore', () => {
const file = {
groups: {
frontend: {
ignore: [
'lodash'
],
packages: [
'packages/frontend/package.json',
'packages/lalalalala/package.json'
]
},
backend: {
packages: [
'packages/backend/package.json'
]
}
}
}
const result = validate(file)
expect(result.error).toBeFalsy()
})

test('invalid: groupname has invalid characters', () => {
const file = {
groups: {
'front-end': {
ignore: [
'lodash'
],
packages: [
'packages/frontend/package.json',
'packages/lalalalala/package.json'
]
},
backend: {
packages: [
'packages/backend/package.json'
]
}
}
}
const result = validate(file)
expect(result.error).toBeTruthy()
expect(result.error.details[0].message).toMatch(/"front-end" is not allowed/)
})

test('invalid: absolute paths are not allowed', () => {
const file = {
groups: {
frontend: {
packages: [
'/packages/frontend/package.json'
]
}
}
}
const result = validate(file)
expect(result.error).toBeTruthy()
expect(result.error.name).toEqual('ValidationError')
expect(result.error.details[0].path).toEqual([ 'groups', 'frontend', 'packages', 0 ])
expect(result.error.details[0].context.value).toEqual('/packages/frontend/package.json')
expect(result.error.details[0].message).toMatch(/fails to match the required pattern/)
})

test('invalid: path is not ending on `package.json`', () => {
const file = {
groups: {
frontend: {
packages: [
'packages/frontend/package.json',
'packages/frontend/'
]
}
}
}
const result = validate(file)
expect(result.error).toBeTruthy()
expect(result.error.name).toEqual('ValidationError')
expect(result.error.details[0].path).toEqual([ 'groups', 'frontend', 'packages', 1 ])
expect(result.error.details[0].context.value).toEqual('packages/frontend/')
expect(result.error.details[0].message).toMatch(/fails to match the required pattern/)
})

test('invalid: no group/s', () => {
const file = {
frontend: {
Expand Down Expand Up @@ -58,4 +142,6 @@ test('invalid: no packages', () => {
}
const result = validate(file)
expect(result.error).toBeTruthy()
expect(result.error.name).toEqual('ValidationError')
expect(result.error.details[0].message).toMatch(/"packages" is required/)
})

0 comments on commit 0932e84

Please sign in to comment.