@@ -9,6 +9,12 @@ import os
from lib import YnlFamily
+try:
+ import jsonschema
+except ModuleNotFoundError as e:
+ print('Error: {}. Try `pip install jsonschema`'.format(e))
+ raise SystemExit(1)
+
class ynlConfig():
def __init__(self):
self.no_schema = True
@@ -66,13 +72,36 @@ def main():
if args.config:
directory = ""
yamls = {}
-
- if not os.path.exists(args.config):
- print("Error: ", args.config, " doesn't exist")
- exit(-1)
-
- f = open(args.config)
- data = json.load(f)
+ configSchema = os.path.dirname(__file__) + "/ynl-config.schema"
+
+ # Load ynl-config json schema
+ try:
+ with open(configSchema, 'r') as f:
+ s = json.load(f)
+ except FileNotFoundError as e:
+ print('Error:', e)
+ raise SystemExit(1)
+ except json.decoder.JSONDecodeError as e:
+ print('Error: {}:'.format(args.schema), e)
+ raise SystemExit(1)
+
+ # Load config file
+ try:
+ with open(args.config, 'r') as f:
+ data = json.load(f)
+ except FileNotFoundError as e:
+ print('Error:', e)
+ raise SystemExit(1)
+ except json.decoder.JSONDecodeError as e:
+ print('Error: {}:'.format(args.schema), e)
+ raise SystemExit(1)
+
+ # Validate json config against the ynl-config schema
+ try:
+ jsonschema.validate(instance=data, schema=s)
+ except jsonschema.exceptions.ValidationError as e:
+ print('Error:', e)
+ raise SystemExit(1)
for k in data:
if k == 'yaml-specs-path':
new file mode 100644
@@ -0,0 +1,72 @@
+{
+ "$schema": "https://json-schema.org/draft-07/schema",
+ "description": "YNL specs configuration file",
+ "type": "object",
+
+ "properties": {
+ "yaml-specs-path": {
+ "description": "Path to Yaml specs",
+ "type": "string"
+ },
+ "spec-args": {
+ "description": "Individual spec args",
+ "type": "object",
+ "patternProperties": {
+ "^.*(\\.yaml)$": {
+ "description": "Specific yaml spec arguments",
+ "type": "object",
+ "properties": {
+ "schema": {
+ "description": "The schema to use",
+ "type": "string"
+ },
+ "no-schema": {
+ "description": "No schema",
+ "type": "boolean",
+ "default": true
+ },
+ "do": {
+ "description": "The do function to use",
+ "type": "string"
+ },
+ "dump": {
+ "description": "The dump function to use",
+ "type": "string"
+ },
+ "subscribe": {
+ "description": "The multicast group to subscribe to",
+ "type": "string"
+ },
+ "sleep": {
+ "description": "The number to seconds to sleep",
+ "type": "number",
+ "default": 0
+ },
+ "json-params": {
+ "description": "The json params to use for different functions",
+ "type": "object",
+ "patternProperties": {
+ "^.*$": {
+ "type": ["string", "number", "object"],
+ "patternProperties": {
+ "^.*$": {
+ "type": ["string", "number"]
+ }
+ },
+ "additionalProperties": false
+ }
+ }
+ },
+ "additionalProperties": false
+ },
+ "additionalProperties": false
+ }
+ },
+ "additionalProperties": false
+ }
+ },
+ "additionalProperties": false,
+ "required": [
+ "yaml-specs-path"
+ ]
+}