1
0
Fork 0

feat: validate config while loading

This commit is contained in:
ssube 2019-11-17 12:07:02 -06:00 committed by Sean Sube
parent dbfe0429fa
commit c1ff388aff
4 changed files with 62 additions and 8 deletions

View File

@ -1,5 +1,43 @@
name: salty-dog-meta
definitions:
log-level:
type: string
enum:
- debug
- info
- warn
- error
config-logger:
type: object
required:
- level
- name
properties:
level:
$ref: "salty-dog-meta#/definitions/log-level"
name:
type: string
streams:
type: array
items:
oneOf:
- type: string
- type: object
config:
type: object
required:
- data
properties:
data:
type: object
required:
- logger
properties:
logger:
$ref: "salty-dog-meta#/definitions/config-logger"
name-safe:
type: string
pattern: "[-a-z0-9]+"
@ -27,12 +65,7 @@ definitions:
minLength: 8
maxLength: 255
level:
type: string
enum:
- debug
- info
- warn
- error
$ref: "salty-dog-meta#/definitions/log-level"
tags:
type: array
items:

View File

@ -4,7 +4,7 @@ import { showCompletionScript } from 'yargs';
import { loadConfig } from './config';
import { CONFIG_ARGS_NAME, CONFIG_ARGS_PATH, MODE, parseArgs } from './config/args';
import { YamlParser } from './parser/YamlParser';
import { createRuleSelector, createRuleSources, loadRules, resolveRules } from './rule';
import { createRuleSelector, createRuleSources, loadRules, resolveRules, validateConfig } from './rule';
import { RuleVisitor } from './rule/RuleVisitor';
import { readSource, writeSource } from './source';
import { VERSION_INFO } from './version';
@ -37,6 +37,11 @@ export async function main(argv: Array<string>): Promise<number> {
},
});
if (!validateConfig(ctx, config)) {
logger.error('config was not valid according to embedded schema');
return STATUS_ERROR;
}
const ruleSelector = createRuleSelector(args);
const ruleSources = createRuleSources(args);

View File

@ -222,3 +222,18 @@ export function validateRules(ctx: VisitorContext, root: any): boolean {
return false;
}
}
export function validateConfig(ctx: VisitorContext, root: any): boolean {
const { definitions, name } = ruleSchemaData as any;
const validCtx = new VisitorContext(ctx);
validCtx.addSchema(name, definitions);
const ruleSchema = validCtx.compile(definitions.config);
if (ruleSchema(root) === true) {
return true;
} else {
ctx.logger.error({ errors: ruleSchema.errors }, 'error validating rules');
return false;
}
}

View File

@ -15,7 +15,8 @@ const EXAMPLE_RULES = `{
name: test,
desc: test-rule,
level: info,
tags: []
tags: [],
check: {}
}]
}`;