ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

ExtractorsBuilt-in ExtractorsValidationCustom ExtractorsMultipart

Protocols

Integrations

Operating

Reference

Extractors

Validation

Declarative rules on a request type, checked by Valid before the handler runs.

Validation is opt-in through the parameter type. Valid<P> extracts P, runs P's rules, and fails the call with a 422 that names each failing field. The example is examples/15-http-requests.

use serde::Deserialize;
use ulo_http::{Created, Json, Valid, Validate};

/// The body of `POST /notes`; a title outside 1 to 20 characters is refused before the handler runs.
#[derive(Deserialize, Validate)]
pub struct NewNote {
    #[validate(length(min = 1, max = 20))]
    pub title: String,
}

#[ulo_http::post("/notes")]
fn create(&self, Valid(Json(new)): Valid<Json<NewNote>>) -> Created<Json<Note>> { /* .. */ }

Valid wraps any extractor whose value implements Validate. Json, Form, Query and Path implement it by delegating to what they wrap, so Valid<Query<Page>> works the same way. A custom extractor under Valid writes its own impl. Valid forwards the wrapped extractor's body consumption, so Valid<Json<T>> counts as the one body reader.

Rules

#[derive(Validate)] takes three rules in #[validate(..)]:

RuleApplies to
length(min = .., max = ..)either bound optional; counts characters for text and items for a collection
range(min = .., max = ..)numbers
emaila shape check, not RFC 5322

Several rules may share one attribute. An unknown rule is a compile error naming the three. A violation's field is the name serde gives the field, with rename and rename_all applied. Nested structs and flatten are not followed.

Teams that already derive validator::Validate can enable the validator feature on ulo-transport, which exposes a bridge from ValidationErrors to violations. The Validate impl is three lines calling it.

What a failure looks like

A title of zero or 21 characters answers:

HTTP/1.1 422
content-type: application/problem+json
{
  "type": "about:blank",
  "title": "Unprocessable Content",
  "status": 422,
  "detail": "`Json` failed 1 validation rule",
  "details": [{ "type": "field_violations", "violations": [{ "field": "title" }] }]
}

The violations entries also carry a description. detail names the outermost extractor, Json for Valid<Json<NewNote>>. The refusal happens before the handler and before any per-execution controller is built, so a refused note is never created. tests/requests.rs lists the notes afterwards and finds none.

An error handler sees the failure as ExtractError::Invalid { param, violations } through CallError::source_as, and can reshape it. See Error Handling.

Valid<P> is defined for any transport whose extractor P implements Validate. It is not specific to HTTP.

Built-in Extractors

The HTTP extractors, what each reads, and the status each answers when the call does not fit.

Custom Extractors

Implement FromCall to give handlers a parameter type of your own.

On this page

RulesWhat a failure looks like