tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Fmt: reformat the repository (ADR 0059 rev. 9)69f7c16 · il y a 3 sem.
101 lignes · 3275 octetsBrut · Historique

form

Type-safe decoding and validation of HTML form data in pure Louise: a port of the Gleam package formal (Apache-2.0).

import form

pub type SignUp { SignUp(email: String, password: String) }

/// A schema that decodes a `SignUp` from form data.
fn signup_schema :: -> form.Schema<SignUp>
{
  use email <- form.field "email" (form.parse_email ())
  use password <-
    form.field
      "password"
      ({ form.parse_string () |> form.check_string_length_more_than 7 })
  use _ <-
    form.field
      "confirm"
      ({ form.parse_string () |> form.check_confirms password })
  form.success SignUp(email: email, password: password)
}

In a route

http.form_body gives the submitted fields as the list of string pairs add_values takes. run answers the decoded value, or the form with the errors attached — which keeps the user's input, for re-rendering:

fn register :: req: Request -> Response
{
  match form.new (signup_schema ())
    |> form.add_values (http.form_body req)
    |> form.run
  { Ok(signup) -> create_account signup
  , Error(f) ->
      http.html (signup_page f) // form.field_error_messages(f, "email")
  }
}

Parsers and checks

Parsers (parse_*) extract a value from a field's input; checks (check_*) constrain it. Parsers short-circuit: when one fails, the checks chained after it don't run, so parse_int() |> check_int_more_than(0) on "abc" reports [MustBeInt] and nothing more. Checks don't short-circuit each other: every failed check adds its error.

ParserAnswersFails with
parse_string()Stringnever
parse_int()IntMustBeInt
parse_float()Float (an int reads fine)MustBeFloat
parse_email()String containing @MustBeEmail
parse_phone_number()digits, + - ( ) removedMustBePhoneNumber
parse_checkbox()Bool: any value is True, absent Falsenever
parse_colour()# + six hex digitsMustBeColour
parse_list(p)p per value of a repeated field
parse_optional(p)Some(p) when the field has text, else None
parse(f), map(p, f)your ownCustomError

Checks: check_not_empty, check_accepted, check_confirms (password confirmation), check_int/float_more_than/less_than, check_string_length_more_than/less_than, and check for your own.

Errors

A failed run attaches FieldErrors to their fields; read them as values (field_errors) or as text (field_error_messages), and add your own from outside the schema with add_error (say, an email that's already registered). The text comes from a translator — en_gb by default, en_us for the other spelling of "colour", or your own with language:

form.new schema |> form.language my_translator

Variants

A form whose shape depends on a hidden input parses the marker first, then branches — each arm is its own schema:

use kind <- form.field("kind", form.parse_string())
match kind {
  "guest" -> {
    use email <- form.field("email", form.parse_email())
    form.success(Guest(email))
  }
, _ -> ...
}

Tests

louise test here, and louise test --native for the native backend.