Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ATO-1315] Flows yaml schema #12901

Merged
merged 30 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/12901.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added Schema file and schema validation for flows.
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ flows:
- action: validate_{{context.collect}}
next:
- if: "{{context.collect}} is not null"
then: "done"
then: "END"
- else: "ask_collect"
- id: "ask_collect"
action: "{{context.utter}}"
next: "listen"
- id: "listen"
action: action_listen
next: "start"
- id: "done"


pattern_code_change:
description: flow used to clean the stack after a bot update
Expand Down
286 changes: 286 additions & 0 deletions rasa/shared/core/flows/flows_yaml_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
{
"type": "object",
"required": [
"flows"
],
"properties": {
"version": {
"type": "string"
},
"flows": {
"type": "object",
"patternProperties": {
"^[A-Za-z_][A-Za-z0-9_]*$": {
"$ref": "#$defs/flow"
}
}
}
},
"$defs": {
"steps": {
"type": "array",
"minContains": 1,
"items": {
"type": "object",
"oneOf": [
{
"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"
},
"next": {
"type": "null"
}
}
},
{
"required": [
"set_slots"
],
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"set_slots": {
"$ref": "#$defs/set_slots"
},
"next": {
"$ref": "#$defs/next"
}
}
},
{
"required": [
"next"
ancalita marked this conversation as resolved.
Show resolved Hide resolved
],
"additionalProperties": false,
"properties": {
"next": {
"$ref": "#$defs/next"
},
"id": {
"type": "string"
}
}
},
{
"required": [
"generation_prompt"
],
"additionalProperties": false,
"properties": {
"generation_prompt": {
"type": "string"
},
"id": {
"type": "string"
},
"next": {
"$ref": "#$defs/next"
}
}
},
{
"required": [
"entry_prompt"
],
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"entry_prompt": {
"type": "string"
},
"next": {
"$ref": "#$defs/next"
}
}
}
]
}
},
"flow": {
"required": [
"steps"
],
"type": "object",
"additionalProperties": false,
"properties": {
"description": {
"type": "string"
},
"if": {
"type": "string"
},
"name": {
"type": "string"
},
"nlu_trigger": {
"type": "array",
"items": {
"required": [
"intent"
],
"type": "object",
"additionalProperties": false,
"properties": {
"intent": {
"type": "object",
"properties": {
"confidence_threshold": {
"type": "number"
},
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
}
}
},
"steps": {
"$ref": "#$defs/steps"
}
}
},
"next": {
"anyOf": [
{
"type": "array",
"minContains": 1,
"items": {
"type": "object",
"oneOf": [
ancalita marked this conversation as resolved.
Show resolved Hide resolved
{
"required": [
"if",
"then"
]
},
{
"required": [
"else"
]
}
],
"properties": {
"else": {
"oneOf": [
{
"type": "string"
},
{
"$ref": "#$defs/steps"
}
]
},
"if": {
"type": "string"
},
"then": {
"oneOf": [
{
"$ref": "#$defs/steps"
},
{
"type": "string"
}
]
}
}
}
},
{
"type": "string"
Urkem marked this conversation as resolved.
Show resolved Hide resolved
}
]
},
"set_slots": {
"type": "array",
"items": {
"type": "object",
"patternProperties": {
"^[A-Za-z_][A-Za-z0-9_]*$": {
"type": ["string", "null", "boolean", "number"]
}
}
}
}
}
}
8 changes: 0 additions & 8 deletions rasa/shared/core/flows/flows_yaml_schema.yml

This file was deleted.

7 changes: 5 additions & 2 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 = "/shared/core/flows/flows_yaml_schema.yml"
FLOWS_SCHEMA_FILE = "shared/core/flows/flows_yaml_schema.json"


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

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

Expand Down
41 changes: 41 additions & 0 deletions rasa/shared/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,44 @@ def validate_training_data_format_version(
docs=DOCS_URL_TRAINING_DATA,
)
return False


def validate_yaml_with_jsonschema(
yaml_file_content: Text, schema_path: Text, package_name: Text = PACKAGE_NAME
) -> None:
"""Validate data format.

Args:
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_json_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(source_data, schema_content)
except ValidationError as error:
error.message += ". Failed to validate data, make sure your data is valid."
raise SchemaValidationError.create_from(error) from error
Loading