Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lpil/formal
https://github.com/lpil/formal
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/lpil/formal
- Owner: lpil
- Created: 2024-02-27T22:13:27.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-29T10:46:36.000Z (9 months ago)
- Last Synced: 2024-05-01T23:04:56.702Z (6 months ago)
- Language: Gleam
- Size: 9.77 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-gleam - formal - [📚](https://hexdocs.pm/formal/) - Type safe HTML form decoding and validation! (Packages / HTML)
README
# formal
Type safe HTML form decoding and validation!
[![Package Version](https://img.shields.io/hexpm/v/formal)](https://hex.pm/packages/formal)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/formal/)```sh
gleam add formal
```
```gleam
import formal/form// Define a type that is to be decoded from the form data
pub type SignUp {
SignUp(email: String, password: String)
}// This function takes the list of key-value string pairs that a HTML form
// produces. It then decodes the form data into a SignUp value, ensuring that
// all the fields are present and valid.
//
pub fn handle_form_submission(values: List(#(String, String))) {
let result =
form.decoding({
use email <- form.parameter
use password <- form.parameter
SignUp(email: email, password: password)
})
|> form.with_values(values)
|> form.field(
"email",
form.string
|> form.and(form.must_not_be_empty)
|> form.and(form.must_be_an_email),
)
|> form.field(
"password",
form.string
|> form.and(form.must_not_be_empty)
|> form.and(form.must_be_string_longer_than(7))
)
|> form.finishcase result {
Ok(data) -> {
// Do something with the SignUp value here
}
Error(form_state) -> {
// Re-render the form with the error messages
}
}
}
```An example of using this library with Wisp and Lustre can be found in
[./example/](https://github.com/lpil/formal/tree/main/example).Further documentation can be found at .