Validate User Input

Learn how to configure validation of user input.

validations (optional)

A set of validation rules before the entity is persisted in the bot memory.

Validation reaction example.

You can use the following validation types:

  • phone
  • email
  • regex
  • length
  • range
  • match
  • image
  • custom

Phone

{
  "type": "question",
  "entity": "contactPhone",
  "entity-type": "Text",
  "messages": [
    "What is your phone number?"
  ],
  "reactions": {
    "validations": [
      {
        "type": "phone",
        "error-message": [
          "I am not sure it's a valid phone number. Can you type it again?"
        ]
      }
    ]
  }
}

Email

{
  "type": "question",
  "entity": "contactEmail",
  "entity-type": "Text",
  "messages": [
    "What is your email?"
  ],
  "reactions": {
    "validations": [
      {
        "type": "email",
        "error-message": [
          "I am not sure it's a valid email. Can you type it again?"
        ]
      }
    ]
  }
}

Regex

{
  "type": "question",
  "entity": "licensePlate",
  "entity-type": "Text",
  "messages": [
    "What is your car's license plate in order to receive a free parking spot as part of your booking?"
  ],
  "reactions": {
    "validations": [
      {
        "type": "regex",
        "parameters": {
          "value": "(\\b\\w[A-Z]?\\d{4}\\w[A-Z]\\b)|(\\b\\w[A-Z]?\\s?[A-Z0-9]{6}\\b)|(\\b\\w[A-Z]?\\d{4}[A-Z]\\b)|(\\bC[CT]?\\s?\\d{4}\\b)|(\\bXX\\s?\\d{4}\\b)"
        },
        "error-message": [
          "I am not sure it's a valid license plate number. Can you type it again?"
        ]
      }
    ]
  }
}

Length

Evaluate a length of a string. Set min and max length.

Note: Works only with strings, not numbers.

{
  "entity": "quickNote",
  "entity-type": "Text",
  "messages": [
    "Leave a quick note for your doctor up to 200 characters."
  ],
  "reactions": {
    "validations": [
      {
        "type": "length",
        "parameters": {
          "min": 1,
          "max": 200
        },
        "error-message": [
          "The quick note should be between 1 and 200 characters"
        ]
      }
    ]
  }
}

Range

You can also use a range for validating Date, Time or Number. Note that the user’s input might be in the format “I think 2 o’clock in the afternoon is perfect for me.” and NativeChat will process the request and send for validation “14:00”.

{
    "entity": "appointmentTime",
    "entity-type": "Time",
    "messages": [
        "Please enter the time during which you want to meet with {{selectedDoctor}}."
    ],
    "reactions": {
        "validations": [
            {
                "type": "range",
                "parameters": {
                    "min": "7:00",
                    "max": "18:00"
                },
                "error-message": [
                    "The working hours for the hospital are 7:00 AM to 6:00 PM. Please enter a time during these hours."]
            }
        ]
    }
}

Match

{
  "entity": "confirmation",
  "entity-type": "Text",
  "is-explicit": true,
  "messages": [
    "Do you confirm your booking with {{selectedDoctor}}?"
  ],
  "display": {
    "type": "quick-reply",
    "data": [
      "Yes",
      "No"
    ]
  },
  "reactions": {
    "validations": [
      {
        "type": "match",
        "parameters": {
          "values": [
            "Yes",
            "yes"
          ]
        },
        "error-message": [
          "Please, enter the change that you would like to make to your appointment."
        ]
      }
    ]
  }
}

Image

Image validation uses image recognition to validate that a file is an image of something specified. It requires one parameter - contains, which is an array of acceptable things that could be recognized in the image. If any of the enumerated things is present in the uploaded file then the validation is successful.

