{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://dscsagovernance.org/dscsa-trace-message-schemas/pdg-dscsa-ti-request-v2.0.0.json",
    "title": "PDG DSCSA TI Request Schema",
    "description": "Defines requests for DSCSA Transaction Information.",
    "type": "object",
    "properties": {
        "tiRequestSet": {
            "properties": {
                "tracingSchemaSetVersion": {
                    "$ref": "#/definitions/tracingSchemaSetVersionType",
                    "$comment": "The schema version as published by PDG."
                },
                "tiRequestAuditReferences": {
                    "type": "object",
                    "properties": {
                        "tiRequestID": {
                            "$ref": "#/definitions/uuidType",
                            "$comment": "This is a unique ID assigned to this message.  The receiver of the message can reference this ID in communications.  "
                        },
                        "tiRequestTimestamp": {
                            "$ref": "#/definitions/timestampType",
                            "$comment": "This is the date and time the message was created.  "
                        },
                        "tiRequesterInvestigationID": {
                            "$ref": "#/definitions/investigationIDType",
                            "$comment": "This is a unique ID the Requester has assigned to this message.  The receiver of the message can reference this ID in communications. "
                        }
                    },
                    "required": [
                        "tiRequestID",
                        "tiRequestTimestamp"
                    ],
                    "$comment": "A collection of properties used to track the request and investigation."
                },
                "tiRequestingPartyInformation": {
                    "type": "object",
                    "required": [
                        "contactInformation",
                        "callbackAddress"
                    ],
                    "properties": {
                        "dscsaCredentialPresentation": {
                            "$ref": "#/definitions/dscsaCredentialPresentationType",
                            "$comment": "The ATP, ATP-Equivalent or DSCSA Authority credential."
                        },
                        "contactInformation": {
                            "$ref": "#/definitions/contactInformationType",
                            "$comment": "Information to aid the Requester or Responder in contacting the other party in regard to the Request or Response."
                        },
                        "requesterGLN": {
                            "$ref": "#/definitions/glnType",
                            "$comment": "The GS1 Global Location Number of your organization. "
                        },
                        "callbackAddress": {
                            "$ref": "#/definitions/callbackAddressType",
                            "$comment": "The methods and addresses where the responder is to send the TI Response message or where the requester can contact is to send exception information."
                        }
                    },
                    "$comment": "Information identifying the TI Requester."
                },
                "tiRequestParameters": {
                    "type": "object",
                    "properties": {
                        "responseTypeRequested": {
                            "type": "string",
                            "enum": [
                                "Transaction Information",
                                "All Known Owners",
                                "Last Known Owner"
                            ],
                            "$comment": "The type of information the Requester is asking the Responder to provide.  "
                        },
                        "investigationReasonAttestation": {
                            "type": "string",
                            "enum": [
                                "Suspect Product Investigation",
                                "Illegitimate Product Investigation",
                                "Recalled Product Investigation",
                                "Compliance Audit",
                                "Health Authority Inspection"
                            ],
                            "$comment": "The reason for initiating this TI Request. There are four choices for this mandatory attribute."
                        },
                        "investigationCircumstances": {
                            "type": "string",
                            "$comment": "A short description of the investigation that will help the Responder determine their appropriate actions. "
                        },
                        "onBehalfOfAuthority": {
                            "type": "object",
                            "properties": {
                                "onBehalfOfAuthorityFlag": {
                                    "$ref": "#/definitions/onBehalfOfAuthorityType",
                                    "default": false,
                                    "$comment": "Indicates that this request is made on behalf of an Authority."
                                },
                                "authorityInformation": {
                                    "$ref": "#/definitions/contactInformationType",
                                    "$comment": "The TI Requests in this set are requested by an authority. Responders may use authority Information to contact the authority directly and provide the requested information."
                                }
                            },
                            "$comment": "The TI Requests in this set are requested by an authority. Responders may use authorityInformation to contact the authority directly and provide the requested information."
                        }
                    },
                    "allOf": [
                        {
                            "if": {
                                "properties": {
                                    "investigationReasonAttestation": {"const": "Recalled Product Investigation"}
                                }
                            },
                            "then": {
                                "properties": {
                                    "3911IncidentNumberList": {
                                        "not": {}
                                    },
                                    "recallDepth": {
                                        "$ref": "#/definitions/recallDepthType",
                                        "$comment": "Used to mitigate unnecessary returns when the recall doesn't affect the Responder."
                                    },
                                    "recallRequestLevel": {
                                        "type": "string",
                                        "enum": [
                                            "Serial Number",
                                            "Lot Number"
                                        ]
                                    }
                                },
                                "required": [
                                    "responseTypeRequested",
                                    "investigationReasonAttestation",
                                    "investigationCircumstances",
                                    "recallDepth",
                                    "recallRequestLevel"
                                ]
                            },
                            "else": {
                                "if": {
                                    "properties": {
                                        "investigationReasonAttestation": {"const": "Illegitimate Product Investigation"}
                                    }
                                },
                                "then": {
                                    "required": [
                                        "responseTypeRequested",
                                        "investigationReasonAttestation",
                                        "investigationCircumstances"
                                    ],
                                    "properties": {
                                        "3911IncidentNumberList": {
                                            "$ref": "#/definitions/3911IncidentListType",
                                            "$comment": "List of 3911 Incident number(s) are provided if assigned. The 3911 Incident Number(s) assigned by the FDA associated with the investigation. Responders provide 3911 Incident Number(s) they know and do not repeat 3911 Incident Number(s) that the Requester may have included in the TI Request."
                                        },
                                        "recallDepth": {
                                            "not": {}
                                        },
                                        "recallRequestLevel": {
                                            "not": {}
                                        }
                                    }
                                },
                                "else": {
                                    "required": [
                                        "responseTypeRequested",
                                        "investigationReasonAttestation",
                                        "investigationCircumstances"
                                    ],
                                    "properties": {
                                        "3911IncidentNumberList": {
                                            "not": {}
                                        },
                                        "recallDepth": {
                                            "not": {}
                                        },
                                        "recallRequestLevel": {
                                            "not": {}
                                        }
                                    }
                                }
                            }
                        },
                        {
                            "if": {
                                "properties": {
                                    "onBehalfOfAuthority": {
                                        "type": "object",
                                        "properties": {
                                            "onBehalfOfAuthorityFlag": {"const": true}
                                        },
                                        "required": ["onBehalfOfAuthorityFlag"]
                                    }
                                }
                            },
                            "then": {
                                "properties": {
                                    "onBehalfOfAuthority": {
                                        "type": "object",
                                        "if": {
                                            "properties": {}
                                        },
                                        "then": {
                                            "required": ["authorityInformation"]
                                        }
                                    }
                                }
                            },
                            "else": {
                                "properties": {
                                    "onBehalfOfAuthority": {
                                        "type": "object",
                                        "if": {
                                            "properties": {}
                                        },
                                        "then": {
                                            "not": {
                                                "required": ["authorityInformation"]
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    ],
                    "$comment": "Provides context for the individual requests and the type of information being requested."
                },
                "tiRequestAdditionalInformation": {
                    "type": "object",
                    "properties": {
                        "additionalInformationLink": {
                            "type": "array",
                            "items": {"$ref": "#/definitions/additionalInformationLinkType"},
                            "$comment": "Lists the addresses to documents or files that are related to this request."
                        },
                        "additionalInformationBase64": {
                            "type": "array",
                            "items": {"$ref": "#/definitions/additionalInformationBase64Type"},
                            "$comment": "Encodes (in Base 64) document or files that are relevant to this request."
                        }
                    },
                    "$comment": "Provide links to or incorporates additional documents."
                },
                "tiRequests": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "requestLineNumber": {
                                "type": "number",
                                "$comment": "Identifies an individual request within a request set. "
                            },
                            "productID": {
                                "type": "object",
                                "properties": {
                                    "gtin": {
                                        "$ref": "#/definitions/gtinType",
                                        "$comment": "The GS1 Global Trade Item Number (GTIN) identifying the product. See Implementation Guideline: Applying GS1 Standards for DSCSA and Traceability Conditional based on Product ID choice (GTIN or NDC)."
                                    },
                                    "ndc": {
                                        "$ref": "#/definitions/ndcType",
                                        "$comment": "The FDA National Drug Code. Conditional based on Request Product ID choice (GTIN or NDC)."
                                    }
                                },
                                "oneOf": [
                                    {
                                        "required": ["gtin"]
                                    },
                                    {
                                        "required": ["ndc"]
                                    }
                                ],
                                "$comment": "Either the Global Trade Item Number (GTIN) or the National Drug Code (NDC)."
                            },
                            "serialNumberOrLotNumber": {
                                "type": "object",
                                "properties": {
                                    "serialNumber": {
                                        "$ref": "#/definitions/serialNumberType",
                                        "$comment": "A unique or random number when combined with the GTIN makes a unique identifier for homogeneous cases and packages."
                                    },
                                    "lotNumber": {
                                        "$ref": "#/definitions/lotNumberType",
                                        "$comment": "The Lot number assigned by the Manufacturer or Repackager found in the original Transaction Information."
                                    }
                                },
                                "oneOf": [
                                    {
                                        "required": ["serialNumber"]
                                    },
                                    {
                                        "required": ["lotNumber"]
                                    }
                                ],
                                "$comment": "The FDA as the authority, May include a Lot Number instead of the serial number.    Please be aware of the volume of response data a Lot Number based request may return.    GTIn searches can distinguish between cases and packages with the same serial number.  NDC searches may return information on a case and package with the same NDC/Serial Number."
                            },
                            "requestLineNumberAdditionalInformation": {
                                "type": "object",
                                "properties": {
                                    "additionalInformationLink": {
                                        "type": "array",
                                        "items": {"$ref": "#/definitions/additionalInformationLinkType"},
                                        "$comment": "List the addresses to documents or files that are related to this request."
                                    },
                                    "additionalInformationBase64": {
                                        "type": "array",
                                        "items": {"$ref": "#/definitions/additionalInformationBase64Type"},
                                        "$comment": "Collection of documents or files that are encoded in Base 64."
                                    }
                                },
                                "$comment": "List file name(s) that were sent with this request or links to file(s) associated with this request."
                            }
                        },
                        "required": [
                            "requestLineNumber",
                            "productID",
                            "serialNumberOrLotNumber"
                        ]
                    },
                    "$comment": "A collection of individual items for which Transaction or Ownership information is being requested."
                }
            },
            "type": "object",
            "required": [
                "tracingSchemaSetVersion",
                "tiRequestAuditReferences",
                "tiRequestingPartyInformation",
                "tiRequestParameters",
                "tiRequests"
            ]
        }
    },
    "definitions": {
        "3911IncidentListType": {
            "type": "array",
            "minItems": 0,
            "items": {"type": "string"},
            "$comment": "List of 3911 Incident number(s) are provided if assigned. The 3911 Incident Number(s) assigned by the FDA associated with the investigation. Responders provide 3911 Incident Number(s) they know and do not repeat 3911 Incident Number(s) that the Requester may have included in the TI Request."
        },
        "additionalInformationBase64Type": {
            "type": "string",
            "contentEncoding": "base64",
            "description": "This property can hold base64 encoded data of various image and document formats (e.g., PNG, JPG, PDF)."
        },
        "additionalInformationLinkType": {
            "type": "string",
            "format": "uri"
        },
        "apiType": {
            "type": "string",
            "format": "uri",
            "pattern": "^(https?|wss?|ftp)://[^\\s/$.?#].[^\\s]*$"
        },
        "callbackAddressType": {
            "type": "object",
            "properties": {
                "apiCallbackAddress": {
                    "$ref": "#/definitions/apiType",
                    "$comment": "The URI address where the TI Response is to be sent."
                },
                "emailCallbackAddress": {
                    "$ref": "#/definitions/emailType",
                    "$comment": "The email address where the TI Response is to be sent."
                }
            },
            "$comment": "The methods and addresses where the responder is to send the TI Response message or where the requester can contact is to send exception information.",
            "required": ["emailCallbackAddress"]
        },
        "contactInformationType": {
            "properties": {
                "personOrDepartmentName": {
                    "$ref": "#/definitions/nameValueType",
                    "$comment": "Part of contactInformation. The person or Department that can be contacted regarding the Request or Response."
                },
                "organizationName": {
                    "$ref": "#/definitions/nameValueType",
                    "$comment": "Name of organization requesting TI or responding to a request."
                },
                "contactMethod": {
                    "type": "object",
                    "$comment": "Phone number or email address that the Requester or Responder can be contacted with.",
                    "properties": {
                        "phone": {
                            "$ref": "#/definitions/phoneType",
                            "$comment": "The phone number of the Requester or Responder."
                        },
                        "email": {
                            "$ref": "#/definitions/emailType",
                            "$comment": "The email address of the Requester or Responder."
                        }
                    },
                    "anyOf": [
                        {
                            "required": ["phone"]
                        },
                        {
                            "required": ["email"]
                        }
                    ]
                }
            },
            "type": "object",
            "required": [
                "personOrDepartmentName",
                "organizationName",
                "contactMethod"
            ],
            "$comment": "Information to aid the Requester or Responder in contacting the other party in regard to the Request or Response."
        },
        "drugNameType": {"type": "string"},
        "dscsaCredentialPresentationType": {
            "type": "object",
            "properties": {
                "dscsaATPCredentialPresentation": {
                    "type": "string",
                    "format": "jwt",
                    "$comment": "A W3C standard verifiable credential conformant to the OCI DSCSA ATP Credential specification."
                },
                "dscsaATP-EquivalentCredentialPresentation": {
                    "type": "string",
                    "format": "jwt",
                    "$comment": "A W3C standard verifiable credential conformant with the OCI DSCSA ATP Equivalent Credential specification."
                },
                "dscsaAuthorityCredentialPresentation": {
                    "type": "string",
                    "format": "jwt",
                    "$comment": "A W3C standard verifiable credential conformant with the OCI DSCSA Authority Credential specification."
                }
            },
            "oneOf": [
                {
                    "required": ["dscsaATPCredentialPresentation"]
                },
                {
                    "required": ["dscsaATP-EquivalentCredentialPresentation"]
                },
                {
                    "required": ["dscsaAuthorityCredentialPresentation"]
                }
            ],
            "$comment": "Requester or Responder signed presentation of an ATP, ATP-Equivalent or DSCSA Authority credential. See latest OCI Schema Version."
        },
        "emailType": {
            "type": "string",
            "format": "email"
        },
        "glnType": {
            "type": "string",
            "minLength": 13,
            "pattern": "^\\d{13}$",
            "maxLength": 13,
            "$comment": "The GS1 Global Location Number (GLN) as recorded in the TI record. See Implementation Guideline: Applying GS1 Standards for DSCSA and Traceability."
        },
        "gtinType": {
            "type": "string",
            "minLength": 14,
            "pattern": "^\\d{14}$",
            "maxLength": 14
        },
        "investigationIDType": {
            "type": "string",
            "$comment": "Identifier created by Requester to group and archive requests and their associated responses.  Recommend for Requester to assign unique idenfier and consider use of UUID or unique key combinations of RequestID + CaseID + Requester."
        },
        "lotNumberType": {
            "type": "string",
            "minLength": 1,
            "maxLength": 20,
            "$comment": "The Lot number assigned by the Manufacturer or Repackager found in the original Transaction Information."
        },
        "nameValueType": {"type": "string"},
        "ndcType": {
            "oneOf": [
                {
                    "type": "string",
                    "pattern": "^[0-9]{4}-[0-9]{4}-[0-9]{2}$",
                    "$comment": "4-4-2"
                },
                {
                    "type": "string",
                    "pattern": "^[0-9]{5}-[0-9]{3}-[0-9]{2}$",
                    "$comment": "5-3-2"
                },
                {
                    "type": "string",
                    "pattern": "^[0-9]{5}-[0-9]{4}-[0-9]{1}$",
                    "$comment": "5-4-1"
                },
                {
                    "type": "string",
                    "$comment": "6-4-2",
                    "pattern": "^[0-9]{6}-[0-9]{4}-[0-9]{2}$"
                }
            ],
            "$comment": "The FDA National Drug Code. Conditional based on Request Product ID choice (GTIN or NDC)."
        },
        "onBehalfOfAuthorityType": {
            "type": "boolean",
            "enum": [
                true,
                false
            ],
            "default": false,
            "$comment": "The contact information for the authority that requested the Trade Partner to make this request. The Responder may use this information to contact the requesting authority directly."
        },
        "phoneType": {
            "type": "string",
            "$comment": "The phone number of the Requester or Responder."
        },
        "recallDepthType": {
            "type": "string",
            "enum": [
                "Wholesale Distributor",
                "Pharmacy",
                "Patient"
            ]
        },
        "serialNumberType": {
            "type": "string",
            "maxLength": 20,
            "minLength": 1,
            "pattern": ""
        },
        "timestampType": {
            "$comment": "A timestamp to millisecond precision, with an explicit timezone indicator (+/‐hh:mm) relative to UTC",
            "type": "string",
            "pattern": "^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T(2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]\\.[0-9]{3}(Z|((\\+|\\-)((0[0-9]|1[0-3]):([0-5][0-9])|14:00)))"
        },
        "tracingSchemaSetVersionType": {
            "type": "string",
            "description": "Tracing Request and Response Schema Set Version. The tracing request and tracing response schema are versioned together to ensure alignment.",
            "default": "2.0.0",
            "pattern": "^[0-9]{1}.[0-9]{1}.[0-9]{1}$"
        },
        "uuidType": {
            "type": "string",
            "pattern": "^[a-fA-F\\d]{8}-[a-fA-F\\d]{4}-4[a-fA-F\\d]{3}-[89abAB][a-fA-F\\d]{3}-[a-fA-F\\d]{12}$",
            "$comment": "Universally Unique Identifier (UUID)"
        }
    },
    "required": ["tiRequestSet"]
}