JSV.StructSupport (jsv v0.5.0)

View Source

Utilities to work with structs defined by schemas.

Summary

Functions

Returns a tuple where the first element is a list of schema :properties keys that do not have a :default value, and the second element is a list of {key, default_value} tuples. sorted by keys.

Returns a list of {binary_key, atom_key} tuples for the given schema. The list is sorted by keys.

Returns the required property of the schema or an empty list.

Validates the given raw schema can be used to define a module struct or raises an exception.

Functions

data_pairs_partition(schema)

@spec data_pairs_partition(JSV.raw_schema()) :: {[atom()], keyword()}

Returns a tuple where the first element is a list of schema :properties keys that do not have a :default value, and the second element is a list of {key, default_value} tuples. sorted by keys.

Both lists are sorted by key.

The schema must be valid against validate!1/.

keycast_pairs(schema, target \\ nil)

@spec keycast_pairs(JSV.raw_schema(), target :: nil | module()) :: [
  {binary(), atom()}
]

Returns a list of {binary_key, atom_key} tuples for the given schema. The list is sorted by keys.

The schema must be valid against validate!1/.

This function accepts a second argument, which must be a module that implements a struct (with defstruct/1). When given, the function will validate that all schema keys exist in the given struct.

list_required(schema)

@spec list_required(JSV.raw_schema()) :: [atom()]

Returns the required property of the schema or an empty list.

The schema must be valid against validate!1/.

validate!(schema)

@spec validate!(JSV.raw_schema()) :: :ok

Validates the given raw schema can be used to define a module struct or raises an exception.

It will check the following:

  • Schema defines a type object.
  • Schema has properties.
  • properties is a map with atom keys.
  • required, if present, contains only atoms