Custom validation in scripting

Set custom validation logic for certain questions on the page. Custom validation occurs onComplete.

Note:
  • Multiple questions can be set as invalid in the same script.
  • You cannot set an invalid input as valid and bypass default validation. After an onComplete script runs, the server will validate all data points posted to the server. Custom validation can only apply tighter validation.
  • A generic error may occur if question properties that were set in the UI, and custom validation, contradict each other.
  • Only set visible questions on the current page as invalid. Anything else will likely cause errors.
Function Description
response.setError("postalCode", "Please provide a postal code within North Vancouver") Question validation. In this example, the respondent is prompted to provide a valid postal code from North Vancouver.
response.setError("orderNumber", "Could not find the provided order number. Please enter a valid number.") Question validation. In this example, the respondent is prompted to provide a valid order number.

Response Payload (Validation Error)

In case of a custom validation error, the responding service will return a 400 response code (Unprocessable Entry). The response body will contain the following payload:

{
    "status": 400,
    "message": "Cannot navigate next with invalid step(s) on the page.",
    "developerMessage": null,
    "errorId": "39506622-6a9e-e252-a1da-459d27efdb30",
    "errorDetails": {
        "invalidSteps": [
            {
                "path": "steps/e5b0e35d-8fbd-4ce0-802a-d29de1133485",
                "validationError": "Number 2 must be greater than 10"
            },
            {
                "path": "steps/e3ead579-c8cc-4c88-950e-6213f3a32a99"
            }
        ]
    }
}