ValidField v0.6.0 ValidField

ValidField allows for unit testing values against a changeset.

Summary

Functions

Combines assert_valid_field/3 and assert_invalid_field/3 into a single call. The third argument is the collection of valid values to be tested. The fourth argument is the collection of invalid values to be tested

Will assert if the given field’s current value in the changeset is invalid or not

Raises an ValidField.ValidationError when the values for the field are valid for the changset provided. Returns the original changset map from with_changeset/1 to allow subsequent calls to be piped

Will assert if the given fields current values in the changeset are invalid or not

Will assert if the given field’s current value in the changeset is valid or not

Raises an ValidField.ValidationError when the values for the field are invalid for the changset provided. Returns the original changset map from with_changeset/1 to allow subsequent calls to be piped

Will assert if the given fields current values in the changeset are valid or not

Add values that will be set on the changeset during assertion runs

Returns a changeset map to be used with assert_valid_field/3 or assert_invalid_field/3. When with_changeset is passed a single arguments, it is assumed to be an Ecto Model struct and will call the changeset function on the struct’s module

Returns a changeset map to be used with assert_valid_field/3 or assert_invalid_field/3. The function passed to with_changeset/2 must accept two arguments, the first being the model provided to with_changeset/2, the second being the map of properties to be applied in the changeset

Functions

assert_field(changeset, field, valid_values, invalid_values)

Specs

assert_field(map, atom, list, list) :: map

Combines assert_valid_field/3 and assert_invalid_field/3 into a single call. The third argument is the collection of valid values to be tested. The fourth argument is the collection of invalid values to be tested.

Examples

ValidField.with_changeset(%Model{})
|> ValidField.assert_field(:first_name, ["George", "Barry"], ["", nil])
assert_invalid_field(changeset, field)

Specs

assert_invalid_field(map, atom) :: map

Will assert if the given field’s current value in the changeset is invalid or not.

Examples

iex> ValidField.with_changeset(%Model{})
...> |> ValidField.assert_invalid_field(:first_name)
iex> ValidField.with_changeset(%Model{first_name: "Test"})
...> |> ValidField.assert_invalid_field(:first_name)
** (ValidField.ValidationError) Expected the following values to be invalid for "first_name": "Test"
assert_invalid_field(changeset, field, values)

Specs

assert_invalid_field(map, atom, list) :: map

Raises an ValidField.ValidationError when the values for the field are valid for the changset provided. Returns the original changset map from with_changeset/1 to allow subsequent calls to be piped

Examples

iex> ValidField.with_changeset(%Model{})
...> |> ValidField.assert_invalid_field(:first_name, [nil])
...> |> ValidField.assert_invalid_field(:first_name, [""])
iex> ValidField.with_changeset(%Model{})
...> |> ValidField.assert_invalid_field(:first_name, ["Test"])
** (ValidField.ValidationError) Expected the following values to be invalid for "first_name": "Test"
assert_invalid_fields(changeset, fields)

Specs

assert_invalid_fields(map, list) :: map

Will assert if the given fields current values in the changeset are invalid or not.

Only returns an error on the first occurance, doesn’t collect.

Examples

iex> ValidField.with_changeset(%Model{})
...> |> ValidField.assert_invalid_fields([:first_name])
iex> ValidField.with_changeset(%Model{first_name: "Test"})
...> |> ValidField.assert_invalid_fields([:first_name])
** (ValidField.ValidationError) Expected the following values to be invalid for "first_name": "Test"
assert_valid_field(changeset, field)

Specs

assert_valid_field(map, atom) :: map

Will assert if the given field’s current value in the changeset is valid or not.

Examples

iex> ValidField.with_changeset(%Model{first_name: "Test"})
...> |> ValidField.assert_valid_field(:first_name)
iex> ValidField.with_changeset(%Model{})
...> |> ValidField.assert_valid_field(:first_name)
** (ValidField.ValidationError) Expected the following values to be valid for "first_name": nil
assert_valid_field(changeset, field, values)

Specs

assert_valid_field(map, atom, list) :: map

Raises an ValidField.ValidationError when the values for the field are invalid for the changset provided. Returns the original changset map from with_changeset/1 to allow subsequent calls to be piped

Examples

iex> ValidField.with_changeset(%Model{})
...> |> ValidField.assert_valid_field(:first_name, ["Test"])
...> |> ValidField.assert_valid_field(:last_name, ["Value"])
iex> ValidField.with_changeset(%Model{})
...> |> ValidField.assert_valid_field(:first_name, [nil, ""])
** (ValidField.ValidationError) Expected the following values to be valid for "first_name": nil, ""
assert_valid_fields(changeset, fields)

Specs

assert_valid_fields(map, list) :: map

Will assert if the given fields current values in the changeset are valid or not.

Only returns an error on the first occurance, doesn’t collect.

Examples

iex> ValidField.with_changeset(%Model{})
...> |> ValidField.assert_valid_fields([:first_name, :last_name])
iex> ValidField.with_changeset(%Model{first_name: "Test", last_name: "Something"})
...> |> ValidField.assert_valid_fields([:first_name, :last_name])
** (ValidField.ValidationError) Expected the following values to be valid for "first_name": nil
put_params(changeset, params)

Specs

put_params(map, map) :: map

Add values that will be set on the changeset during assertion runs

with_changeset(model)

Specs

with_changeset(Ecto.Model.t) :: map

Returns a changeset map to be used with assert_valid_field/3 or assert_invalid_field/3. When with_changeset is passed a single arguments, it is assumed to be an Ecto Model struct and will call the changeset function on the struct’s module

Examples

ValidField.with_changeset(%Model{})
|> ValidField.assert_invalid_field(:first_name, [nil])
|> ValidField.assert_invalid_field(:first_name, [""])
with_changeset(model, func)

Specs

with_changeset(Ecto.Model.t, function) :: map

Returns a changeset map to be used with assert_valid_field/3 or assert_invalid_field/3. The function passed to with_changeset/2 must accept two arguments, the first being the model provided to with_changeset/2, the second being the map of properties to be applied in the changeset.

Examples

ValidField.with_changeset(%Model{}, &Model.changeset/2)
|> ValidField.assert_invalid_field(:first_name, [nil])
|> ValidField.assert_invalid_field(:first_name, [""])