From 66dd66959fc293326a21fbe70474d0b2facced06 Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Wed, 7 Jul 2021 21:14:52 +0200 Subject: [PATCH] refactor linter validators --- src/cmd/cmd.gen.js | 9 +++++++-- src/cmd/cmd.lint.js | 5 +++++ src/linter.js | 12 ++++++------ tests/lint.test.js | 48 +++++++++++++++------------------------------ 4 files changed, 34 insertions(+), 40 deletions(-) create mode 100644 src/cmd/cmd.lint.js diff --git a/src/cmd/cmd.gen.js b/src/cmd/cmd.gen.js index 47e5a10..db58496 100644 --- a/src/cmd/cmd.gen.js +++ b/src/cmd/cmd.gen.js @@ -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 ': "Path to an info template. (optional)", '--annotations ': "Path to an annotations template. (optional)", '--out ': "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) } diff --git a/src/cmd/cmd.lint.js b/src/cmd/cmd.lint.js new file mode 100644 index 0000000..7012772 --- /dev/null +++ b/src/cmd/cmd.lint.js @@ -0,0 +1,5 @@ +module.exports = { + run(args) { + + } +} \ No newline at end of file diff --git a/src/linter.js b/src/linter.js index db91004..fb7a6e1 100644 --- a/src/linter.js +++ b/src/linter.js @@ -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}` } } } diff --git a/tests/lint.test.js b/tests/lint.test.js index c77ace6..136a372 100644 --- a/tests/lint.test.js +++ b/tests/lint.test.js @@ -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 }) }) \ No newline at end of file