From 1a6e8f427cd929c28eee8d91c2d3669e7bd5e43b Mon Sep 17 00:00:00 2001 From: Caleb Courier <13314870+CalebCourier@users.noreply.github.com> Date: Thu, 1 Aug 2024 13:31:34 -0700 Subject: [PATCH] Add examples for using custom validators --- docs/concepts/validators.md | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/concepts/validators.md b/docs/concepts/validators.md index a29c13479..a0e297a65 100644 --- a/docs/concepts/validators.md +++ b/docs/concepts/validators.md @@ -92,6 +92,46 @@ Custom validators must be defined before creating a `Guard` or `RAIL` spec in th but otherwise can be used like built in validators. It can be used in a `RAIL` spec OR a `Pydantic` model like so: +Custom validators must be defined before creating a `Guard` in the code, +but otherwise can be used just like built in validators. + +### Guard.use Example +```py +from guardrails import Guard +from .my_custom_validators import starts_with_a, StartsWith + +guard = Guard().use( + StartsWith("my-prefix") +).use( + starts_with_a() +) +``` + +### Pydantic Example +```py +from guardrails import Guard +from pydantic import BaseModel, Field +from .my_custom_validators import starts_with_a, StartsWith + +class MyModel(BaseModel): + a_string: Field(validators=[starts_with_a()]) + custom_string: Field(validators=[StartsWith("my-prefix")]) + +guard = Guard.from_pydantic(MyModel) +``` + +### RAIL Example +```xml + +... + + + + +... + +``` + ## Submitting a Custom Validator to the Hub There are two ways to create a new validator and submit it to the Hub.