Multiple Validation On A Slot Type In An Interaction Dialog model


In this article, I am going to explain how multiple validations can be applied to a slot in a dialog model of Alexa skill. 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.

Slot validation

Slot validation provides an additional dialog model component to help you manage the conversation between your skill and the user. You can configure a set of validation rules for a slot, along with prompts to use for slot values that fail validation.
When the user fills the slot with an utterance and you delegate the dialog to Alexa, Alexa checks the provided value against your rules. If the value fails the validation, Alexa uses your prompts to ask the user for a correct value.
For example, in this dialog, the user is filling a custom Planet slot that only accepts values that match the names of planets in our solar system,
  • User - What is the weather like on the sun? (The value the sun is not an acceptable Planet slot value, so Alexa prompts for a new value.)
  • Alexa - The Sun is not a planet within our solar system. Please tell me a planet instead.
  • User - OK, Mars (The value mars is an acceptable value for a Planet slot.)
  • Alexa - On Mars, high of 70 and low of minus 195 fahrenheit. Sunny with a chance of sandstorms later in the day.
To use slot validation,
  • Define validation rules and prompts for your slots where needed. These become part of the dialog model for the skill. There are several possible rules for the different types of slots.
  • Set up your skill to delegate the dialog to Alexa

Use multiple validation rules for a slot

You can create multiple validation rules for a slot. The rules are checked in the order in which they are shown in the developer console or listed in the JSON. If a value fails a rule, Alexa stops checking and responds with the prompt to correct the value. In a set of multiple rules, a later rule is checked only when all preceding rules have passed.
This means that you can define rules to provide smarter responses depending on the user's mistake. For example, suppose your skill provides facts about planets and uses a custom slot type with the planets of the solar system. You could use a single validation (Accept only Slot type's values and synonyms) to only accept the planet names and provide a generic prompt like "{Planet} is not a planet. Please tell me a planet in our solar system."
However, you know that users might ask about other objects in the solar system that are not planets. Rather than using this generic prompt, you could set up an additional validation to reject these specific values (Reject only a set of values) with a more specific prompt before applying the more generic validation.
For example, you could define these rules for the Planet slot,
Reject only a set of values with the values "the sun", "sun", and "our sun". Prompts,
  • "While the sun is part of our solar system, I can only answer this question for planets. Please tell me a planet."
  • "We don't think of the sun as having weather, exactly, so please tell me a planet instead."
  • (similar prompts showing that Alexa understands the value provided)
Accept only Slot type's values and synonyms. Prompts,
  • {Planet} is not a planet. Please tell me a planet in our solar system.
  • (similar generic prompts that repeat back the incorrect value).
JSON code for Validation Rules
  1. {  
  2.   "interactionModel": {  
  3.     "dialog": {  
  4.       "delegationStrategy""SKILL_RESPONSE",        
  5.       "intents": [  
  6.         {  
  7.           "name""GetPlanetWeather",  
  8.           "delegationStrategy""ALWAYS",  
  9.           "confirmationRequired"false,  
  10.           "prompts": {},  
  11.           "slots": [  
  12.             {  
  13.               "name""planet",  
  14.               "type""Planet",  
  15.               "confirmationRequired"false,  
  16.               "elicitationRequired"true,  
  17.               "prompts": {  
  18.                 "elicitation""Elicit.Slot.27936398519.1404759397895"  
  19.               },  
  20.               "validations": [  
  21.                 {  
  22.                   "type""isNotInSet",  
  23.                   "prompt""Slot.Validation.154616755422.1503388152026",  
  24.                   "values": [  
  25.                     "sol",  
  26.                     "the sun",  
  27.                     "our sun",  
  28.                     "our star",  
  29.                     "sun"  
  30.                   ]  
  31.                 },  
  32.                 {  
  33.                   "type""hasEntityResolutionMatch",  
  34.                   "prompt""Slot.Validation.154616755422.1503388152026"  
  35.                 }  
  36.               ]  
  37.             }  
  38.           ]  
  39.         }  
  40.       ],  
  41.     },  
  42.     "prompts": [  
  43.       {  
  44.         "id""Elicit.Slot.27936398519.1404759397895",  
  45.         "variations": [  
  46.           {  
  47.             "type""PlainText",  
  48.             "value": "Tell me a planet and I'll give you an idea of the weather   
  49.                           there"  
  50.           },  
  51.           {  
  52.             "type""PlainText",  
  53.             "value""For what planet?"  
  54.           },  
  55.           {  
  56.             "type""PlainText",  
  57.             "value""Tell me a planet in our solar system"  
  58.           },  
  59.           {  
  60.             "type""PlainText",  
  61.             "value""What planet did you want to know about?"  
  62.           }  
  63.         ]  
  64.       },  
  65.       {  
  66.         "id""Slot.Validation.154616755422.1503388152026.257948657476",  
  67.         "variations": [  
  68.           {  
  69.             "type""PlainText",  
  70.             "value": "While the sun is the center of our solar system, it is not a   
  71.                           planet. Please tell me a planet."  
  72.           },  
  73.           {  
  74.             "type""PlainText",  
  75.             "value""Only planets have weather. Please tell me a planet"  
  76.           },  
  77.           {  
  78.             "type""PlainText",  
  79.             "value": "We don't think of the sun as having weather, exactly, so   
  80.                           please tell me a planet instead."  
  81.           },  
  82.           {  
  83.             "type""PlainText",  
  84.             "value": "While the sun is part of our solar system, I can only answer   
  85.                           this question for planets. Please tell me a planet."  
  86.           }  
  87.         ]  
  88.       },  
  89.       {  
  90.         "id""Slot.Validation.154616755422.1503388152026.1155325818232",  
  91.         "variations": [  
  92.           {  
  93.             "type""PlainText",  
  94.             "value": "Please tell me a planet like Earth, Mars, or Jupiter. You   
  95.                           know, one of the nine in the solar system."  
  96.           },  
  97.           {  
  98.             "type""PlainText",  
  99.             "value": "{planet} is not a planet -- I need one of the nine planets in   
  100.                           our solar system"  
  101.           },  
  102.           {  
  103.             "type""PlainText",  
  104.             "value": "I did not recognize {planet} as a planet in our solar system.   
  105.                           Please tell me a valid planet."  
  106.           },  
  107.           {  
  108.             "type""PlainText",  
  109.             "value""Please tell me a planet within our solar system"  
  110.           }  
  111.         ]  
  112.       }  
  113.     ]  
  114.   }  
  115. }  
You can see and edit the JSON representation of slot validations within your dialog model in the JSON Editor. Validations for a slot are defined in the validations array in interactionModel.dialog.intents[].slots[].validations. The corresponding prompts are in prompts.


In this article, I explained about slot validation, and how to use slot validation for different slot types. I also explained how multiple validation can be defined for a single slot in a dialog model. Proper examples and coding snippets are provided.