Skip to content

Commit

Permalink
feat: validate config after parsing (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 20, 2018
1 parent accbeeb commit b7ef1ad
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 13 deletions.
5 changes: 5 additions & 0 deletions config/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ module.exports = {
/dtrace-provider/
],
rules: [{
test: /schema.yml$/,
rules: [{
use: ['ajv-pack-loader', 'yaml-loader']
}]
}, {
test: /\.tsx?$/,
rules: [{
enforce: 'pre',
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
"@types/ws": "~6.0",
"@types/yargs-parser": "~11.0",
"ajv": "^6.6.2",
"ajv-pack": "^0.3.1",
"ajv-pack-loader": "^2.2.0",
"awesome-typescript-loader": "~5.2",
"aws-sdk": "~2.380.0",
"bufferutil": "~4.0",
Expand Down Expand Up @@ -97,6 +99,7 @@
"webpack-bundle-analyzer": "~3.0",
"webpack-cli": "~3.1",
"ws": "~6.1",
"yaml-loader": "^0.5.0",
"yargs-parser": "~11.1"
},
"resolutions": {
Expand Down
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import { EntityModule } from './module/EntityModule';
import { ServiceModule } from './module/ServiceModule';
import { TransformModule } from './module/TransformModule';

interface AjvSchema {
(data: any): boolean;
errors: Array<any>;
}

const SCHEMA: AjvSchema = require('./schema.yml');

// main arguments
const MAIN_ARGS = {
array: ['config-path'],
Expand Down Expand Up @@ -72,6 +79,14 @@ async function main(argv: Array<string>): Promise<number> {
logger.info(VERSION_INFO, 'version info');
logger.info({ args }, 'main arguments');

const valid = SCHEMA(config);
if (!valid) {
logger.error({ errors: SCHEMA.errors }, 'config failed to validate');
return STATUS_ERROR;
} else {
logger.info('config is valid');
}

const botModule = new BotModule({ logger });
const modules: Array<Module> = [
botModule,
Expand Down
173 changes: 169 additions & 4 deletions src/schema.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
$schema: "http://json-schema.org/schema#" # yes, this is a json schema written in yaml
$id: "https://github.com/ssube/isolex/schema.yml"
definitions:
command:
entity-command:
type: object
properties:
noun:
type: string
verb:
type: string

entity-context:
type: object
properties:
channel:
$ref: "#/definitions/entity-context-channel"
name:
type: string
uid:
type: string

match-rule:
type: object
properties:
key:
type: string
negate:
type: boolean
operator:
type:
enum: [any, every]
enum: [any, every]
values:
type: array
items:
Expand All @@ -28,16 +40,169 @@ definitions:
regexp:
type: string
match-data:
type: object
properties:
rules:
type: array
items:
$ref: "#/definitions/match-rule"

match-results:
type: object
properties:
matched:
type: boolean
errors:
type: array
items:
type: string
type: string

service-bot:
type: object
properties:
controllers:
type: array
items:
anyOf:
- $ref: "#/definitions/service-controller-count"
- $ref: "#/definitions/service-controller-kubernetes"
- $ref: "#/definitions/service-controller-math"
- type: object
properties:
metadata:
$ref: "#/definitions/service-metadata"
filters:
type: array
items:
$ref: "#/definitions/service-metadata"
listeners:
type: array
items:
$ref: "#/definitions/service-metadata"
logger:
type: object
properties:
level:
enum: [debug, info, warn, error]
name:
type: string
migrate:
type: boolean
parser:
type: array
items:
anyOf:
- type: object
properties:
metadata:
$ref: "#/definitions/service-metadata"
storage:
type: object
properties:
database:
type: string
type:
type: string

service-controller-count:
type: object
properties:
metadata:
$ref: "#/definitions/service-metadata"
data:
type: object
properties:
default:
properties:
count:
type: string
name:
type: string
field:
properties:
count:
type: string
name:
type: string
range:
properties:
min:
type: number
max:
type: number

service-controller-kubernetes:
type: object
properties:
metadata:
$ref: "#/definitions/service-metadata"
data:
type: object
properties:
context:
properties:
cluster:
type: string
default:
type: string
path:
type: string
default:
properties:
namespace:
type: string

service-controller-math:
type: object
properties:
metadata:
$ref: "#/definitions/service-metadata"
data:
type: object
properties:
format:
properties:
list:
properties:
join:
type: string
number:
properties:
notation:
enum: [fixed, exponential, engineering, auto]
precision:
type: number
lowerExp:
type: number
upperExp:
type: number
fraction:
type: string
node:
properties:
implicit:
type: string
parenthesis:
type: string
math:
properties:
matrix:
type: string
number:
type: string

service-metadata:
type: object
properties:
kind:
type: string
name:
type: string

# root
type: object
properties:
data:
$ref: "#/definitions/service-bot"
metadata:
$ref: "#/definitions/service-metadata"
Loading

0 comments on commit b7ef1ad

Please sign in to comment.