refactor linter validators
parent
f0334c6118
commit
66dd66959f
|
@ -1,6 +1,7 @@
|
|||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const os = require('os')
|
||||
const linter = require('../linter')
|
||||
|
||||
const internal_templates = [
|
||||
'default.json',
|
||||
|
@ -93,7 +94,6 @@ function prepare(config) {
|
|||
prepareUserDir(config)
|
||||
prepareData(config)
|
||||
checkPreconditions(config)
|
||||
|
||||
console.trace('config:', config)
|
||||
return config
|
||||
}
|
||||
|
@ -114,13 +114,18 @@ module.exports = {
|
|||
'--info <info-path>': "Path to an info template. (optional)",
|
||||
'--annotations <annotations-path>': "Path to an annotations template. (optional)",
|
||||
'--out <output-path>': "Path to output the generated template. (optional)",
|
||||
'--quiet, -q': "Do not create the spec file, just output it to stdout. (optional)"
|
||||
'--quiet, -q': "Do not create the spec file, just output it to stdout. (optional)",
|
||||
'--skip-linting': "Skip the linting process. (optional)"
|
||||
}
|
||||
},
|
||||
run(args) {
|
||||
let config = prepare({ args })
|
||||
let spec = generateTemplate(config)
|
||||
|
||||
if(!args['skip-linting']) {
|
||||
linter.lint(spec)
|
||||
}
|
||||
|
||||
if(!args.q && !args.quiet) {
|
||||
writeToFile(config, spec)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
module.exports = {
|
||||
run(args) {
|
||||
|
||||
}
|
||||
}
|
|
@ -4,22 +4,22 @@ const NAME_REGEX = /^[a-z0-9_-]+$/;
|
|||
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
|
||||
|
||||
function required() {
|
||||
return (path, input, msgs) => {
|
||||
return (path, input) => {
|
||||
input = input && input.trim()
|
||||
if(!input) {
|
||||
msgs.push(`Property '${path}' must be set! value=${input}`)
|
||||
return `Property '${path}' must be set! value=${input}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function length(opts) {
|
||||
return (path, input, msgs) => {
|
||||
return (path, input) => {
|
||||
input = input && input.trim() || ''
|
||||
if(opts.min && input.length < opts.min) {
|
||||
msgs.push(`Property '${path}' must have at least ${opts.min} characters! actual=${input.length}`)
|
||||
return `Property '${path}' must have at least ${opts.min} characters! actual=${input.length}`
|
||||
}
|
||||
if(opts.max && input.length > opts.max) {
|
||||
msgs.push(`Property '${path}' must not have more than ${opts.max} characters! actual=${input.length}`)
|
||||
return `Property '${path}' must not have more than ${opts.max} characters! actual=${input.length}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ function regex(regex) {
|
|||
return (path, input, msgs) => {
|
||||
input = input && input.trim()
|
||||
if(!input.match(regex)) {
|
||||
msgs.push(`Property '${path}' must only comply to pattern ${regex}! value=${input}`)
|
||||
return `Property '${path}' must only comply to pattern ${regex}! value=${input}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,43 +7,27 @@ 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)
|
||||
expect(linter.validators.required()('path', '')).to.not.be.empty
|
||||
expect(linter.validators.required()('path', null)).to.not.be.empty
|
||||
expect(linter.validators.required()('path', undefined)).to.not.be.empty
|
||||
expect(linter.validators.required()('path', ' ')).to.not.be.empty
|
||||
})
|
||||
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)
|
||||
expect(linter.validators.length({ min: 4 })('path', '')).to.not.be.empty
|
||||
expect(linter.validators.length({ min: 4 })('path', null)).to.not.be.empty
|
||||
expect(linter.validators.length({ min: 4 })('path', undefined)).to.not.be.empty
|
||||
expect(linter.validators.length({ min: 4 })('path', '123')).to.not.be.empty
|
||||
})
|
||||
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)
|
||||
expect(linter.validators.length({ max: 4 })('path', '')).to.be.undefined
|
||||
expect(linter.validators.length({ max: 4 })('path', null)).to.be.undefined
|
||||
expect(linter.validators.length({ max: 4 })('path', undefined)).to.be.undefined
|
||||
expect(linter.validators.length({ max: 4 })('path', '12345')).to.not.be.empty
|
||||
expect(linter.validators.length({ max: 4 })('path', ' 1 ')).to.be.undefined
|
||||
})
|
||||
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)
|
||||
expect(linter.validators.regex(/[0-9]+/)('path', 'abcdef')).to.not.be.empty
|
||||
expect(linter.validators.regex(/[0-9]+/)('path', ' ')).to.not.be.empty
|
||||
expect(linter.validators.regex(/.+/)('path', ' ')).to.not.be.empty
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue