added tests for linter module
parent
95dbfc31b6
commit
6d15afd8e3
|
@ -5,25 +5,28 @@ const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
|
||||||
|
|
||||||
function required() {
|
function required() {
|
||||||
return (path, input, msgs) => {
|
return (path, input, msgs) => {
|
||||||
|
input = input && input.trim()
|
||||||
if(!input) {
|
if(!input) {
|
||||||
msgs.push(`Property '${path}' must be set! value=${input}`)
|
msgs.push(`Property '${path}' must be set! value=${input}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function length(min, max=-1) {
|
function length(opts) {
|
||||||
return (path, input, msgs) => {
|
return (path, input, msgs) => {
|
||||||
if(min > 0 && input.length < min) {
|
input = input && input.trim() || ''
|
||||||
msgs.push(`Property '${path}' must have at least ${min} characters! actual=${input.length}`)
|
if(opts.min && input.length < opts.min) {
|
||||||
|
msgs.push(`Property '${path}' must have at least ${opts.min} characters! actual=${input.length}`)
|
||||||
}
|
}
|
||||||
if(max > 0 && input.length > max) {
|
if(opts.max && input.length > opts.max) {
|
||||||
msgs.push(`Property '${path}' must not have more than ${max} characters! actual=${input.length}`)
|
msgs.push(`Property '${path}' must not have more than ${opts.max} characters! actual=${input.length}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function regex(regex) {
|
function regex(regex) {
|
||||||
return (path, input, msgs) => {
|
return (path, input, msgs) => {
|
||||||
|
input = input && input.trim()
|
||||||
if(!input.match(regex)) {
|
if(!input.match(regex)) {
|
||||||
msgs.push(`Property '${path}' must only comply to pattern ${regex}! value=${input}`)
|
msgs.push(`Property '${path}' must only comply to pattern ${regex}! value=${input}`)
|
||||||
}
|
}
|
||||||
|
@ -32,8 +35,8 @@ function regex(regex) {
|
||||||
|
|
||||||
|
|
||||||
const linterMappings = [
|
const linterMappings = [
|
||||||
{ path: 'name', linters: [ required(), length(4), regex(NAME_REGEX) ] },
|
{ path: 'name', linters: [ required(), length({ min: 4 }), regex(NAME_REGEX) ] },
|
||||||
{ path: 'description', linters: [ required(), length(4), regex(DESCRIPTION_REGEX) ] }
|
{ path: 'description', linters: [ required(), length({ min: 4 }), regex(DESCRIPTION_REGEX) ] }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,4 +54,10 @@ module.exports.lint = function(spec) {
|
||||||
module.exports.regex = {
|
module.exports.regex = {
|
||||||
NAME_REGEX,
|
NAME_REGEX,
|
||||||
DESCRIPTION_REGEX
|
DESCRIPTION_REGEX
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.validators = {
|
||||||
|
required,
|
||||||
|
length,
|
||||||
|
regex,
|
||||||
}
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
const assert = require('assert').strict
|
||||||
|
const logging = require('../src/logging')
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const linter = require('../src/linter')
|
||||||
|
|
||||||
|
logging.init(-1)
|
||||||
|
|
||||||
|
describe('linter validators', () => {
|
||||||
|
it('creates an error on required property', () => {
|
||||||
|
let msg = []
|
||||||
|
linter.validators.required()('path', '', msg)
|
||||||
|
linter.validators.required()('path', null, msg)
|
||||||
|
linter.validators.required()('path', undefined, msg)
|
||||||
|
linter.validators.required()('path', ' ', msg)
|
||||||
|
|
||||||
|
expect(msg).to.not.be.empty
|
||||||
|
expect(msg.length).to.equal(4)
|
||||||
|
})
|
||||||
|
it('creates an error on to small strings for length validator', () => {
|
||||||
|
let msg = []
|
||||||
|
linter.validators.length({ min: 4 })('path', '', msg)
|
||||||
|
linter.validators.length({ min: 4 })('path', null, msg)
|
||||||
|
linter.validators.length({ min: 4 })('path', undefined, msg)
|
||||||
|
linter.validators.length({ min: 4 })('path', '123', msg)
|
||||||
|
|
||||||
|
expect(msg).to.not.be.empty
|
||||||
|
expect(msg.length).to.equal(4)
|
||||||
|
})
|
||||||
|
it('creates an error on too large strings for length validator', () => {
|
||||||
|
let msg = []
|
||||||
|
linter.validators.length({ max: 4 })('path', '', msg)
|
||||||
|
linter.validators.length({ max: 4 })('path', null, msg)
|
||||||
|
linter.validators.length({ max: 4 })('path', undefined, msg)
|
||||||
|
linter.validators.length({ min: 4 })('path', '12345', msg)
|
||||||
|
linter.validators.length({ min: 4 })('path', ' 1 ', msg)
|
||||||
|
|
||||||
|
expect(msg).to.not.be.empty
|
||||||
|
expect(msg.length).to.equal(1)
|
||||||
|
})
|
||||||
|
it('creates an error when string does not match regex', () => {
|
||||||
|
let msg = []
|
||||||
|
linter.validators.regex(/[0-9]+/)('path', 'abcdef', msg)
|
||||||
|
linter.validators.regex(/[0-9]+/)('path', ' ', msg)
|
||||||
|
linter.validators.regex(/.+/)('path', ' ', msg)
|
||||||
|
|
||||||
|
expect(msg).to.not.be.empty
|
||||||
|
expect(msg.length).to.equal(3)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue