RockSolid.Traversal (rock_solid v0.0.7)

Copy Markdown

Utilities and functions to traverse schemas, collect keywords, etc.

Summary

Functions

Returns whether the reversed path is a definiiton.

Returns whether the reversed path is part of a dependency definition

Returns the value at the given location in the schema.

Returns whether the reversed path should be treated as a literal

Returns whether the current (reversed) path is properties

Sets a value in the given (existing) path and returns the updated schema

Returns a map of all references ($id, $anchor, $ref) and their corresponding values in the format

Converts a JSON pointer to a path

Converts a JSON path list to a pointer.

Types

path_t()

@type path_t() :: [String.t()]

Functions

definition?(reversed_path)

@spec definition?([String.t()]) :: boolean()

Returns whether the reversed path is a definiiton.

Examples

iex> RockSolid.Traversal.definition?(["type", "#"])
false

iex> RockSolid.Traversal.definition?(["$defs", "#"])
true

dependencies?(reversed_path)

Returns whether the reversed path is part of a dependency definition

Examples

iex> RockSolid.Traversal.dependencies?(["dependentSchemas", "#"])
true

iex> RockSolid.Traversal.dependencies?(["dependentSchemas", "properties", "#"])
false

get_in_schema(schema, list)

@spec get_in_schema(any(), [String.t()]) :: any()

Returns the value at the given location in the schema.

The path argument may contain a leading "#". You can convert a JSON Pointer string to a valid path by calling to_path/1

Examples

iex> RockSolid.Traversal.get_in_schema(%{"foo" => %{"bar" => "baz"}}, ["#", "foo", "bar"])
"baz"

iex> RockSolid.Traversal.get_in_schema(%{"foo" => ["a", "b"]}, ["#", "foo", "1"])
"b"

is_atomic(v)

(macro)

literal?(reversed_path)

@spec literal?([String.t()]) :: boolean()

Returns whether the reversed path should be treated as a literal

property?(reversed_path)

@spec property?([String.t()]) :: boolean()

Returns whether the current (reversed) path is properties

Examples

iex> RockSolid.Traversal.property?(["type", "#"])
false

iex> RockSolid.Traversal.property?(["properties", "person", "$defs", "#"])
true

iex> RockSolid.Traversal.property?(["properties", "properties", "meta", "$defs", "#"])
false

put_in_schema(schema, path, value)

@spec put_in_schema(any(), [String.t()], any()) ::
  {:ok, any()} | {:error, Exception.t()}

Same as put_in_schema!/3 but returns a tuple

put_in_schema!(schema, path, new_value)

Sets a value in the given (existing) path and returns the updated schema

Examples

iex> RockSolid.Traversal.put_in_schema!(%{"foo" => ["a", "b"]}, ["#", "foo", "1"], "c")
%{"foo" => ["a", "c"]}

references(schema)

@spec references(map()) :: %{required(String.t()) => %{required(path_t()) => any()}}

Returns a map of all references ($id, $anchor, $ref) and their corresponding values in the format

%{"$ref" => %{["#", "path"] => value},  "$id" => %{}}

to_path(json_pointer, opts \\ [])

@spec to_path(String.t(), Keyword.t()) :: [String.t()]

Converts a JSON pointer to a path

Examples

iex> RockSolid.Traversal.to_path("#/$defs/something")
["#", "$defs", "something"]

iex> RockSolid.Traversal.to_path("#/paths/~1users")
["#", "paths", "/users"]

iex> RockSolid.Traversal.to_path("/path/to/0/foo")
["path", "to", "0", "foo"]

Options

  • :include_root? - boolean/0 whether to include the root "#". Defaults to true

to_pointer(path)

Converts a JSON path list to a pointer.

If the list does not start with "#" it is assumed to be a reversed path

Examples

iex> RockSolid.Traversal.to_pointer(["#", "$defs", "foo"])
"#/$defs/foo"

iex> RockSolid.Traversal.to_pointer(["bar", "$defs", "#"])
"#/$defs/bar"

iex> RockSolid.Traversal.to_pointer(["#", "/users", "GET"])
"#/~1users/GET"