{
  "$id": "common-mailer",
  "definitions": {
    "prop-email": {
      "anyOf": [
        { "type": "string" },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "name": {
              "type": "string"
            },
            "address": {
              "type": "string",
              "format": "email"
            }
          }
        }
      ]
    },
    "mixed-email": {
      "anyOf": [{
        "type": "array",
        "minItems": 1,
        "maxItems": 15,
        "uniqueItems": true,
        "items": {
          "$ref": "#/definitions/prop-email"
        }
      }, {
        "$ref": "#/definitions/prop-email"
      }]
    },
    "account": {
      "type": "object",
      "properties": {
        "transport": {
          "type": "string",
          "enum": ["sparkpost", "mailgun"]
        },
        "sparkPostApiKey": { "type": "string" },
        "service": { "type": "string" },
        "host": { "type": "string" },
        "port": {
          "oneOf": [
            { "type": "string" },
            { "type": "integer" }
          ]
        },
        "secure": {
          "type": "boolean"
        },
        "auth": {
          "type": "object",
          "properties": {
            "user": {
              "type": "string"
            },
            "pass": {
              "type": "string",
              "minLength": 1
            },
            "xoauth2": {
              "type": "string",
              "minLength": 1
            },
            "api_key": {
              "type": "string",
              "minLength": 1
            },
            "domain": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "ignoreTLS": { "type": "boolean" },
        "name": { "type": "string" },
        "localAddress": { "type": "string" },
        "connectionTimeout": { "type": "integer" },
        "greetingTimeout": { "type": "integer" },
        "socketTimeout": { "type": "integer" },
        "debug": { "type": "boolean" },
        "authMethod": { "type": "string" },
        "tls": { "type": "object" }
      },
      "anyOf": [
        { "required": ["host", "port"] },
        { "required": ["service"] },
        { "required": ["transport"] }
      ],
      "if": {
        "type": "object",
        "required": ["transport"],
        "properties": {
          "transport": {
            "const": "sparkpost"
          }
        }
      },
      "then": {
        "type": "object",
        "required": ["sparkPostApiKey"],
        "properties": {
          "options": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "transactional": {
                "type": "boolean"
              },
              "return_path": {
                "type": "string"
              }
            }
          }
        }
      },
      "else": {
        "if": {
          "type": "object",
          "required": ["transport"],
          "properties": {
            "transport": {
              "const": "mailgun"
            }
          }
        },
        "then": {
          "type": "object",
          "required": ["auth"],
          "properties": {
            "auth": {
              "type": "object",
              "required": ["api_key", "domain"]
            }
          }
        },
        "else": {
          "type": "object",
          "required": ["auth"],
          "properties": {
            "auth": {
              "type": "object",
              "anyOf": [
                { "required": ["user", "pass"] },
                { "required": ["user", "xoauth2"] }
              ]
            }
          }
        }
      }
    },
    "email": {
      "type": "object",
      "anyOf": [
        { "required": ["to"] },
        { "required": ["cc"] },
        { "required": ["bcc"] }
      ],
      "required": ["from"],
      "properties": {
        "from": {
          "type": "string"
        },
        "sender": {
          "type": "string",
          "format": "email"
        },
        "to": {
          "$ref": "#/definitions/mixed-email"
        },
        "cc": {
          "$ref": "#/definitions/mixed-email"
        },
        "bcc": {
          "$ref": "#/definitions/mixed-email"
        },
        "replyTo": {
          "$ref": "#/definitions/prop-email"
        },
        "inReplyTo": {
          "type": "string"
        },
        "references": {
          "oneOf": [{
            "type": "string"
          }, {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "string"
            }
          }]
        },
        "subject": {
          "type": "string"
        },
        "text": {
          "oneOf": [
            { "type": "string" },
            { "type": "object" }
          ]
        },
        "html": {
          "type": "string"
        },
        "watchHtml": {
          "type": "string"
        },
        "priority": {
          "type": "string",
          "enum": ["high", "normal", "low"]
        },
        "headers": {
          "oneOf": [{
            "type": "array",
            "items": {
              "type": "object",
              "required": ["key"],
              "properties": {
                "key": {
                  "type": "string",
                  "minLength": 1
                },
                "value": {
                  "type": "string"
                }
              }
            }
          }, {
            "type": "object"
          }]
        },
        "alternatives": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "filename": {
                "type": "string"
              },
              "cid": {
                "type": "string"
              },
              "content": {
                "anyOf": [
                  { "type": "string" },
                  { "type": "object" }
                ]
              },
              "encoding": {
                "type": "string"
              },
              "contentType": {
                "type": "string"
              },
              "contentDisposition": {
                "type": "string"
              }
            }
          }
        },
        "attachments": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "filename": {
                "type": "string"
              },
              "cid": {
                "type": "string"
              },
              "content": {
                "oneOf": [
                  { "type": "string" },
                  { "type": "object" }
                ]
              },
              "encoding": { "type": "string" },
              "contentType": { "type": "string" },
              "contentDisposition": { "type": "string" },
              "path": { "type": "string" }
            }
          }
        },
        "envelope": {
          "type": "object",
          "properties": {
            "from": {
              "type": "string"
            },
            "to": {
              "type": "string"
            }
          }
        },
        "messageId": {
          "type": "string"
        },
        "date": {
          "type": "string"
        },
        "encoding": {
          "type": "string"
        }
      }
    }
  }
}
