{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Rules",
    "description": "The collection of rules",
    "type": "array",
    "items": {
        "title": "Rule.",
        "type": "object",
        "properties": {
            "id": {
                "description": "Unique identifier given by the server",
                "type": "integer"
            },
            "name": {
                "description": "A name to identify the rule.",
                "type": "string"
            },
            "active": {
                "description": "Enable/Disable a rule. When disabled, the rule is not evaluated on events received.",
                "type": "boolean"
            },
            "organizationId": {
                "description": "The organization for which the rule is configured.",
                "type": "integer"
            },
            "conditions": {
                "description": "List of condition to evaluate against events received.",
                "type": "array",
                "items": {
                    "description": "Condition to evaluate, **MUST** define at least one of the three condition criteria: `eventSourceId`, `eventTypeId` or `fn`.",
                    "type": "object",
                    "properties": {
                        "description": {
                            "description": "Human friendly text to let the possibility to explain what the condition evaluates.",
                            "type": "string"
                        },
                        "eventSource": {
                            "description": "Define the data for an event source",
                            "type": "object",
                            "properties": {
                                "id": {
                                    "description": "The event source to match on event received. If not present, joker `*` will be used.",
                                    "type": "integer"
                                },
                                "generatedIdentifier": {
                                    "description": "Unique identifier generated by iFLUX server of the event source usable in the events.",
                                    "type": "string"
                                }
                            },
                            "required": [
                                "id",
                                "generatedIdentifier"
                            ]
                        },
                        "eventType": {
                            "description": "Define the data for an event type",
                            "type": "object",
                            "properties": {
                                "id": {
                                    "description": "The event type to match on event received. If not present, joker `*` will be used.",
                                    "type": "integer"
                                },
                                "type": {
                                    "description": "Event type used in the events sent.",
                                    "type": "string"
                                }
                            },
                            "required": [
                                "id",
                                "type"
                            ]
                        },
                        "fn": {
                            "description": "Javascript expression to be evaluated. If not present, only `event source` and `event type` will be evaluated.",
                            "type": "object",
                            "properties": {
                                "expression": {
                                    "description": "Javascript expression to let more complex condition matching. **MUST** be a valid javascript expression",
                                    "type": "string"
                                },
                                "sampleEvent": {
                                    "description": "A valid event that will be evaluated to true with the expression.",
                                    "type": "object"
                                }
                            },
                            "required": [
                                "expression",
                                "sample"
                            ]
                        }
                    }
                },
                "minItems": 1
            },
            "transformations": {
                "description": "List of transformations to apply to an event once the conditions matched.",
                "type": "array",
                "items": {
                    "description": "Transformation to apply.",
                    "type": "object",
                    "properties": {
                        "description": {
                            "description": "Human friendly text to let the possibility to explain what the transformation does.",
                            "type": "string"
                        },
                        "actionTarget": {
                            "description": "Define the data for an action target",
                            "type": "object",
                            "properties": {
                                "id": {
                                    "description": "The action target to send the event transformed.",
                                    "type": "integer"
                                },
                                "generatedIdentifier": {
                                    "description": "Unique identifier generated by iFLUX server of the action target sent with actions.",
                                    "type": "string"
                                }
                            },
                            "required": [
                                "id",
                                "generatedIdentifier"
                            ]
                        },
                        "actionType": {
                            "description": "Define the data for an action type",
                            "type": "object",
                            "properties": {
                                "id": {
                                    "description": "The action type.",
                                    "type": "integer"
                                },
                                "type": {
                                    "description": "Action type used in the actions sent.",
                                    "type": "string"
                                }
                            },
                            "required": [
                                "id",
                                "type"
                            ]
                        },
                        "fn": {
                            "description": "Javascript expression to transform the events. If not present, event is not transformed and passed as it is received.",
                            "type": "object",
                            "properties": {
                                "expression": {
                                    "description": "Javascript expression to let more complex transformation. **MUST** be a valid javascript expression",
                                    "type": "string"
                                },
                                "sample": {
                                    "description": "A valid sample to validate the transformation expression.",
                                    "type": "object",
                                    "properties": {
                                        "event": {
                                            "description": "A valid event for the transformation expression validation.",
                                            "type": "object"
                                        },
                                        "eventSourceTemplateId": {
                                            "description": "The event source template to inject to the transformation validation.",
                                            "type": "integer"
                                        },
                                        "eventTypeId": {
                                            "description": "The event type to inject to the transformation validation.",
                                            "type": "integer"
                                        }
                                    },
                                    "required": [
                                        "event"
                                    ]
                                }
                            },
                            "required": [
                                "expression",
                                "sample"
                            ]
                        }
                    },
                    "required": [
                        "actionTarget",
                        "actionType"
                    ]
                },
                "minItems": 1
            }
        },
        "required": [
            "id",
            "name",
            "active",
            "organizationId",
            "conditions",
            "transformations"
        ]
    }
}