{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "id" : "http://labkey.org/schemas/surveyResponse.json",
   "title": "Survey Response",
   "type": "object",
   "required": ["participantId", "metadata", "data"],
   "properties": 
   {
      "participantId": 
      {
         "description": "Identifier for the participant within the application",
         "type": "string"
      },
      "metadata": 
      {
         "$ref" : "#/definitions/surveyMetadata",
         "description": "Information to identify the survey and its version"
      },
      "data":
      { 
         "description": "The data from the participant",
         "$ref" : "#/definitions/surveyData"
      }
   },
   "definitions": 
   {
      "surveyMetadata": 
      {
         "description": "Metadata about the activity as a whole",
         "type": "object",
         "required": ["studyId", "activityId", "version"],
         "studyId": {
            "description": "Identifier for the study being responded to",
            "type": "string"
         },
         "activityId": 
         {
            "description": "Identifier for the activity whose data is represented",
            "type": "string",
         },
         "version": 
         {
            "description": "Version of the survey being answered",
            "type": "string",
         }
      },
      "surveyData": 
      {
         "description": "The set of data provided in response to the survey",
         "required": ["startTime", "endTime", "results"],
         "type": "object",
         "properties": 
         {
            "startTime": 
            { 
               "description": "Time at which the participant started the survey",
               "$ref": "#/definitions/dateTimeValue"
            },
            "endTime": 
            { 
               "description": "Time at which the participant submitted the survey",
               "$ref": "#/definitions/dateTimeValue"
            },
            "results": 
            {
               "description": "The set of results for this survey",
               "type": "array",
               "items": 
               {
                  "$ref": "#/definitions/surveyDataResult"
               },
            }
         }
      },
      "surveyDataResult": 
      {
         "required": ["key", "startTime", "endTime"],
         "type": "object",
         "properties": 
         {
            "startTime":
            { 
               "description": "Time at which the participant started this task",
               "$ref": "#/definitions/dateTimeValue"

            },
            "endTime":
            { 
               "description": "Time at which the participant ended this task",
               "$ref": "#/definitions/dateTimeValue"
            },
            "key":
            { 
               "description": "Unique identifier for the task",
               "type": "string"
            },
            "resultType": 
            {
               "description": "The type of the result",
               "enum": [
                  "scale",
                  "continuousScale",
                  "textScale",
                  "valuePicker",
                  "imageChoice",
                  "textChoice",
                  "boolean",
                  "numeric",
                  "timeOfDay",
                  "date",
                  "text",
                  "email",
                  "timeInterval",
                  "height",
                  "location",
                  "grouped"
               ]
            },
            "skipped": 
            {
               "description": "Indicates whether this task was actively skipped by the user",
               "type": "boolean"
            },
            "value" :
            { 
               "description": "The result of the task",
               "anyOf" : 
               [
                  { "type": "boolean" },
                  { "type": "number" },
                  { "type": "string" },
                  { "type": "null" },
                  { "$ref": "#/definitions/dateValue" },
                  { "$ref": "#/definitions/timeOfDayValue" },
                  { "$ref": "#/definitions/timeValue" },
                  { "$ref": "#/definitions/choiceValue" },
                  { "$ref": "#/definitions/groupedValue" }
               ]
            }
         }
      },
      "dateTimeValue" : 
      {
         "type": "string",
         "description": "represents a date-time",
         "format": "date-time" // yyyy-MM-dd'T'HH:mm:ss.SSSZ
      },
      "timeOfDayValue" :
      {
         "type": "string",
         "descripiton": "represents a time",
         "format" :"^[0-9]{2}:[0-9]{2} [AP]M$" // HH:mm AM/PM
      },
      "timeValue" : 
      {
         "type": "string",
         "description": "represents a timestamp or duration",
         "pattern": "^[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]{3})?([+-][0-9]{4})?$" // HH:mm:ss.SSSZ
      },
      "dateValue" : 
      {
         "type": "string",
         "pattern" : "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" // yyyy-MM-dd
      },
      "choiceValue" : 
      {
         "description": "A collection of one or more choices from a multi-select field",
         "type": "array",
         "items": 
         {
            "type": "string"
         },
      },
      "groupedValue": 
      {
         "description": "a set of results that are related to each other",
         "type" : "array",
         "items": 
         {
            "oneOf" : 
            [
               {"$ref" : "#/definitions/groupedValue"},
               {"$ref" : "#/definitions/surveyDataResult"}
            ]
         }
      }
   }
}
