54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
const objectPath = require('object-path');
|
|
|
|
const NAME_REGEX = /^[a-z0-9_-]+$/;
|
|
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
|
|
|
|
function required() {
|
|
return (path, input, msgs) => {
|
|
if(!input) {
|
|
msgs.push(`Property '${path}' must be set! value=${input}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
function length(min, max=-1) {
|
|
return (path, input, msgs) => {
|
|
if(min > 0 && input.length < min) {
|
|
msgs.push(`Property '${path}' must have at least ${min} characters! actual=${input.length}`)
|
|
}
|
|
if(max > 0 && input.length > max) {
|
|
msgs.push(`Property '${path}' must not have more than ${max} characters! actual=${input.length}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
function regex(regex) {
|
|
return (path, input, msgs) => {
|
|
if(!input.match(regex)) {
|
|
msgs.push(`Property '${path}' must only comply to pattern ${regex}! value=${input}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
const linterMappings = [
|
|
{ path: 'name', linters: [ required(), length(4), regex(NAME_REGEX) ] },
|
|
{ path: 'description', linters: [ required(), length(4), regex(DESCRIPTION_REGEX) ] }
|
|
]
|
|
|
|
|
|
module.exports.lint = function(spec) {
|
|
let messages = [];
|
|
linterMappings.forEach(element => {
|
|
let value = objectPath.get(spec, element.path);
|
|
element.linters.forEach(linter => {
|
|
linter(element.path, value, messages);
|
|
});
|
|
});
|
|
return messages;
|
|
}
|
|
|
|
module.exports.regex = {
|
|
NAME_REGEX,
|
|
DESCRIPTION_REGEX
|
|
} |