View Source MapSchemaValidator (map_schema_validator v0.1.2)
It's a map format verifier, verify if keys/values exist in a given map, short and quick, you can specify more than one format and verify list of values.
Just use the function MapSchemaValidator.validate/2
or MapSchemaValidator.validate!/2
Summary
Functions
By param schema validates the param data and compares the format and values to ensure follows a specific format.
Same as MapSchemaValidator.validate/2 but raises in fail.
Functions
By param schema validates the param data and compares the format and values to ensure follows a specific format.
Examples
iex> MapSchemaValidator.validate(%{key: :string}, %{key: "value"})
{:ok, nil}
iex> MapSchemaValidator.validate(%{key: [:string, :number]}, %{key: 1})
{:ok, nil}
iex> MapSchemaValidator.validate(%{key: [%{inner_key: :string}]}, %{key: [%{inner_key: "value_1"}, %{inner_key: "value_2"}]})
{:ok, nil}
iex> MapSchemaValidator.validate(%{key: [%{inner_key: :string}]}, %{key: [%{inner_key: 1}, %{inner_key: "value_2"}]})
{:error, %MapSchemaValidator.InvalidMapError{message: "error at: key -> inner_key"}}
Same as MapSchemaValidator.validate/2 but raises in fail.
Examples
iex> MapSchemaValidator.validate!(%{key: :string}, %{key: "value"})
:ok
iex> MapSchemaValidator.validate!(%{key: [:string, :number]}, %{key: 1})
:ok
iex> MapSchemaValidator.validate!(%{key: [%{inner_key: :string}]}, %{key: [%{inner_key: "value_1"}, %{inner_key: "value_2"}]})
:ok
iex> MapSchemaValidator.validate!(%{key: [%{inner_key: :string}]}, %{key: [%{inner_key: 1}, %{inner_key: "value_2"}]})
** (UndefinedFunctionError) function JsonValidator.validate/2 is undefined (module JsonValidator is not available)
JsonValidator.validate(%{schemas: [%{name: :string, fields: %{string: :string}, hooks?: [%{events: [:string, :integer], headers: %{string: :string}, method: :string, url: :string}]}]}, %{"schemas" => [%{"fields" => %{"age" => "number", "birth" => "datetime", "male" => "boolean", "name" => "string"}, "hooks" => [%{"events" => ["create", false], "headers" => %{"key" => "value"}, "method" => "post", "url" => "https://someurl.test/webhook"}], "name" => "users"}, %{"fields" => %{"expiration" => "datetime", "name" => "string"}, "name" => "products"}]})
/data/default_book.livemd#cell:hu7mznvjuk3vuqwpft3emi562ezmozpx:48: (file)