{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Root iAgree schema.",
    "description": "iAgree v2 JSON schema",
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        },
        "version": {
            "type": "string"
        },
        "type": {
            "type": "string"
        },
        "context": {
            "type": "object",
            "properties": {
                "provider": {
                    "type": "string"
                },
                "consumer": {
                    "type": "string"
                },
                "validity": {
                    "$ref": "#/properties/validity"
                },
                "definitions": {
                    "$ref": "#/properties/definitions"
                },
                "infrastructure": {
                    "$ref": "#/properties/infrastructure"
                },
                "iAgree": {
                    "type": "string"
                }
            },
            "required": [
                "definitions"
            ]
        },
        "terms": {
            "type": "object",
            "properties": {
                "guarantees": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/guarantee"
                    }
                },
                "metrics": {
                    "$ref": "#/properties/metrics"
                },
                "configurations": {
                    "type": "object",
                    "additionalProperties": {
                        "$ref": "#/properties/configuration"
                    }
                },
                "pricing": {
                    "$ref": "#/properties/pricing"
                },
                "quotas": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/quota"
                    }
                },
                "rates": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/rate"
                    }
                }
            },
            "required": [
                "pricing",
                "metrics",
                "guarantees"
            ]
        },
        "configuration": {
            "type": "object",
            "properties": {
                "of": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/scopedConfiguration"
                    }
                },
                "scope": {
                    "$ref": "#/properties/scope"
                }
            },
            "required": [
                "of",
                "scope"
            ]
        },
        "scopedConfiguration": {
            "type": "object",
            "properties": {
                "scope": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {}
                    }
                },
                "value": {
                    "oneOf": [
                        {
                            "type": "string"
                        },
                        {
                            "type": "number"
                        },
                        {
                            "type": "boolean"
                        }
                    ]
                }
            },
            "required": [
                "scope",
                "value"
            ]
        },
        "validity": {
            "type": "object",
            "properties": {
                "initial": {
                    "type": "string"
                },
                "timeZone": {
                    "type": "string"
                },
                "end": {
                    "type": "string"
                }
            },
            "required": [
                "initial",
                "timeZone"
            ]
        },
        "infrastructure": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {
                    "type": "string"
                }
            }
        },
        "definitions": {
            "type": "object",
            "properties": {
                "schemas": {
                    "$ref": "#/properties/_schema"
                },
                "scopes": {
                    "$ref": "#/properties/scopes"
                },
                "logs": {
                    "$ref": "#/properties/log"
                }
            },
            "required": [
                "schemas",
                "scopes"
            ]
        },
        "_schema": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {
                    "type": "object",
                    "properties": {
                        "description": {
                            "type": "string"
                        },
                        "type": {
                            "type": "string"
                        },
                        "unit": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        "scopes": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {
                            "type": "object",
                            "properties": {
                                "name": {
                                    "type": "string"
                                },
                                "description": {
                                    "type": "string"
                                },
                                "type": {
                                    "type": "string"
                                },
                                "metadata": {
                                    "type": "object",
                                    "patternProperties": {
                                        "^[a-zA-Z0-9]*$": {}
                                    }
                                },
                                "format": {
                                    "type": "string"
                                },
                                "default": {}
                            },
                            "required": [
                                "name",
                                "description",
                                "type"
                            ]
                        }
                    }
                }
            }
        },
        "scope": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {}
            }
        },
        "log": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {
                    "type": "object",
                    "properties": {
                        "default": {
                            "type": "boolean"
                        },
                        "uri": {
                            "type": "string"
                        },
                        "stateUri": {
                            "type": "string"
                        },
                        "measures": {
                            "type": "string"
                        },
                        "scopes": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {
                                    "type": "object",
                                    "patternProperties": {
                                        "^[a-zA-Z0-9]*$": {
                                            "type": "string"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "required": [
                        "uri",
                        "stateUri",
                        "measures"
                    ]
                }
            }
        },
        "pricing": {
            "type": "object",
            "properties": {
                "cost": {
                    "type": "number",
                    "format": "double"
                },
                "currency": {
                    "type": "string"
                },
                "billing": {
                    "$ref": "#/properties/billing"
                }
            },
            "required": [
                "billing"
            ]
        },
        "billing": {
            "type": "object",
            "properties": {
                "period": {
                    "type": "string"
                },
                "initial": {
                    "type": "string"
                },
                "penalties": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/compensation"
                    }
                },
                "rewards": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/compensation"
                    }
                }
            }
        },
        "metrics": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {
                    "type": "object",
                    "properties": {
                        "schema": {
                            "type": "object",
                            "properties": {
                                "description": {
                                    "type": "string"
                                },
                                "type": {
                                    "type": "string"
                                },
                                "unit": {
                                    "type": "string"
                                }
                            }
                        },
                        "computer": {
                            "type": "string"
                        },
                        "parameters": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        },
                        "scope": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        },
                        "metadata": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        },
                        "defaultStateReload": {
                            "type": "boolean"
                        },
                        "window": {
                            "$ref": "#/properties/window"
                        }
                    }
                }
            }
        },
        "rate": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "scope": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        }
                    }
                },
                "over": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        }
                    }
                },
                "of": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/scopedRate"
                    }
                }
            },
            "required": [
                "id",
                "scope",
                "over",
                "of"
            ]
        },
        "quota": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "scope": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        }
                    }
                },
                "over": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        }
                    }
                },
                "of": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/scopedQuota"
                    }
                }
            },
            "required": [
                "id",
                "scope",
                "over",
                "of"
            ]
        },
        "scopedQuota": {
            "type": "object",
            "properties": {
                "scope": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {}
                    }
                },
                "limits": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/limit"
                    }
                }
            },
            "required": [
                "scope",
                "limits"
            ]
        },
        "scopedRate": {
            "type": "object",
            "properties": {
                "scope": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {}
                    }
                },
                "limits": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/limit"
                    }
                }
            },
            "required": [
                "scope",
                "limits"
            ]
        },
        "limit": {
            "type": "object",
            "properties": {
                "max": {
                    "type": "number"
                },
                "period": {
                    "type": "string"
                }
            },
            "required": [
                "max"
            ]
        },
        "guarantee": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "scope": {
                    "$ref": "#/properties/guarantee_scope"
                },
                "of": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/scoped_guarante"
                    }
                }
            },
            "required": [
                "id",
                "scope",
                "of"
            ]
        },
        "guarantee_scope": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {}
            }
        },
        "scoped_guarante": {
            "type": "object",
            "properties": {
                "scope": {
                    "$ref": "#/properties/guarantee_scope"
                },
                "objective": {
                    "type": "string"
                },
                "with": {
                    "$ref": "#/properties/with"
                },
                "window": {
                    "$ref": "#/properties/window"
                },
                "evidences": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/evidence"
                    }
                },
                "penalties": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/compensation"
                    }
                },
                "rewards": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/compensation"
                    }
                }
            },
            "required": [
                "scope",
                "objective",
                "with",
                "window"
            ]
        },
        "with": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {}
                    }
                }
            }
        },
        "window": {
            "type": "object",
            "properties": {
                "initial": {
                    "type": "string"
                },
                "end": {
                    "type": "string"
                },
                "type": {
                    "type": "string"
                },
                "period": {
                    "type": "string"
                }
            },
            "required": [
                "type",
                "period",
                "initial"
            ]
        },
        "evidence": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {
                    "type": "object",
                    "properties": {
                        "schema": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        },
                        "computer": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        "compensation": {
            "type": "object",
            "properties": {
                "over": {
                    "$ref": "#/properties/over"
                },
                "of": {
                    "type": "array",
                    "items": {
                        "$ref": "#/properties/compensation_of"
                    }
                },
                "aggegatedBy": {
                    "type": "string"
                },
                "groupBy": {
                    "type": "object",
                    "patternProperties": {
                        "^[a-zA-Z0-9]*$": {
                            "type": "object",
                            "patternProperties": {
                                "^[a-zA-Z0-9]*$": {}
                            }
                        }
                    }
                },
                "upTo": {
                    "type": "number",
                    "format": "double"
                }
            },
            "required": [
                "over"
            ]
        },
        "compensation_of": {
            "type": "object",
            "properties": {
                "value": {
                    "type": "string"
                },
                "condition": {
                    "type": "string"
                }
            },
            "required": [
                "value"
            ]
        },
        "over": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z0-9]*$": {
                    "type": "object",
                    "properties": {
                        "description": {
                            "type": "string"
                        },
                        "type": {
                            "type": "string"
                        },
                        "unit": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    },
    "required": [
        "id",
        "version",
        "type",
        "context",
        "terms"
    ]
}