Skip to content

Commit

Permalink
Add examples for using custom validators
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebCourier authored Aug 1, 2024
1 parent dad9cc6 commit 1a6e8f4
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/concepts/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<rail version="0.1">
...
<output>
<string name="a_string" type="string" validators="starts-with-a">
<string name="custom_string" type="string" validators="starts-with: my-prefix">
</output>
...
</rail>
```

## Submitting a Custom Validator to the Hub

There are two ways to create a new validator and submit it to the Hub.
Expand Down

0 comments on commit 1a6e8f4

Please sign in to comment.