Provides pure functions for converting and matching JSON Pointers and field paths.
JSON Schema validation errors use JSON Pointers, while the application uses paths made of string keys and list indexes. This module centralizes the conversion rules.
Summary
Functions
Decodes a JSON Pointer into unresolved segments.
Escapes a JSON Pointer segment according to RFC 6901.
Returns the resolved path when a definition pattern matches the beginning of a path.
Parses a JSON Pointer into a path of string keys and list indices.
Converts list indices in a JSON Pointer path to integers.
Types
@type pattern() :: [pattern_segment()]
@type pattern_segment() :: String.t() | :index
@type pointer_path() :: [segment()]
@type resolved_path() :: [resolved_segment()]
@type resolved_segment() :: String.t() | non_neg_integer()
@type segment() :: String.t()
Functions
@spec decode(String.t()) :: {:ok, pointer_path()} | :error
Decodes a JSON Pointer into unresolved segments.
iex> ExParamsSchema.Schema.JsonPointer.decode("#/a~1b~0c/0")
{:ok, ["a/b~c", "0"]}
Escapes a JSON Pointer segment according to RFC 6901.
@spec match(pattern(), pointer_path()) :: {:ok, resolved_path()} | :error
Returns the resolved path when a definition pattern matches the beginning of a path.
:index matches only segments that are non-negative integers.
@spec parse(String.t()) :: resolved_path()
Parses a JSON Pointer into a path of string keys and list indices.
iex> ExParamsSchema.Schema.JsonPointer.parse("#/entries/0/value")
["entries", 0, "value"]# represents the root. Invalid pointers are treated as an empty path.
@spec resolve_segments(pointer_path()) :: resolved_path()
Converts list indices in a JSON Pointer path to integers.