diff --git a/rules/salty-dog.yml b/rules/salty-dog.yml index e51fb02..43dbe46 100644 --- a/rules/salty-dog.yml +++ b/rules/salty-dog.yml @@ -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: diff --git a/src/app.ts b/src/app.ts index d4d7306..c45a215 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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): Promise { }, }); + 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); diff --git a/src/rule/index.ts b/src/rule/index.ts index 8435101..8c61952 100644 --- a/src/rule/index.ts +++ b/src/rule/index.ts @@ -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; + } +} diff --git a/test/rule/TestLoadRule.ts b/test/rule/TestLoadRule.ts index 01b9b6b..6380662 100644 --- a/test/rule/TestLoadRule.ts +++ b/test/rule/TestLoadRule.ts @@ -15,7 +15,8 @@ const EXAMPLE_RULES = `{ name: test, desc: test-rule, level: info, - tags: [] + tags: [], + check: {} }] }`;