Skip to content

Commit

Permalink
do not gracefully fail on YAML validation fail
Browse files Browse the repository at this point in the history
  • Loading branch information
tahmidefaz committed Jul 30, 2024
1 parent df97693 commit a25bdb2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
3 changes: 2 additions & 1 deletion example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ def main(user_message):

if __name__ == "__main__":
messages = [
"Are you human?",
"do some magic on spotify!",
"I want to make sure that I attend my friend's brithday party later this year",
"I feel like eating out tonight",
"It's so dark in this room... I can't see anything... help!",
"anything interesting happened in the world of AI today?",
"look for a flight form New York to Los Angeles for July 28th",
"look for a flight from Denver to Honolulu for July 28th",
"what's the weather in Seattle today?"
]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "seedling"
version = "0.2.0"
version = "0.2.1"
description = "A simple library for intent recognition using LLMs."
authors = [{name = "Tahmid Efaz"}]
license = {file = "LICENSE"}
Expand Down
25 changes: 5 additions & 20 deletions src/seedling/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@
}


def is_valid(filename: str, data: dict) -> bool:
"""Validates data against a JSON schema.
Args:
filename: The name of the file being validated.
data: The file data to validate.
Returns:
True if the data is valid, False otherwise.
"""
try:
jsonschema.validate(instance=data, schema=SCHEMA)
except jsonschema.exceptions.ValidationError as e:
print(f"{filename} is invalid: {e}")
return False
return True


def read(directory: str) -> list:
"""Reads YAML files in a directory, validates it using a JSONschema
and returns an array of dictionaries.
Expand All @@ -77,7 +59,10 @@ def read(directory: str) -> list:
for yaml_file in yaml_files:
with open(yaml_file, 'r', encoding='UTF-8') as f:
data = yaml.safe_load(f)
if is_valid(yaml_file, data):
all_topic_info.append(data)

# Validate against JSONschema, and fail script when validation do not pass
jsonschema.validate(instance=data, schema=SCHEMA)

all_topic_info.append(data)

return all_topic_info

0 comments on commit a25bdb2

Please sign in to comment.