#!/usr/bin/env python3
#
# Validate data for the tcp archiver
#

import pscheduler

try:
    json = pscheduler.json_load()
except ValueError as ex:
    pscheduler.succeed_json({
        "valid": False,
        "error": str(ex)
        })

MAX_SCHEMA = 1

SPEC_SCHEMA = {

    "local": {

        "v1": {
            "type": "object",
            "properties": {
                "bind": { "$ref": "#/pScheduler/Host" },
                "host": { "$ref": "#/pScheduler/Host" },
                "ip-version": { "$ref": "#/pScheduler/ip-version" },
                "port": { "$ref": "#/pScheduler/UInt16" },
                "retry-policy": { "$ref": "#/pScheduler/RetryPolicy" }
            },
            "required": [
                "host",
                "port"
            ],
            "additionalProperties": False
        }
    }
}

# Build a temporary structure with a reference that points
# directly at the validator for the specified version of the
# schema.  Using oneOf or anyOf results in error messages that are
# difficult to decipher.

temp_schema = {
    "local": SPEC_SCHEMA["local"],
    "$ref":"#/local/v%s" % json.get("schema", 1)
}

valid, error = pscheduler.json_validate(json, temp_schema, max_schema=MAX_SCHEMA)

if not valid:
    pscheduler.succeed_json({
        "valid": False,
        "error": error
        })
    
pscheduler.succeed_json({ "valid": True })
