Skip to content

Commit

Permalink
add basic schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Urkem committed Oct 16, 2023
1 parent bae28b8 commit 1c978eb
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 90 deletions.
259 changes: 180 additions & 79 deletions rasa/shared/core/flows/flows_yaml_schema.json
Original file line number Diff line number Diff line change
@@ -1,109 +1,210 @@
{
"type": "object",
"required": [
"flows"
],
"properties": {
"flows": {
"type": "object",
"minProperties": 1,
"additionalProperties": false,
"patternProperties": {
"^[A-Za-z_][A-Za-z0-9_]*$": {
"$ref": "#$defs/flow"
}
}
}
},
"$defs": {
"steps": {
"type": "array",
"minContains": 1,
"items": {
"type": "object",
"oneOf": [
{
"required": [
"action"
]
},
{
"required": [
"collect"
]
},
{
"required": [
"link"
]
},
{
"required": [
"set_slots"
]
}
],
"properties": {

"name": {
"action": {
"type": "string"
},
"ask_before_filling": {
"type": "boolean"
},
"collect": {
"type": "string"
},
"description": {
"type": "string"
},

"if": {
"type": "string",
"description": "Flow guard condition"
"id": {
"type": "string"
},

"nlu_trigger": {
"type": "object",
"properties":{
"nlu_trigger": {
"type": "array",
"items": {
"type": "object",
"required": ["intent"],
"properties": {
"intent": {
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string"
},
"confidence_threshold": {
"type": "number"
}
}
}
}
}
}
}
"link": {
"type": "string"
},
"next": {
"$ref": "#$defs/next"
},
"reset_after_flow_ends": {
"type": "boolean"
},
"set_slots": {
"$ref": "#$defs/set_slots"
},
"utter": {
"type": "string"
},

"steps": {
"rejections": {
"type": "array",
"items": {
"type": "object",
"required": [
"if",
"utter"
],
"properties": {
"collect": {
"if": {
"type": "string"
},
"description": {
"utter": {
"type": "string"
},
"ask_before_filling": {
"type": "boolean"
},
"next": {
"type": "array",
"items": {
"type": "object",
"properties": {
"if": {
"type": "string"
},
"then": {
"type": "array",
"items": {
"type": "object",
"properties": {
"action": {
"type": "string"
},
"next": {
"type": "string"
}
},
"required": ["action", "next"]
}
}
},
"required": ["if", "then"]
}
}
}
}
}
}
},
"flow": {
"required": [
"name",
"steps"
],
"type": "object",
"properties": {
"description": {
"type": "string"
},
"if": {
"type": "string"
},
"name": {
"type": "string"
},
"nlu_trigger": {
"type": "array",
"items": {
"required": [
"intent"
],
"type": "object",
"properties": {
"intent": {
"properties": {
"confidence_threshold": {
"type": "number"
},
"name": {
"type": "string"
}
},
"link": {
"type": "string"
},
"id": {
"type": "string"
},
"action": {
"type": "string"
}
"required": [
"name"
],
"type": "object"
}
}
}
},
"steps": {
"$ref": "#$defs/steps"
}
}
},
"next": {
"anyOf": [
{
"type": "array",
"minContains": 1,
"items": {
"type": "object",
"oneOf": [
{
"required": [
"if",
"then"
]
},
"required": ["next"]
{
"required": [
"else"
]
}
],
"properties": {
"else": {
"oneOf": [
{
"type": "string"
},
{
"$ref": "#$defs/steps"
}
]
},
"if": {
"type": "string"
},
"then": {
"oneOf": [
{
"$ref": "#$defs/steps"
},
{
"type": "string"
}
]
}
}
}
},
"required": ["name", "steps"]
{
"enum": [
"END"
]
}
]
},
"set_slots": {
"type": "array",
"items": {
"type": "object",
"patternProperties": {
"^[A-Za-z_][A-Za-z0-9_]*$": {
"type": "string"
}
}
}
}
},
"required": ["flows"]
}
}
}
9 changes: 5 additions & 4 deletions rasa/shared/core/flows/yaml_flows_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

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


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

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)

flows = FlowsList.from_json(yaml_content.get(KEY_FLOWS, {}))
if not skip_validation:
flows.validate()
Expand Down
23 changes: 16 additions & 7 deletions rasa/shared/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,25 @@ def validate_training_data_format_version(
)
return False

def validate_yaml_with_schema(json_data: Dict[Text, Any], schema: Dict[Text, Any]) -> None:
from jsonschema import validate
from jsonschema import ValidationError

def validate_dict_with_schema(data: Dict[Text, Any], schema: Dict[Text, Any]) -> None:
"""Validate data format.
Args:
data (Dict[Text, Any]): Data to validate
schema (Dict[Text, Any]): jsonschema to validate against
Raises:
SchemaValidationError: if validation fails.
"""
from jsonschema import validate, ValidationError

try:
validate(json_data, schema)
except ValidationError as e:
e.message += (
validate(data, schema)
except ValidationError as error:
error.message += (
f". Failed to validate data, make sure your data "
f"is valid. For more information about the format visit "
f"{DOCS_URL_TRAINING_DATA}."
)
raise SchemaValidationError.create_from(e) from e
raise SchemaValidationError.create_from(error) from error

0 comments on commit 1c978eb

Please sign in to comment.