121 lines
2.9 KiB
Plaintext
121 lines
2.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mregex_match...\u001b[0m\n",
|
|
"✅Successfully installed guardrails/regex_match!\n",
|
|
"\n",
|
|
"\n",
|
|
"Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mvalid_range...\u001b[0m\n",
|
|
"✅Successfully installed guardrails/valid_range!\n",
|
|
"\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"! guardrails hub install hub://guardrails/regex_match --quiet\n",
|
|
"! guardrails hub install hub://guardrails/valid_range --quiet"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pydantic import BaseModel, Field\n",
|
|
"from guardrails import Guard, OnFailAction\n",
|
|
"from guardrails.hub import RegexMatch, ValidRange\n",
|
|
"\n",
|
|
"\n",
|
|
"class Person(BaseModel):\n",
|
|
" name: str\n",
|
|
" # Existing way of assigning validators\n",
|
|
" age: int = Field(validators=[ValidRange(0, 100, on_fail=OnFailAction.EXCEPTION)])\n",
|
|
" is_employed: bool"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Validation failed for field with errors: Value 101 is greater than 100.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import json\n",
|
|
"from guardrails.errors import ValidationError\n",
|
|
"\n",
|
|
"\n",
|
|
"guard = Guard.for_pydantic(Person)\n",
|
|
"\n",
|
|
"try:\n",
|
|
" guard.validate(json.dumps({\"name\": \"john doe\", \"age\": 101, \"is_employed\": False}))\n",
|
|
"except ValidationError as e:\n",
|
|
" print(e)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Validation failed for field with errors: Result must match ^(?:[A-Z][^\\s]*\\s?)+$\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Now let's add a new validator to the name field\n",
|
|
"\n",
|
|
"guard.use(\n",
|
|
" RegexMatch(\"^(?:[A-Z][^\\\\s]*\\\\s?)+$\", on_fail=OnFailAction.EXCEPTION), on=\"$.name\"\n",
|
|
")\n",
|
|
"\n",
|
|
"try:\n",
|
|
" guard.validate(json.dumps({\"name\": \"john doe\", \"age\": 30, \"is_employed\": True}))\n",
|
|
"except ValidationError as e:\n",
|
|
" print(e)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": ".venv",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.12.1"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|