Skip to content

Commit

Permalink
Add support of validation of slots with . in name
Browse files Browse the repository at this point in the history
  • Loading branch information
d61h6k4 committed Mar 13, 2023
1 parent b3ebe7a commit cba91fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rasa_sdk/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async def get_validation_events(
slots.pop(slot_name)
continue

method_name = f"validate_{slot_name.replace('-','_')}"
method_name = f"validate_{slot_name.replace('-','_').replace('.', '_')}"
validate_method = getattr(self, method_name, None)

if not validate_method:
Expand Down
52 changes: 49 additions & 3 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
)
from rasa_sdk.slots import SlotMapping


DEFAULT_DOMAIN = {
"slots": {
"some_other_slot": {
Expand Down Expand Up @@ -759,9 +758,9 @@ async def extract_my_slot(
(["my_slot", "other_slot"], {}, [SlotSet(REQUESTED_SLOT, "other_slot")]),
# Extract method for slot which is also mapped in domain
(
["my_slot"],
["my_slot", "other_slot"],
{"forms": {"some_form": {"required_slots": ["my_slot"]}}},
[],
[SlotSet(REQUESTED_SLOT, "other_slot")],
),
],
)
Expand Down Expand Up @@ -874,3 +873,50 @@ async def required_slots(
dispatcher = CollectingDispatcher()
events = await form.run(dispatcher=dispatcher, tracker=tracker, domain=domain)
assert events == expected_return_events


async def test_form_validation_space_slot():
form_name = "some_form"

class TestFormValidationSpaceSlotAction(FormValidationAction):
def name(self) -> Text:
return form_name

def validate_space_slot(
self,
slot_value: Any,
dispatcher: "CollectingDispatcher",
tracker: "Tracker",
domain: "DomainDict",
) -> Dict[Text, Any]:
if slot_value == "correct_value":
return {
"space.slot": "validated_value",
}
return {
"space.slot": None,
}

form = TestFormValidationSpaceSlotAction()

# tracker with active form
tracker = Tracker(
"default",
{},
{},
[SlotSet("space.slot", "correct_value")],
False,
None,
{"name": form_name, "is_interrupted": False, "rejected": False},
"action_listen",
)

dispatcher = CollectingDispatcher()
events = await form.run(
dispatcher=dispatcher,
tracker=tracker,
domain={"forms": {form_name: {"required_slots": ["space.slot"]}}},
)
assert events == [
SlotSet("space.slot", "validated_value"),
]

0 comments on commit cba91fa

Please sign in to comment.