module Amber::Router::ParamConstraints

Overview

The built-in route parameter rules, and the conversion from what a route definition writes to the rules the router stores.

A route names a rule by symbol or passes a rule value:

get "/posts/:id", PostsController, :show, {"id" => :numeric}
get "/objects/:key", ObjectsController, :show, {key: Amber::Router::ParamConstraints::UUID}
get "/codes/:code", CodesController, :show, {"code" => Amber::Router::ParamConstraints::NUMERIC.with_length(3)}

Defined in:

amber/router/param_constraints.cr
amber/router/param_constraints/character_run.cr
amber/router/param_constraints/custom.cr
amber/router/param_constraints/one_of.cr
amber/router/param_constraints/slug.cr
amber/router/param_constraints/uuid.cr

Constant Summary

ALNUM = CharacterRun.new("alnum", 'a'..'z', 'A'..'Z', '0'..'9')

One or more ASCII letters and digits.

ALPHA = CharacterRun.new("alpha", 'a'..'z', 'A'..'Z')

One or more ASCII letters.

HEX = CharacterRun.new("hex", '0'..'9', 'a'..'f', 'A'..'F')

One or more hexadecimal digits, in either letter case.

NUMERIC = CharacterRun.new("numeric", '0'..'9')

One or more ASCII digits.

PRESETS = {:numeric => NUMERIC, :uuid => UUID, :slug => SLUG, :alpha => ALPHA, :alnum => ALNUM, :hex => HEX} of Symbol => ParamConstraint

The rules a route can name by symbol.

SLUG = Slug.new

A lowercase slug; see Slug.

UUID = Uuid.new

A hyphenated UUID; see Uuid.

Class Method Summary

Class Method Detail

def self.normalize(constraints : Hash | NamedTuple) : Hash(String, ParamConstraint) #

Returns constraints keyed by parameter name, with every symbol replaced by its preset rule. Raises UnknownPresetError for a symbol that is not a preset.


[View source]