# valguard
[](https://hex.pm/packages/valguard)
[](https://hexdocs.pm/valguard/)
A Gleam validation library for form and api endpoint validation. Currently a work-in-progress.
Better documentation and examples Coming Soon™.
## Examples
An example of validating a login form:
```gleam
import valguard.{type ValidationError} as v
/// Your custom validation error type that wraps valguard error type
pub type Errors {
ErrorValidatingParams(List(ValidationError))
}
/// Your login endpoint params
type LoginParams {
LoginParams(email: String, password: String)
}
// your code here...
/// Validates login params, where `ErrorValidatingParams` is your own validation error type.
///
/// Function returns a result with a list of validation errors. These errors
/// can be used to build a map of key value pairs, where the keys is the param name and value
/// is the validation error message.
fn validate_params(params: LoginParams) -> Result(Nil, Errors) {
[
v.with("email", params.email, [
v.string_required(_, "This field is required"),
v.email_is_valid(_, "Email address is not valid"),
]
),
v.with("password", params.password, [
v.string_required(_, "This field is required"),
]),
]
|> v.collect_errors
|> v.prepare_with(ErrorValidatingParams)
}
```
## Goals
- Perform exhaustive param validation for good form validation UX. Don't just stop on the first error
- Reduce repetitive boilerplate when trying to validate endpoint params
- Look and feel nice to use
## Motivation
After building a sizeable backend api in gleam, I found I was writing a lot of boilerplate code
to validate my endpoint params for each of my APIs. There are existing libraries for this but
I personally wasn't a fan of their API, so I started to build my own internally.
The main goal was to enforce param validation and customise the per-field validation errors
that get returned to the client. This allows for a nice form validation UX but can also be used
generally to enforce arbitrary requirements on params submitted to your endpoints.
## Development
```sh
mise up # Install dependencies
gleam run # Run the project
gleam test # Run the tests
```