feat: validate config while loading
This commit is contained in:
parent
dbfe0429fa
commit
c1ff388aff
|
@ -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:
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,8 @@ const EXAMPLE_RULES = `{
|
|||
name: test,
|
||||
desc: test-rule,
|
||||
level: info,
|
||||
tags: []
|
||||
tags: [],
|
||||
check: {}
|
||||
}]
|
||||
}`;
|
||||
|
||||
|
|
Loading…
Reference in New Issue