H.Schema (H v0.3.0)

Copy Markdown View Source

Boundary validator for untrusted decoded JSON maps, command envelopes, and HTTP parameters.

Never calls String.to_atom/1 on external input. Field specs define the exact permitted keys and types; keys are read by Atom.to_string(key) with fallback to existing atom keys for internal callers.

Summary

Functions

Normalizes a session title: converts nil or blank strings to nil, collapses CR/LF/tabs to spaces, trims whitespace, and rejects NUL bytes, control characters, or titles exceeding 200 characters.

Parses a positive integer from either an integer (> 0) or a base-10 digit string.

Validates an untrusted map against field_specs.

Validates a session ID against ^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$ to prevent path traversal in session directories.

Types

field_spec()

@type field_spec() :: {atom(), field_type()} | {atom(), field_type(), keyword()}

field_type()

@type field_type() ::
  :string
  | :non_empty_string
  | :id_string
  | :session_id
  | :session_tags
  | :abs_path
  | :boolean
  | :int
  | {:int, keyword()}
  | :pos_int
  | {:literal, term()}
  | {:enum, [term()]}
  | :map
  | {:map, [field_spec()]}
  | {:list, field_type()}
  | :any

Functions

normalize_title(value)

@spec normalize_title(term()) :: {:ok, nil | String.t()} | {:error, String.t()}

Normalizes a session title: converts nil or blank strings to nil, collapses CR/LF/tabs to spaces, trims whitespace, and rejects NUL bytes, control characters, or titles exceeding 200 characters.

parse_pos_int(val)

@spec parse_pos_int(term()) :: {:ok, pos_integer()} | {:error, String.t()}

Parses a positive integer from either an integer (> 0) or a base-10 digit string.

validate_map(input, field_specs)

@spec validate_map(term(), [field_spec()]) :: {:ok, map()} | {:error, String.t()}

Validates an untrusted map against field_specs.

Each spec is {field_key, type} or {field_key, type, opts} where opts supports :required, :default, and :nullable. Unknown keys in input are ignored and omitted from the returned atom-keyed map.

validate_session_id(id)

@spec validate_session_id(term()) :: {:ok, String.t()} | {:error, String.t()}

Validates a session ID against ^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$ to prevent path traversal in session directories.