View Source Flint.Extensions.PreTransforms (Flint v0.6.0)
The PreTransforms provides a convenient :derive option to express how the field is computed.
By default, this occurs after casting and before validations.
derived fields let you define expressions with support for custom bindings to include any
field declarations that occur before the current field.
:derive will automatically put the result of the input expression into the field value.
By default, this occurs before any other validation, so you can still have access to field
bindings and even the current computed field value (eg. within a :when validation from the
When extension).
You can define a derived field with respect to the field itself, in which case it acts as
transformation. Typically in Ecto, incoming transformations of this support would happen
at the cast step, which means the behavior is determined by the type in which you are casting into.
:derive lets you apply a transformation after casting to change that behavior
without changing the underlying allowed type.
You can also define a derived field with an expression that does not depend on the field,
in which case it is suggested that you use the field macro instead of field! since any input
in that case would be thrashed by the derived value. This means that a field can be completely
determined as a product of other fields!
defmodule Test do
use Flint.Schema
embedded_schema do
field! :category, Union, oneof: [Ecto.Enum, :decimal, :integer], values: [a: 1, b: 2, c: 3]
field! :rating, :integer, when: category == target_category
field :score, :integer, derive: rating + category, gt: 1, lt: 100, when: score > rating
end
endTest.new!(%{category: 1, rating: 80}, target_category: 1)
# %Test{category: 1, rating: 80, score: 81}
Summary
Functions
Applies transformations to each field according to the :derive options passed in the schema specification.
Functions
Applies transformations to each field according to the :derive options passed in the schema specification.
These transformations are applied after casting, but before validations when used within the default Flint.Changeset.changeset implementation.
Accepts optional bindings which are passed to evaluated code.