howdy/problem

What is wrong with one field of some input.

Rules in howdy/validate and howdy/form report a Problem rather than a sentence, so the words can be chosen later, in the reader’s language. JSON error responses carry each problem’s code and its details, such as maximum, with an English message; a client can translate by code, and a page rendered on the server can use message with words for its locale:

let words = problem.translated(locale)
form.error(errors, "name") |> option.map(problem.message(_, words))

translated covers the locales Howdy ships words for; build a Messages of your own for any other, or to change a few words: problem.Messages(..problem.english(), required: "can't be blank").

Types

The words for each problem. Each describes the field, as in “Name must not be empty”, and so starts in lower case in English.

pub type Messages {
  Messages(
    required: String,
    repeated: String,
    blank: String,
    too_short: fn(Int) -> String,
    too_long: fn(Int) -> String,
    too_small: fn(Int) -> String,
    too_large: fn(Int) -> String,
    email: String,
    integer: String,
    one_of: fn(List(String)) -> String,
    too_few_items: fn(Int) -> String,
    too_many_items: fn(Int) -> String,
    file_too_large: fn(String) -> String,
    file_type: fn(List(String)) -> String,
  )
}

Constructors

  • Messages(
      required: String,
      repeated: String,
      blank: String,
      too_short: fn(Int) -> String,
      too_long: fn(Int) -> String,
      too_small: fn(Int) -> String,
      too_large: fn(Int) -> String,
      email: String,
      integer: String,
      one_of: fn(List(String)) -> String,
      too_few_items: fn(Int) -> String,
      too_many_items: fn(Int) -> String,
      file_too_large: fn(String) -> String,
      file_type: fn(List(String)) -> String,
    )
pub type Problem {
  Required
  Repeated
  Blank
  TooShort(minimum: Int)
  TooLong(maximum: Int)
  TooSmall(minimum: Int)
  TooLarge(maximum: Int)
  NotEmail
  NotInteger
  NotOneOf(allowed: List(String))
  TooFewItems(minimum: Int)
  TooManyItems(maximum: Int)
  FileTooLarge(maximum: Int)
  FileType(allowed: List(String))
  Custom(code: String, message: String)
}

Constructors

  • Required

    Nothing was given.

  • Repeated

    The field was given more than once.

  • Blank

    The text is empty.

  • TooShort(minimum: Int)

    The text has fewer than minimum characters.

  • TooLong(maximum: Int)

    The text has more than maximum characters.

  • TooSmall(minimum: Int)

    The number is less than minimum.

  • TooLarge(maximum: Int)

    The number is more than maximum.

  • NotEmail

    The text is not an email address.

  • NotInteger

    The text is not a whole number.

  • NotOneOf(allowed: List(String))

    The value is not one of allowed.

  • TooFewItems(minimum: Int)

    The list has fewer than minimum items.

  • TooManyItems(maximum: Int)

    The list has more than maximum items.

  • FileTooLarge(maximum: Int)

    The file has more than maximum bytes.

  • FileType(allowed: List(String))

    The file’s type is not one of allowed, such as image/*.

  • Custom(code: String, message: String)

    Anything else. code is for clients and message for people, such as Custom("taken", "is already taken").

Values

pub fn code(problem: Problem) -> String

A short stable name for the problem, sent to clients as code.

pub fn decoder() -> decode.Decoder(Problem)

Read back the fields to_json_fields writes. An unknown code, or one without the details it needs, becomes Custom with the message sent.

pub fn describe_size(bytes: Int) -> String

A size in bytes as people write it: 5 MB, 512 KB or 1500 bytes.

pub fn english() -> Messages

The messages in English.

pub fn locales() -> List(String)

The locales translated has words for.

pub fn message(problem: Problem, messages: Messages) -> String

The message for a problem.

pub fn to_json_fields(
  problem: Problem,
) -> List(#(String, json.Json))

The JSON fields describing a problem: code, an English message, and the details the code needs, such as maximum or allowed.

pub fn translated(locale: locale.Locale) -> Messages

The messages for a locale, or for the nearest one Howdy has words for, falling back to English. These were machine translated and are waiting for review by native speakers.

✨ Search Document