#!/usr/bin/env python3
#
# Validate data for the httparchiver
#

import pscheduler

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

MAX_SCHEMA = 4

SPEC_SCHEMA = {

    "local": {

        "v1": {
            "type": "object",
            "properties": {
                "schema": {" type": "integer", "enum": [ 1 ] },
                "_url": { "$ref": "#/pScheduler/URL" },
                "op": {
                    "type": "string",
                    "enum": [
                        "put",
                        "post",
                    ]
                },
                "bind": { "$ref": "#/pScheduler/Host" },
                "retry-policy": { "$ref": "#/pScheduler/RetryPolicy" }
            },
            "required": [ "_url" ],
            "additionalProperties": False
        },

        "v2": {
            "type": "object",
            "properties": {
                "schema": {" type": "integer", "enum": [ 2 ] },
                "_url": { "$ref": "#/pScheduler/URL" },
                "op": {
                    "type": "string",
                    "enum": [
                        "put",
                        "post",
                    ]
                },
                "_headers": {
                    "type": "object",
                    "patternProperties": {
                        "^.*$": { "$ref": "#/pScheduler/String" }
                    },
                    "additionalProperties": False
                },
                "bind": { "$ref": "#/pScheduler/Host" },
                "retry-policy": { "$ref": "#/pScheduler/RetryPolicy" }
            },
            "required": [ "schema", "_url" ],
            "additionalProperties": False
        },

         "v3": {
            "type": "object",
            "properties": {
                "schema": {" type": "integer", "enum": [ 3 ] },
                "_url": { "$ref": "#/pScheduler/URL" },
                "op": {
                    "type": "string",
                    "enum": [
                        "put",
                        "post",
                    ]
                },
                "verify-ssl": { "$ref": "#/pScheduler/Boolean" },
                "_headers": {
                    "type": "object",
                    "patternProperties": {
                        "^.*$": { "$ref": "#/pScheduler/String" }
                    },
                    "additionalProperties": False
                },
                "bind": { "$ref": "#/pScheduler/Host" },
                "retry-policy": { "$ref": "#/pScheduler/RetryPolicy" }
            },
            "required": [ "schema", "_url" ],
            "additionalProperties": False
         },
        "v4": {
            "type": "object",
            "properties": {
                "schema": {" type": "integer", "enum": [ 4 ] },
                "_url": { "$ref": "#/pScheduler/URL" },
                "op": {
                    "type": "string",
                    "enum": [
                        "put",
                        "post",
                    ]
                },
                "verify-ssl": { "$ref": "#/pScheduler/Boolean" },
                "_headers": {
                    "type": "object",
                    "patternProperties": {
                        "^.*$": { "$ref": "#/pScheduler/String" }
                    },
                    "additionalProperties": False
                },
                "timeout": { "$ref": "#/pScheduler/Duration" },
                "bind": { "$ref": "#/pScheduler/Host" },
                "retry-policy": { "$ref": "#/pScheduler/RetryPolicy" }
            },
            "required": [ "schema", "_url" ],
            "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 })
