{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://netboxlabs.com/schemas/diode/deviation_rules.schema.json",
  "title": "Deviation rules",
  "description": "Rule file of the NetBox Assurance deviation rules engine. Rules are evaluated in order; the first enabled rule whose conditions all hold decides apply or review. A deviation that matches no rule is held for review.",
  "type": "object",
  "additionalProperties": false,
  "required": ["version"],
  "properties": {
    "version": {
      "description": "File format version.",
      "const": 1
    },
    "rules": {
      "type": "array",
      "items": { "$ref": "#/$defs/rule" }
    }
  },
  "$defs": {
    "objectType": {
      "type": "string",
      "description": "A NetBox object type such as dcim.device, or \"*\" for any.",
      "pattern": "^(\\*|[a-z_]+\\.[a-z_]+)$"
    },
    "scope": {
      "type": "string",
      "description": "Which object the condition reads its tag or custom field from: \"object\" (default), \"parent_device\", or a dotted field path on the ingested message such as device.site.",
      "pattern": "^(object|parent_device|[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)*)$"
    },
    "rule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "action", "object_types"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "description": "Unique rule name. Recorded on the deviations the rule applies."
        },
        "enabled": {
          "type": "boolean",
          "default": true,
          "description": "false keeps the rule in the file and skips it."
        },
        "action": {
          "type": "string",
          "enum": ["apply", "review"],
          "description": "apply writes the deviation to NetBox; review holds it in the review queue."
        },
        "object_types": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/objectType" },
          "description": "The deviation's own object type must be one of these."
        },
        "related_object_types": {
          "type": "array",
          "items": { "$ref": "#/$defs/objectType" },
          "description": "Required on apply rules: the other object types the deviation may also change. [] means none, \"*\" means any. Not valid on a review rule."
        },
        "operations": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "enum": ["create", "update"] },
          "description": "Allowed operations. Omitted means any. There is no delete."
        },
        "tag": {
          "type": "object",
          "additionalProperties": false,
          "description": "A tag the object will carry once the deviation applies, by exact name or slug: on the payload or already on the object in NetBox. When NetBox tags the reconciler cannot name could be it, the deviation is held.",
          "properties": {
            "name": { "type": "string", "minLength": 1 },
            "slug": { "type": "string", "minLength": 1 },
            "scope": { "$ref": "#/$defs/scope" }
          },
          "anyOf": [{ "required": ["name"] }, { "required": ["slug"] }]
        },
        "custom_field": {
          "type": "object",
          "additionalProperties": false,
          "required": ["name"],
          "description": "A custom field the object will hold once the deviation applies (the payload's value when sent, NetBox's otherwise): set to any non-empty value, or equal to \"equals\".",
          "properties": {
            "name": { "type": "string", "minLength": 1 },
            "equals": { "type": ["string", "number", "boolean"] },
            "scope": { "$ref": "#/$defs/scope" }
          }
        }
      },
      "if": { "properties": { "action": { "const": "review" } } },
      "then": { "not": { "required": ["related_object_types"] } },
      "else": { "required": ["related_object_types"] }
    }
  }
}
