{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://labkey.org/schemas/surveyDesign.json",
  "title": "Activity Metadata",
  "type": "object",
  "required": [
    "activity",
  ],
  "properties": {
    "activity": "#/definitions/activity",
    "description": "The activity "
  },
  "definitions": {
    "activity": {
      "description": "The activity metadata and steps",
      "type": "object",
      "required": [
        "metadata",
        "steps"
        ],
      "properties": {
        "metadata": {
          "$ref": "#/definitions/surveyMetadata",
          "description": "Metadata to identify the survey and its version"
        },
        "steps": {
          "description": "The steps in the activity",
          "type": "array",
          "items": {
            "$ref": "#/definitions/surveyStep"
          }
        }
      }
    },
    "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 survey being responded to",
        "type": "string"
      },
      "name": {
        "descirption": "The display name of the survey",
        "type": "string"
      },
      "version": {
        "description": "Version of the survey being answered",
        "type": "string"
      }
    },
    "surveyStep": {
      "description": "The set of data provided in response to the survey",
      "required": [
        "type",
        "resultType",
        "key"
      ],
      "type": "object",
      "properties": {
        "key": {
          "description": "Unique, mnemonic key to identify this step",
          "type": "string"
        },
        "title": {
          "description": "The descriptive text for the step (e.g., the text of the question)",
          "type": "string"
        },
        "skippable": {
          "description": "Indicator of whether a step can be skipped by the user",
          "type": "boolean"
        },
        "repeatable": {
          "description": "Indicator of whether a step is repeatable (valid only for form steps)",
          "type": "boolean"
        },
        "phi": {
          "description": "Indication of the PHI level for the response to this step.  \n                          Required for all steps that have a responseType",
          "enum": [
            "PHI",
            "Limited",
            "NotPHI"
          ]
        },
        "type": {
          "description": "The type of step in the survey",
          "enum": [
            "form",
            "instruction",
            "question",
            "task"
          ]
        },
        "resultType": {
          "description": "The type of the result from this survey. Required for all question-* type steps.",
          "enum": [
            "scale",
            "continuousScale",
            "textScale",
            "valuePicker",
            "imageChoice",
            "textChoice",
            "boolean",
            "numeric",
            "timeOfDay",
            "date",
            "text",
            "email",
            "timeInterval",
            "height",
            "location",
            "grouped",
            "fetalKickCounter",
            "towerOfHanoi",
            "spatialSpanMemory"
          ]
        },
        "format": {
          "description": "The data necessary to specify the format of the result for validation and display",
          "anyOf": [
            {
              "$ref": "#/definitions/numericFormat"
            },
            {
              "$ref": "#/definitions/textFormat"
            },
            {
              "$ref": "#/definitions/textChoiceFormat"
            },
            {
              "$ref": "#/definitions/dateFormat"
            }
            /* other formats ommited for brevity as they contain no fields we require for the schema */
          ]
        }
      }
    },
    "textFormat": {
      "description": "Format specification for a text field",
      "type": "object",
      "required": [
        "maxLength"
      ],
      "properties": {
        "maxLength": {
          "description": "The maximum length of the text field; use 0 to indicate no limit",
          "type": "number"
        },
        "validationRegex": {
          "description": "Regular expression used for validating the text that is entered",
          "type": "string"
        },
        "invalidMessage": {
          "description": "Message to display if the entered text is invalid",
          "type": "string"
        },
        "multipleLines": {
          "description": "Indicator if the text entry should spread over multiple lines or not",
          "type": "boolean"
        },
        "placeholder": {
          "type": "string"
        }
      }
    },
    "numericFormat": {
      "description": "Format specification for a numeric field",
      "type": "object",
      "required": [
        "style"
      ],
      "properties": {
        "style": {
          "description": "Indicator of the type of the number",
          "enum": ["Integer", "Decimal"]
        },
        "unit": {
          "description": "The unit of the number",
          "type": "string"
        },
        "minValue": {
          "description": "Minimum value for the number",
          "type": "number"
        },
        "maxValue": {
          "description": "Maximum value for the number",
          "type": "number"
        },
        "placeholder": {
          "type": "string"
        }
      }
    },
    "textChoiceFormat": {
      "description": "Format for specifying a set of text choices",
      "type": "object",
      "required": [
        "textChoices",
        "selectionStyle"
      ],
      "textChoices": {
        "description": "The set of choices to display",
        "type": "array",
        "items": {
          "type": "string",
          "$ref": "#/definitions/other"
        }
      },
      "selectionStyle": {
        "description": "Indicator of the cardinality of the selection available",
        "enum": ["Single", "Multiple"]
      }
    },
    "dateFormat": {
      "description": "Format for a date field",
      "type": "object",
      "required": [
        "style"
      ],
      "style": {
        "description": "Indicator of whether timestamps are included or not",
        "enum": ["Date", "Date-Time"]
      },
      "minDate": {
        "description": "The minimum allowable date",
        "type": "string"
      },
      "maxDate": {
        "description": "The maximum allowable date",
        "type": "string"
      },
      "default": {
        "description": "The default date to show in the field",
        "type": "string"
      }
    },
    "other": {
      "description": "Indicator of requirement and format of respondent customizable choice selection",
      "type": "object",
      "required": [
        "textfieldReq",
        "placeholder",
        "isMandatory"
      ],
      "textfieldReq": {
        "description": "Flag indicating custom text input is shown to respondent",
        "type": "boolean"
      },
      "placeholder": {
        "description": "Empty field text shown to respondent",
        "type": "string"
      },
      "isMandatory": {
        "description": "Flag indicating custom text must not be empty",
        "type": "boolean"
      }
    }
  }
}
