Files
semantic-kernel/python/semantic_kernel/utils/validation.py
T
Jordan Henkel 1c2a5056b2 Python: remove Verify and ./diagnostics (#287)
### Motivation and Context

In an effort to make the Python port more idiomatic, let's remove the
`Verify` class and the `./diagnostics` sub-module. There are many more
follow-on tasks to do here, but this is a good start (and already a
large enough change).

### Description

This PR does the following:

1. Removes the `Verify` class, and re-writes all instances of `Verify.*`
2. Adds a `validation.py` in the `./utils` sub-module to hand some of
the more complex cases from `Verify` (checking that skills/functions/and
function params have valid names)
3. Removes the rest of the `./diagnostics` sub-module (w/ a longer-term
goal of removing all/most of our custom exception classes and, instead,
using appropriate built-in error classes)
2023-04-13 16:12:36 -07:00

73 lines
2.1 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
from re import match as re_match
from typing import Optional
def validate_skill_name(value: Optional[str]) -> None:
"""
Validates that the skill name is valid.
Valid skill names are non-empty and
match the regex: [0-9A-Za-z_]*
:param value: The skill name to validate.
:raises ValueError: If the skill name is invalid.
"""
if not value:
raise ValueError("The skill name cannot be `None` or empty")
SKILL_NAME_REGEX = r"^[0-9A-Za-z_]*$"
if not re_match(SKILL_NAME_REGEX, value):
raise ValueError(
f"Invalid skill name: {value}. Skill "
f"names may only contain ASCII letters, "
f"digits, and underscores."
)
def validate_function_name(value: Optional[str]) -> None:
"""
Validates that the function name is valid.
Valid function names are non-empty and
match the regex: [0-9A-Za-z_]*
:param value: The function name to validate.
:raises ValueError: If the function name is invalid.
"""
if not value:
raise ValueError("The function name cannot be `None` or empty")
FUNCTION_NAME_REGEX = r"^[0-9A-Za-z_]*$"
if not re_match(FUNCTION_NAME_REGEX, value):
raise ValueError(
f"Invalid function name: {value}. Function "
f"names may only contain ASCII letters, "
f"digits, and underscores."
)
def validate_function_param_name(value: Optional[str]) -> None:
"""
Validates that the function parameter name is valid.
Valid function parameter names are non-empty and
match the regex: [0-9A-Za-z_]*
:param value: The function parameter name to validate.
:raises ValueError: If the function parameter name is invalid.
"""
if not value:
raise ValueError("The function parameter name cannot be `None` or empty")
FUNCTION_PARAM_NAME_REGEX = r"^[0-9A-Za-z_]*$"
if not re_match(FUNCTION_PARAM_NAME_REGEX, value):
raise ValueError(
f"Invalid function parameter name: {value}. Function parameter "
f"names may only contain ASCII letters, digits, and underscores."
)