Define Slot Validation Rules For Slots In An Alexa Skill Dialog Model

Introduction

 
In this article, I am going to demonstrate how to define validation rules for slots and how to define prompts to warn users if a user provides an unacceptable value for any slot this way Alexa can validate user response. Slot Validation is one of the components required to create a dialog model for an Alexa skill. Dialog models are created to simplify the Alexa skills code to collect and confirm slot values. The dialog model identifies the prompts and utterances to collect, validate, and confirms the slot values and intents.
 

Components of a Dialog Model

 
There are four components to the dialog model:
 
Required slots and prompts
 
A required slot represents information that your skill must have in order to fulfill the user's request. For instance, the RegisterBirthday intent requires, at a minimum, the day (day), month (month), and year (year) to save birthday details for the future. You can control the order in which Alexa asks for the slot values.
 
Slot confirmation prompts
 
You can optionally designate a required slot as requiring confirmation. This means that the user must also confirm the slot value with a yes/no response before proceeding.
 
Intent confirmation prompts
 
You can specify that the entire intent requires confirmation. This is common for skills that order products or make reservations. A confirmation prompt typically repeats back to the user all the information previously provided for the user to confirm.
 
Slot validation rules and prompts
 
You can configure a set of validation rules that the slot value provided by the user must pass to be considered valid. If the value fails validation, Alexa can use your prompts to ask for a corrected value. You can use slot validation rules with both required and non-required slots.
 

Creating Custom Alexa Skill

 
A custom Alexa skill named birthday time has been created. The skill contains an intent name RegisterBirthday intent having a day, month, and year as different types of slot values.
 
 
 
Day slot of RegisterBirthday intent
 
 
 

Define slot validation rules

 
Slot validation lets you create validation rules for your slot values. Alexa can then check the user's response against these rules and prompt the user if the user provides an unacceptable value.
 
There are several possible rules for the different types of slots.
 
To create slot validation rules:
  • In the left-hand navigation, find the intent and its list of slots, then click the slot name. Alternatively, from the page for a particular intent, find the slot in the Intent Slots section below the Sample Utterances and click the Edit Dialog link.
  • Under the slot type field, select Validations.
  • Under Create Validation Rule, select the rule you want to use and click the plus.
  • The list shows just the validators that are relevant for the slot's type.
  • Fill in any additional fields specific to the rule, such as the value to compare to. This depends on the rule.
  • Below the rule, enter at least one prompt that Alexa should say to ask the user for a new value.
  • You can include the slot name in curly brackets ({ }) so that Alexa speaks back the value that failed validation.
  • Be sure to save and build the interaction model.
Below, I have defined validation rules for one of the slots of RegisterBirthday intent that is day slot.
 

Creating a validation rule

 
Validation rules are checked when a slot value is spoken by a user. To create a validation rule, we need to choose a validator first. There are two validators available that accept only a set of values and reject only a set of values.
 
We can choose either of the two options depending on what is required in the skill.
 
For the day slot, I have chosen “accept only a set of values” validator. I have also added values that are acceptable under this validation rule. These values are all the days of the month starting from 1 to 31.
 
 
 
Alexa will prompt for a new value. If the user provides unacceptable values which are not within the specified set, Alexa can then check the user's response against these rules and prompt the user if the user provides an unacceptable value. In this case, Alexa can prompt “Please say a valid day”.
 
Json code for defining slot validation is as follows:
  1. "intents": [  
  2.                 {  
  3.                     "name""RegisterBirthdayIntent",  
  4.                     "confirmationRequired"true,  
  5.                     "prompts": {  
  6.                         "confirmation""Confirm.Intent.746177723927"  
  7.                     },  
  8.                     "slots": [  
  9.                         {  
  10.                             "name""day",  
  11.                             "type""AMAZON.Ordinal",  
  12.                             "confirmationRequired"true,  
  13.                             "elicitationRequired"true,  
  14.                             "prompts": {  
  15.                                 "confirmation""Confirm.Slot.1170703155793",  
  16.                                 "elicitation""Elicit.Slot.653974572406"  
  17.                             },  
  18.                             "validations": [  
  19.                                 {  
  20.                                     "type""isInSet",  
  21.                                     "prompt":   
  22.                                     "Slot.Validation.190694681136.1424851421300",  
  23.                                     "values": [  
  24.                                         "22",  
  25.                                         "23",  
  26.                                         "24",  
  27.                                         "25",  
  28.                                         "26",  
  29.                                         "27",  
  30.                                         "28",  
  31.                                         "29",  
  32.                                         "30",  
  33.                                         "31",  
  34.                                         "10",  
  35.                                         "11",  
  36.                                         "12",  
  37.                                         "13",  
  38.                                         "14",  
  39.                                         "15",  
  40.                                         "16",  
  41.                                         "17",  
  42.                                         "18",  
  43.                                         "19",  
  44.                                         "1",  
  45.                                         "2",  
  46.                                         "3",  
  47.                                         "4",  
  48.                                         "5",  
  49.                                         "6",  
  50.                                         "7",  
  51.                                         "8",  
  52.                                         "9",  
  53.                                         "20",  
  54.                                         "21"  
  55.                                     ]  
  56.                                 }  
  57.                             ]  
  58.                         },  
  59.                         {  
  60.                             "name""month",  
  61.                             "type""MonthType",  
  62.                             "confirmationRequired"true,  
  63.                             "elicitationRequired"true,  
  64.                             "prompts": {  
  65.                                 "confirmation""Confirm.Slot.1170703155793",  
  66.                                 "elicitation""Elicit.Slot.282358190483.487594834711"  
  67.                             },  
  68.                             "validations": [  
  69.                                 {  
  70.                                     "type""hasEntityResolutionMatch",  
  71.                                     "prompt":   
  72.                                     "Slot.Validation.190694681136.463018349705"  
  73.                                 }  
  74.                             ]  
  75.                         },  
  76.                         {  
  77.                             "name""year",  
  78.                             "type""AMAZON.FOUR_DIGIT_NUMBER",  
  79.                             "confirmationRequired"true,  
  80.                             "elicitationRequired"true,  
  81.                             "prompts": {  
  82.                                 "confirmation":   
  83.                                 "Confirm.Slot.1170703155793.576520739409",  
  84.                                 "elicitation":   
  85.                                 "Elicit.Slot.1253554922892.1358447173887"  
  86.                             },  
  87.                             "validations": [  
  88.                                 {  
  89.                                     "type""isGreaterThan",  
  90.                                     "prompt":   
  91.                                     "Slot.Validation.1253554922892.1358447173887",  
  92.                                     "value""1900"  
  93.                                 },  
  94.                                 {  
  95.                                     "type""isLessThan",  
  96.                                     "prompt":   
  97.                                     "Slot.Validation.1253554922892.1358447173887",  
  98.                                     "value""2018"  
  99.                                 }  
  100.                             ]  
  101.                         }  
  102.                     ]  
  103.                 }  
  104.             ]   
Output
 
 
From the output, we can see that after invoking the skill, Alexa asks for the date of birth. If the user provides an unacceptable value, then Alexa will prompt a message saying “Please say a valid day” to ensure the user provides a valid day of the month.
 

Summary

 
In this article, I have demonstrated how to define validation rules for slots and how to define prompts to warn users if a user provides an unacceptable value for any slot this way Alexa can validate user response. I have also explained one of the components of the dialog model. Proper examples of Alexa speech prompts and user utterances are provided along with the JSON code snippet of the interaction model.