{
  "type": "question",
  "entity": "receiptPicture",
  "entity-type": "File",
  "entity-display": "Receipt picture",
  "messages": [
    "Can you take a picture of your billed amount?"
  ],
  "reactions": {
    "validations": [
      {
        "type": "image",
        "error-message": [
          "Even though I enjoy {{receiptPicture.labels.[0]}} pictures, we'll need a photo with the invoice of the billed amount."
        ],
        "parameters": {
          "contains": [
            "receipt", "document"
          ]
        }
      }
    ]
  }
}

Custom

You can create a custom validation that evaluates an expression. Use the condition parameter to set the expression. Use any of the condition rules for the expression.

Note that you can use the current user input by simply typing {{entity}} which will be evaluated from the current input. If the validation fails, the {{entity}} won’t be persistent in the bot memory.

{
  "entity": "pinCode",
  "entity-type": "PinCode",
  "messages": [
    "Please type your pin code number."
  ],
  "reactions": {
    "validations": [
      {
        "type": "custom",
        "parameters": {
          "condition": "{{$eq pinCode '1234'}}"
        },
        "error-message": [
          "The pin code number is not valid. Please make sure to enter a valid pin code number."
        ]
      }
    ]
  }
}

You can also call a service and use the response in the validation expression. Use the special _response entity to access the response from the service. The response can be either a text or a JSON. Check the data-source documentation for more information on setting up the request.

{
  "entity": "pinCode",
  "entity-type": "PinCode",
  "messages": [
    "Please type your pin code number."
  ],
  "reactions": {
    "validations": [
      {
        "type": "custom",
        "parameters": {
          "data-source": {
            "endpoint": "http://some.validation.service/checkPinCode?pinCodeToValidate={{$encodeURI pinCode}}"
          },
          "condition": "{{$eq _response 'valid'}}"
        },
        "error-message": [
          "The pin code number is not valid. Please make sure to enter a valid pin code number that hasn't expired."
        ]
      }
    ]
  }
}
{
  "entity": "pinCode",
  "entity-type": "PinCode",
  "messages": [
    "Please type your pin code number."
  ],
  "reactions": {
    "validations": [
      {
        "type": "custom",
        "parameters": {
          "data-source": {
            "endpoint": "http://some.validation.service/getValidPinCodes",
            "selector": "$.Result[:].code"
          },
          "condition": "{{$in pinCode _response}}"
        },
        "error-message": [
          "The pin code number is not valid. Please make sure to enter a valid pin code number that hasn't expired."
        ]
      }
    ]
  }
}

If you have previously saved the result from an external web service in a context entity, you can use that entity directly instead of calling the service again.

Example of a context:

{
  "doctors": [
    {
      "Name": "John Burke",
      "Id" : "123"
    },
     {
      "Name": "Marry Cannon",
      "Id" : "456"
    }
  ]
}
{
  "entity": "doctor",
  "entity-type": "Doctor",
  "messages": [
    "Which is the doctor that you want to meet with?"
  ],
  "reactions": {
    "validations": [
      {
        "type": "custom",
        "parameters": {
          "data-source": {
            "entity": "doctors",
            "selector": "$.Id[:]"
          },
          "condition": "{{$in doctor.Id _response}}"
        },
        "error-message": [
          "The doctor you have selected is not available."
        ]
      }
    ]
  }
}

Multiple validations

You can chain multiple validation steps for a single question step that will be checked one at a time.

{
  "entity": "pinCode",
  "entity-type": "PinCode",
  "messages": [
    "Please type your 5 digit pin code number."
  ],
  "reactions": {
    "validations": [
      {
        "type": "regex",
        "parameters": {
          "value": "^[0-9]{5}$"
        },
        "error-message": [
          "Your pin code should be a 5 digit number. Please enter your pin code number again."
        ]
      },
      {
        "type": "custom",
        "parameters": {
          "data-source": {
            "endpoint": "http://some.validation.service/checkPinCode?pinCodeToValidate={{$encodeURI pinCode}}"
          },
          "condition": "{{$eq _response 'valid'}}"
        },
        "error-message": [
          "The pin code number is not valid. Please make sure to enter a valid pin code number that hasn't expired."
        ]
      }
    ]
  }
}