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

import pscheduler

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

data_validator = {
    "type": "object",
    "properties": {
        "schema": { "$ref": "#/pScheduler/Cardinal" },
        "url": { "$ref": "#/pScheduler/URL" },
        "bind": { "$ref": "#/pScheduler/Host" },
        "_auth-token": { "$ref": "#/pScheduler/String" },
        "measurement-agent": { "$ref": "#/pScheduler/Host" },
        "verify-ssl": { "$ref": "#/pScheduler/Boolean" },
        "retry-policy": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "attempts": {"$ref": "#/pScheduler/Cardinal" },
                    "wait": {"$ref": "#/pScheduler/Duration" },
                },
                "required": [ "attempts", "wait"]
            }
        },
        "data-formatting-policy": {
            "type": "string",
            "enum": [
                "prefer-mapped",
                "mapped-and-raw",
                "mapped-only",
                "raw-only",
            ]
        },
        "summaries": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "event-type": { "$ref": "#/pScheduler/String" },
                    "summary-type": { "$ref": "#/pScheduler/String" },
                    "summary-window": { "$ref": "#/pScheduler/CardinalZero" },
                },
                "required": [ "event-type", "summary-type", "summary-window"]
            }
        }
    },
    "additionalProperties": False,
    "required": [ "url"]
}

valid, error = pscheduler.json_validate(json, data_validator)

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