Skip to content

Http Responses

Chris Michael edited this page Sep 29, 2026 · 3 revisions

Pudu

Http Responses

A failed validation usually leaves the program as an HTTP 422 answer. The Http module converts a result through Pudu's standard report and problem types:

module Ex09Http

import Std.Io as Io
import PuduLangValidator.Http as Http
import PuduLangValidator.Rule as Rule
import PuduLangValidator.Rules.Text as TextRule
import PuduLangValidator.Validator as Validator

type Customer = { name: Str }

export fn main() -> Int {
  let validator = Validator.add(Validator.create(), Rule.build(
    TextRule.notEmpty(Rule.ruleFor("name", |customer: Customer| customer.name))))
  let result = Validator.validate(&validator, Customer{name: ""})
  let report = Http.toReport(&result)
  let problem = Http.problem(&result)
  let response = Http.respond(&result)
  let line = "fields=" + show(report.failures.length())
    + " problem=" + show(problem.status)
    + " http=" + show(response.status.code)
    + " field=" + show(report.failures[0].field)
    + " expected=" + show(report.failures[0].expected)
  match Io.writeLine(line) {
    case Ok(_) => if report.failures.length() == 1 && problem.status == 422 && response.status.code == 422 { 0 } else { 2 }
    case Err(_) => 1
  }
}

Output:

fields=1 problem=422 http=422 field="name" expected="Text is required."

toReport maps each failure to a faulty field with its expected message: the empty name above becomes field "name" expecting "Text is required.". problem builds the 422 problem object with an errors member per field. respond renders it as an application/problem+json response. HTTP stays at this boundary: the validator itself never touches the network.

Related

Clone this wiki locally