47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
const objectPath = require('object-path');
|
|
|
|
|
|
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(/^[a-z0-9_-]+$/) ] },
|
|
{ path: 'description', linters: [ required(), length(4), regex(/^[A-Z]{1}[a-z0-9_-]+\.$/) ] }
|
|
]
|
|
|
|
|
|
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;
|
|
} |