refactor linter validators
parent
f0334c6118
commit
66dd66959f
|
@ -1,6 +1,7 @@
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
|
const linter = require('../linter')
|
||||||
|
|
||||||
const internal_templates = [
|
const internal_templates = [
|
||||||
'default.json',
|
'default.json',
|
||||||
|
@ -93,7 +94,6 @@ function prepare(config) {
|
||||||
prepareUserDir(config)
|
prepareUserDir(config)
|
||||||
prepareData(config)
|
prepareData(config)
|
||||||
checkPreconditions(config)
|
checkPreconditions(config)
|
||||||
|
|
||||||
console.trace('config:', config)
|
console.trace('config:', config)
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
@ -114,13 +114,18 @@ module.exports = {
|
||||||
'--info <info-path>': "Path to an info template. (optional)",
|
'--info <info-path>': "Path to an info template. (optional)",
|
||||||
'--annotations <annotations-path>': "Path to an annotations template. (optional)",
|
'--annotations <annotations-path>': "Path to an annotations template. (optional)",
|
||||||
'--out <output-path>': "Path to output the generated 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) {
|
run(args) {
|
||||||
let config = prepare({ args })
|
let config = prepare({ args })
|
||||||
let spec = generateTemplate(config)
|
let spec = generateTemplate(config)
|
||||||
|
|
||||||
|
if(!args['skip-linting']) {
|
||||||
|
linter.lint(spec)
|
||||||
|
}
|
||||||
|
|
||||||
if(!args.q && !args.quiet) {
|
if(!args.q && !args.quiet) {
|
||||||
writeToFile(config, spec)
|
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}.+\.$/
|
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
|
||||||
|
|
||||||
function required() {
|
function required() {
|
||||||
return (path, input, msgs) => {
|
return (path, input) => {
|
||||||
input = input && input.trim()
|
input = input && input.trim()
|
||||||
if(!input) {
|
if(!input) {
|
||||||
msgs.push(`Property '${path}' must be set! value=${input}`)
|
return `Property '${path}' must be set! value=${input}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function length(opts) {
|
function length(opts) {
|
||||||
return (path, input, msgs) => {
|
return (path, input) => {
|
||||||
input = input && input.trim() || ''
|
input = input && input.trim() || ''
|
||||||
if(opts.min && input.length < opts.min) {
|
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) {
|
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) => {
|
return (path, input, msgs) => {
|
||||||
input = input && input.trim()
|
input = input && input.trim()
|
||||||
if(!input.match(regex)) {
|
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', () => {
|
describe('linter validators', () => {
|
||||||
it('creates an error on required property', () => {
|
it('creates an error on required property', () => {
|
||||||
let msg = []
|
expect(linter.validators.required()('path', '')).to.not.be.empty
|
||||||
linter.validators.required()('path', '', msg)
|
expect(linter.validators.required()('path', null)).to.not.be.empty
|
||||||
linter.validators.required()('path', null, msg)
|
expect(linter.validators.required()('path', undefined)).to.not.be.empty
|
||||||
linter.validators.required()('path', undefined, msg)
|
expect(linter.validators.required()('path', ' ')).to.not.be.empty
|
||||||
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', () => {
|
it('creates an error on to small strings for length validator', () => {
|
||||||
let msg = []
|
expect(linter.validators.length({ min: 4 })('path', '')).to.not.be.empty
|
||||||
linter.validators.length({ min: 4 })('path', '', msg)
|
expect(linter.validators.length({ min: 4 })('path', null)).to.not.be.empty
|
||||||
linter.validators.length({ min: 4 })('path', null, msg)
|
expect(linter.validators.length({ min: 4 })('path', undefined)).to.not.be.empty
|
||||||
linter.validators.length({ min: 4 })('path', undefined, msg)
|
expect(linter.validators.length({ min: 4 })('path', '123')).to.not.be.empty
|
||||||
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', () => {
|
it('creates an error on too large strings for length validator', () => {
|
||||||
let msg = []
|
expect(linter.validators.length({ max: 4 })('path', '')).to.be.undefined
|
||||||
linter.validators.length({ max: 4 })('path', '', msg)
|
expect(linter.validators.length({ max: 4 })('path', null)).to.be.undefined
|
||||||
linter.validators.length({ max: 4 })('path', null, msg)
|
expect(linter.validators.length({ max: 4 })('path', undefined)).to.be.undefined
|
||||||
linter.validators.length({ max: 4 })('path', undefined, msg)
|
expect(linter.validators.length({ max: 4 })('path', '12345')).to.not.be.empty
|
||||||
linter.validators.length({ min: 4 })('path', '12345', msg)
|
expect(linter.validators.length({ max: 4 })('path', ' 1 ')).to.be.undefined
|
||||||
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', () => {
|
it('creates an error when string does not match regex', () => {
|
||||||
let msg = []
|
expect(linter.validators.regex(/[0-9]+/)('path', 'abcdef')).to.not.be.empty
|
||||||
linter.validators.regex(/[0-9]+/)('path', 'abcdef', msg)
|
expect(linter.validators.regex(/[0-9]+/)('path', ' ')).to.not.be.empty
|
||||||
linter.validators.regex(/[0-9]+/)('path', ' ', msg)
|
expect(linter.validators.regex(/.+/)('path', ' ')).to.not.be.empty
|
||||||
linter.validators.regex(/.+/)('path', ' ', msg)
|
|
||||||
|
|
||||||
expect(msg).to.not.be.empty
|
|
||||||
expect(msg.length).to.equal(3)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
Loading…
Reference in New Issue