ExParamsSchema.Schema.JsonPointer (ex_params_schema v0.1.1)

Copy Markdown View Source

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

pattern()

@type pattern() :: [pattern_segment()]

pattern_segment()

@type pattern_segment() :: String.t() | :index

pointer_path()

@type pointer_path() :: [segment()]

resolved_path()

@type resolved_path() :: [resolved_segment()]

resolved_segment()

@type resolved_segment() :: String.t() | non_neg_integer()

segment()

@type segment() :: String.t()

Functions

decode(arg1)

@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"]}

escape_segment(segment)

@spec escape_segment(String.t()) :: String.t()

Escapes a JSON Pointer segment according to RFC 6901.

match(pattern, path)

@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.

parse(pointer)

@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.

resolve_segments(path)

@spec resolve_segments(pointer_path()) :: resolved_path()

Converts list indices in a JSON Pointer path to integers.