Skip to content

Commit

Permalink
add fail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Urkem committed Oct 16, 2023
1 parent 1c978eb commit c38fd21
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 86 deletions.
180 changes: 122 additions & 58 deletions rasa/shared/core/flows/flows_yaml_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"flows"
],
"properties": {
"version": {
"type": "string"
},
"flows": {
"type": "object",
"minProperties": 1,
"additionalProperties": false,
"patternProperties": {
"^[A-Za-z_][A-Za-z0-9_]*$": {
"$ref": "#$defs/flow"
Expand All @@ -25,82 +27,146 @@
{
"required": [
"action"
]
],
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"action": {
"type": "string"
},
"next": {
"$ref": "#$defs/next"
}
}
},
{
"required": [
"collect"
]
],
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"description":{
"type": "string"
},
"collect": {
"type": "string"
},
"ask_before_filling": {
"type": "boolean"
},
"reset_after_flow_ends": {
"type": "boolean"
},
"utter": {
"type": "string"
},
"rejections": {
"type": "array",
"items": {
"type": "object",
"required": [
"if",
"utter"
],
"properties": {
"if": {
"type": "string"
},
"utter": {
"type": "string"
}
}
}
},
"next": {
"$ref": "#$defs/next"
}
}
},
{
"required": [
"link"
]
],
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"link": {
"type": "string"
}
}
},
{
"required": [
"set_slots"
]
}
],
"properties": {
"action": {
"type": "string"
},
"ask_before_filling": {
"type": "boolean"
},
"collect": {
"type": "string"
},
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"link": {
"type": "string"
},
"next": {
"$ref": "#$defs/next"
},
"reset_after_flow_ends": {
"type": "boolean"
],
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"reset_after_flow_ends": {
"type": "boolean"
},
"set_slots": {
"$ref": "#$defs/set_slots"
}
}
},
"set_slots": {
"$ref": "#$defs/set_slots"
{
"required": [
"next"
],
"additionalProperties": false,
"properties": {
"next": {
"$ref": "#$defs/next"
},
"id": {
"type": "string"
}
}
},
"utter": {
"type": "string"
{
"required": [
"generation_prompt"
],
"additionalProperties": false,
"properties": {
"generation_prompt": {
"type": "string"
},
"id": {
"type": "string"
}
}
},
"rejections": {
"type": "array",
"items": {
"type": "object",
"required": [
"if",
"utter"
],
"properties": {
"if": {
"type": "string"
},
"utter": {
"type": "string"
}
{
"required": [
"id"
],
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
}
}
}
}
]
}
},
"flow": {
"required": [
"name",
"steps"
],
"type": "object",
"additionalProperties": false,
"properties": {
"description": {
"type": "string"
Expand All @@ -120,6 +186,7 @@
"type": "object",
"properties": {
"intent": {
"type": "object",
"properties": {
"confidence_threshold": {
"type": "number"
Expand All @@ -130,8 +197,7 @@
},
"required": [
"name"
],
"type": "object"
]
}
}
}
Expand Down Expand Up @@ -189,9 +255,7 @@
}
},
{
"enum": [
"END"
]
"type": "string"
}
]
},
Expand All @@ -207,4 +271,4 @@
}
}
}
}
}
12 changes: 7 additions & 5 deletions rasa/shared/core/flows/yaml_flows_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import textwrap
from pathlib import Path
from typing import List, Text, Union

from rasa.shared.core.flows.utils import KEY_FLOWS

import rasa.shared.utils.io
Expand All @@ -9,7 +10,7 @@

from rasa.shared.core.flows.flow import Flow, FlowsList

FLOWS_SCHEMA_FILE = "rasa/shared/core/flows/flows_yaml_schema.json"
FLOWS_SCHEMA_FILE = "shared/core/flows/flows_yaml_schema.json"


class YAMLFlowsReader:
Expand Down Expand Up @@ -52,11 +53,12 @@ def read_from_string(cls, string: Text, skip_validation: bool = False) -> FlowsL
Returns:
`Flow`s read from `string`.
"""
yaml_content = rasa.shared.utils.io.read_yaml(string)

if not skip_validation:
schema = rasa.shared.utils.io.read_yaml_file(FLOWS_SCHEMA_FILE)
rasa.shared.utils.validation.validate_dict_with_schema(yaml_content, schema)
rasa.shared.utils.validation.validate_yaml_with_jsonschema(
string, FLOWS_SCHEMA_FILE
)

yaml_content = rasa.shared.utils.io.read_yaml(string)

flows = FlowsList.from_json(yaml_content.get(KEY_FLOWS, {}))
if not skip_validation:
Expand Down
30 changes: 26 additions & 4 deletions rasa/shared/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,42 @@ def validate_training_data_format_version(
return False


def validate_dict_with_schema(data: Dict[Text, Any], schema: Dict[Text, Any]) -> None:
def validate_yaml_with_jsonschema(
yaml_file_content: Text, schema_path: Text, package_name: Text = PACKAGE_NAME
) -> None:
"""Validate data format.
Args:
data (Dict[Text, Any]): Data to validate
schema (Dict[Text, Any]): jsonschema to validate against
yaml_file_content: the content of the yaml file to be validated
schema_path: the schema of the yaml file
package_name: the name of the package the schema is located in. defaults
to `rasa`.
Raises:
YamlSyntaxException: if the yaml file is not valid.
SchemaValidationError: if validation fails.
"""
from jsonschema import validate, ValidationError
from ruamel.yaml import YAMLError
import pkg_resources

schema_file = pkg_resources.resource_filename(package_name, schema_path)
schema_content = rasa.shared.utils.io.read_yaml_file(schema_file)

try:
# we need "rt" since
# it will add meta information to the parsed output. this meta information
# will include e.g. at which line an object was parsed. this is very
# helpful when we validate files later on and want to point the user to the
# right line
source_data = rasa.shared.utils.io.read_yaml(
yaml_file_content, reader_type=["safe", "rt"]
)
except (YAMLError, DuplicateKeyError) as e:
raise YamlSyntaxException(underlying_yaml_exception=e)

try:
validate(data, schema)
validate(source_data, schema_content)
except ValidationError as error:
error.message += (
f". Failed to validate data, make sure your data "
Expand Down
4 changes: 2 additions & 2 deletions tests/cli/test_rasa_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ def test_rasa_data_validate_flows_success(
name: transfer money
steps:
- id: "ask_recipient"
collect_information: transfer_recipient
collect: "transfer_recipient"
next: "ask_amount"
- id: "ask_amount"
collect_information: transfer_amount
collect: "transfer_amount"
next: "execute_transfer"
- id: "execute_transfer"
action: action_transfer_money"""
Expand Down

0 comments on commit c38fd21

Please sign in to comment.