Search results for validate
Data mapping and validation (extras)
# Data mapping and validation We will take a look at the role schemas play when validating and casting data ...
Ecto.Changeset.validate_change/4 (function)
Stores the validation `metadata` and validates the given `field` change. Similar to `validate_change...
Ecto.Changeset.validations/1 (function)
Returns a keyword list of the validations for this changeset. The keys in the list are the names of fields, and the valu...
Validations and constraints - Ecto.Changeset (module)
Ecto changesets provide both validations and constraints which are ultimately turned into errors in case something goes ...
Ecto.Changeset.traverse_validations/2 (function)
Traverses changeset validations and applies the given function to validations. This behaves the same as `trave...
Ecto.Changeset.validate_subset/4 (function)
Validates a change, of type enum, is a subset of the given enumerable. This validates if...
Ecto.Changeset.validate_confirmation/3 (function)
Validates that the given parameter matches its confirmation. By calling `validate_confir...
Ecto.Changeset.validate_number/3 (function)
Validates the properties of a number. The validation only runs if a change for the given...
Ecto.Changeset.validate_inclusion/4 (function)
Validates a change is included in the given enumerable. The validation only runs if a ch...
Ecto.Changeset.validate_exclusion/4 (function)
Validates a change is not included in the given enumerable. The validation only runs if ...
Ecto.Changeset.validate_format/4 (function)
Validates a change has the given format. The format has to be expressed as a regular exp...
Ecto.Changeset.validate_change/3 (function)
Validates the given `field` change. It invokes the `validator` function to perform the v...
Ecto.Changeset.validate_acceptance/3 (function)
Validates the given parameter is true. Note this validation only checks the parameter it...
Ecto.Changeset.validate_required/3 (function)
Validates that one or more fields are present in the changeset. You can pass a single fi...
Validating changes - Getting Started (extras)
In Ecto, you may wish to validate changes before they go to the database. For instance, you may wish that a perso...
Ecto.Changeset.validate_length/3 (function)
Validates a change is a string or list of the given length. Note that the length of a st...
Example - Ecto.Changeset.validations/1 (function)
%Post{} |> change() |> validate_format(:title, ~r/^\w+:\s/, message: "must start with a topic") |> validate_length(:title,...
Examples - Ecto.Changeset.validate_length/3 (function)
validate_length(changeset, :title, min: 3) validate_length(changeset, :title, max: 100) validate_l...
Examples - Ecto.Changeset.validate_confirmation/3 (function)
validate_confirmation(changeset, :email) validate_confirmation(changeset, :password, message: "does not matc...
Ecto.Changeset.unsafe_validate_unique/4 (function)
Validates that no existing record with a different primary key has the same values for th...
Examples - Ecto.Changeset.validate_required/3 (function)
validate_required(changeset, :title) validate_required(changeset, [:title, :body])
Examples - Ecto.Changeset.validate_number/3 (function)
validate_number(changeset, :count, less_than: 3) validate_number(changeset, :pi, greater_than: 3, less...
Examples - Ecto.Changeset.validate_subset/4 (function)
validate_subset(changeset, :pets, ["cat", "dog", "parrot"]) validate_subset(changeset, :lottery_number...
Examples - Ecto.Changeset.validate_change/4 (function)
iex> changeset = change(%Post{}, %{title: "foo"}) iex> changeset = validate_change changeset, :title, :useless_validator, fn ...> _, _ -> [] ...> end ...
Examples - Ecto.Changeset.validate_acceptance/3 (function)
validate_acceptance(changeset, :terms_of_service) validate_acceptance(changeset, :rules, message: "please ...
Examples - Ecto.Changeset.validate_inclusion/4 (function)
validate_inclusion(changeset, :cardinal_direction, ["north", "east", "south", "west"]) validate_inclusion...
Examples - Ecto.Changeset.unsafe_validate_unique/4 (function)
unsafe_validate_unique(changeset, :city_name, repo) unsafe_validate_unique(changeset, [:city_name, :state_nam...
Examples - Ecto.Changeset.validate_format/4 (function)
validate_format(changeset, :email, ~r/@/)
Schemaless changesets - Data mapping and validation (extras)
...{data, types} |> Ecto.Changeset.cast(params["sign_up"], Map.keys(types)) |> validate_required(...) |> validate_length(...) ``` You can use this technique to validate API endpo...
Examples - Ecto.Changeset.validate_exclusion/4 (function)
validate_exclusion(changeset, :name, ~w(admin superadmin))
Examples - Ecto.Changeset.validate_change/3 (function)
iex> changeset = change(%Post{}, %{title: "foo"}) iex> changeset = validate_change changeset, :title, fn :title, title -> ...> # Value must not be "foo"! ...
Schemas are mappers - Data mapping and validation (extras)
... <-> Ecto schema <-> Forms / API" mapping in two parts. The first will cast and validate the external data with its own structure which you then transform and write to ...
Options - Ecto.Changeset.validate_length/3 (function)
...depoints` or `:bytes` * `:message` - the message on failure, depending on the validation, is one of: * for strings: * "should be %{count} character(s)" *...
Examples - Ecto.Changeset.traverse_validations/2 (function)
iex> traverse_validations(changeset, &(&1)) %{title: [format: ~r/pattern/, length: [min: 1, max: 20]]} iex> traverse_validations(changeset, fn ...>...
Options - Ecto.Changeset.validate_acceptance/3 (function)
* `:message` - the message on failure, defaults to "must be accepted". Can also be a `{msg, opts}` tuple, to provide additional options when using `trav...
Options - Ecto.Changeset.validate_confirmation/3 (function)
* `:message` - the message on failure, defaults to "does not match confirmation". Can also be a `{msg, opts}` tuple, to provide additional options when ...
Options - Ecto.Changeset.validate_exclusion/4 (function)
* `:message` - the message on failure, defaults to "is reserved". Can also be a `{msg, opts}` tuple, to provide additional options when using `traverse_...
Options - Ecto.Changeset.validate_format/4 (function)
* `:message` - the message on failure, defaults to "has invalid format". Can also be a `{msg, opts}` tuple, to provide additional options when using `tr...
Options - Ecto.Changeset.validate_inclusion/4 (function)
* `:message` - the message on failure, defaults to "is invalid". Can also be a `{msg, opts}` tuple, to provide additional options when using `traverse_e...
Options - Ecto.Changeset.validate_number/3 (function)
* `:less_than` * `:greater_than` * `:less_than_or_equal_to` * `:greater_than_or_equal_to` * `:equal_to` * `:not_equal_to` * `:message` - the message...
Options - Ecto.Changeset.validate_required/3 (function)
* `:message` - the message on failure, defaults to "can't be blank". Can also be a `{msg, opts}` tuple, to provide additional options when using `traver...
Options - Ecto.Changeset.validate_subset/4 (function)
* `:message` - the message on failure, defaults to "has an invalid entry". Can also be a `{msg, opts}` tuple, to provide additional options when using `...
Options - Ecto.Changeset.unsafe_validate_unique/4 (function)
* `:message` - the message in case the constraint check fails, defaults to "has already been taken". Can also be a `{msg, opts}` tuple, to provide addit...
Ecto.Changeset.field_missing?/2 (function)
...o this function will have its presence evaluated according to the same rules as `validate_required/3`. This is useful when performing complex validations that are not possible with ...
Ecto.Changeset.apply_changes/1 (function)
...ion will return the underlying data with changes regardless if the changeset is valid or not. See `apply_action/2` for a similar function that ensures the changeset ...
Ecto.ParameterizedType.format/1 (callback)
... other logs. Note this callback is not used when constructing `Ecto.Changeset` validation errors. See the `:message` option of most `Ecto.Changeset` validation functions...
Ecto.Changeset (module)
Changesets allow filtering, casting, validation and definition of constraints when manipulating structs. There is an example o...
Ecto.Changeset.apply_action/2 (function)
Applies the changeset action only if the changes are valid. If the changes are valid, all changes are applied to the changeset data. If th...
Ecto.Changeset.apply_action!/2 (function)
Applies the changeset action if the changes are valid or raises an error.
Ecto.Repo.all/2 (callback)
... the data store matching the given query. May raise `Ecto.QueryError` if query validation fails.
Changesets - Ecto (module)
...an efficiently track changes. Changesets allow developers to filter, cast, and validate changes before we apply them to the data. Imagine the given schema: defmod...
Ecto.Changeset.change/2 (function)
...e value is a term. Note the `value` is directly stored in the changeset with no validation whatsoever. For this reason, this function is meant for working with data inter...
The Ecto.Changeset struct - Ecto.Changeset (module)
The public fields are: * `valid?` - Stores if the changeset is valid * `data` - The changeset source data, for example, a struct * `para...
Ecto.Type.dump/1 (callback)
... callback is called with any term that was stored in the struct and it needs to validate them and convert it to an Ecto native type.
Ecto.Repo.stream/2 (callback)
... enumerate a stream inside a transaction. May raise `Ecto.QueryError` if query validation fails.
Basic CRUD (extras)
...mes from the user via forms, APIs, and often need to be normalized, pruned, and validated via Ecto.Changeset.
Ecto.UUID.cast/1 (function)
...ented as binaries, this means some strings you may not expect are actually also valid UUIDs in their binary form and so will be casted into their string form. If yo...
Changesets - Embedded Schemas (extras)
Changeset functionality for embeds will allow you to enforce arbitrary validations on the data. You can define a changeset function for each module. For example, ...
Ecto.Multi (module)
...ique and will identify its result in case of success or failure. If a multi is valid (i.e. all the changesets in it are valid), all operations will be executed in t...
Casting - Ecto.Schema (module)
... if you attempt to persist the struct above, an error will be raised since Ecto validates the types when sending them to the adapter/database. Therefore, when working w...
Associations (extras)
...mes from the user via forms, APIs, and often need to be normalized, pruned, and validated via `Ecto.Changeset`. We also include examples of migrations, according to [`Ec...
Ecto.Query.API.field/2 (function)
...xample above, both `at_least_four(:doors)` and `at_least_four(:tires)` would be valid calls as the field is dynamically generated.
Ecto.Changeset.update_change/3 (function)
...ue of the change can still be `nil` (unless the field was marked as required on `validate_required/3`).
Example - Ecto.Multi.delete/4 (function)
...d end) |> Ecto.Multi.delete(:delete, fn %{post: post} -> # Others validations post end) |> MyApp.Repo.transaction()
Options - Ecto.Schema.field/3 (macro)
...`:autogenerate` option to generate at insertion time. The default value is validated against the field's type at compilation time and it will raise an ArgumentE...
Examples - Ecto.Changeset.cast/4 (function)
... iex> changeset.errors [ title: {"must be a string", [type: :string, validation: :cast]}, body: {"is_invalid", [type: :string, validation: :cast]} ] ...
Embedded Schemas (extras)
# Embedded Schemas Embedded schemas allow you to define and validate structured data. This data can live in memory, or can be stored in the database...
Ecto.Changeset.cast/4 (function)
... ignored. If casting of all fields is successful, the changeset is returned as valid. Note that `cast/4` validates the types in the `params`, but not in the given `...
Examples - Ecto.Changeset.traverse_errors/2 (function)
...field` and error tuple `{msg, opts}`. It is useful whenever you want to extract validations rules from `changeset.validations` to build detailed error description.
Example - Ecto.Multi.delete_all/4 (function)
...d) |> Ecto.Multi.delete_all(:delete_all, fn %{post: post} -> # Others validations from(c in Comment, where: c.post_id == ^post.id) end) |> MyApp.Re...
Example - Ecto.Multi.update_all/5 (function)
...d) |> Ecto.Multi.update_all(:update_all, fn %{post: post} -> # Others validations from(c in Comment, where: c.post_id == ^post.id, update: [set: [title: "N...
External vs internal data - Ecto.Changeset (module)
...provided by the user in a form that needs to be type-converted and properly validated. This use case is primarily covered by the `cast/4` function. When working ...
Example - Ecto.Multi.insert_all/5 (function)
... Ecto.Multi.insert_all(:insert_all, Comment, fn %{post: post} -> # Others validations entries |> Enum.map(fn comment -> Map.put(comment, :post_i...
Ecto.Changeset.traverse_errors/2 (function)
...opts}`, for example: {"should be at least %{count} characters", [count: 3, validation: :length, min: 3]}
Ecto.Type.dump/3 (function)
...e given type. Opposite to casting, dumping requires the returned value to be a valid Ecto type, as it will be sent to the underlying data store. iex> dump(:str...
Ecto.Repo.delete/2 (callback)
...he struct has been successfully deleted or `{:error, changeset}` if there was a validation or a known constraint error. By default, constraint errors will raise the `Ecto...
Ecto.Repo.insert/2 (callback)
...e struct has been successfully inserted or `{:error, changeset}` if there was a validation or a known constraint error.
Ecto.Changeset.prepare_changes/2 (function)
...epository on insert/update/delete. If the changeset given to the repository is valid, the function given to `prepare_changes/2` will be called with the changeset and...
Ecto.Changeset.put_change/3 (function)
... in the changeset. Note the `value` is directly stored in the changeset with no validation whatsoever. For this reason, this function is meant for working with data inter...
Potential incompatibilities - Changelog for v3.x (extras)
.... It was added for Clickhouse support but it is no longer used * [Ecto.Query] Validate `:prefix` is a string/binary (this was reverted in v3.12.2)
Ecto.Schema (module)
...ch schemas to receive data from a command line interface or a contact form, and validate it, without ever persisting it elsewhere. Such structs do not contain a `__meta...
Ecto.Repo.update/2 (callback)
...he struct has been successfully updated or `{:error, changeset}` if there was a validation or a known constraint error.
Schemaless changesets - Ecto.Changeset (module)
In the changeset examples so far, we have always used changesets to validate and cast data contained in a struct defined by an Ecto schema, such as the `%Us...
Aborted transactions - Ecto.Repo.transaction/2 (callback)
... repo.insert(%Status{value: "failure"}) end end) If the changeset is valid, but the insert operation fails due to a database constraint, the subsequent `re...
Example - Ecto.Schema (module)
...: "jane"} iex> %{user | age: 30} However, most commonly, structs are cast, validated and manipulated with the `Ecto.Changeset` module. The first argument of `schem...
Changeset actions - Ecto.Changeset (module)
...Repo.insert`. `apply_action/2` will return `{:ok, changes}` if the changeset is valid or `{:error, changeset}`, with the given `action` set in the changeset in case ...
v3.10.0 (2023-04-10) - Changelog for v3.x (extras)
...nt, note this release unifies the handling of empty values between `cast/4` and `validate_required/3`. **If you were setting `:empty_values` in the past and you want to preserve this...
Custom actions - Ecto.Changeset.cast_assoc/3 (function)
...struct, params) do struct |> cast(params, [:title, :body]) |> validate_required([:title, :body]) |> case do %{valid?: false, changes: changes} = changeset...
Examples - Ecto.ParameterizedType (behaviour)
...izedType def type(_params), do: :string def init(opts) do validate_opts(opts) Enum.into(opts, %{}) end def cast(data, params) do ...
Ecto.Changeset.unique_constraint/3 (function)
...ccur only after hitting the database, so it will not be visible until all other validations pass. If the constraint fails inside a transaction, the transaction will be mar...
Our first queries - Getting Started (extras)
...r database, Ryan, John and Jane. Note here that we could've used a changeset to validate the data going into the database, but the choice was made not to use one. We'l...
Ecto.Changeset.check_constraint/3 (function)
...ccur only after hitting the database, so it will not be visible until all other validations pass. If the constraint fails inside a transaction, the transaction will be mar...
Ecto.Changeset.merge/2 (function)
... are merged giving precedence to the `changeset2` changes. * `errors` and `validations` - they are simply concatenated. * `required` - required fields are merged; al...
Summary - Multi tenancy with foreign keys (extras)
...We also learned how to leverage database features to enforce the data is always valid. When it comes to associations, you will want to apply composite foreign keys w...
put_assoc vs cast_assoc - Constraints and Upserts (extras)
...abase. It is important to add an index at the database level instead of using a validation since there is always a chance two tags with the same name would be validated a...
Ecto.Changeset.foreign_key_constraint/3 (function)
...ccur only after hitting the database, so it will not be visible until all other validations pass. If the constraint fails inside a transaction, the transaction will be mar...
Options - Ecto.Changeset.cast_assoc/3 (function)
... field. For associations of cardinality one, a non-nil value satisfies this validation. For associations with many entries, a non-empty list is satisfactory. * ...
Options - Ecto.Changeset.cast_embed/3 (function)
...quired field. For embeds of cardinality one, a non-nil value satisfies this validation. For embeds with many entries, a non-empty list is satisfactory. * `:requ...
Ecto (module)
...re and composable * `Ecto.Changeset` - changesets provide a way to track and validate changes before they are applied to the data In summary: * `Ecto.Repo` -...
Examples - Ecto.Changeset.optimistic_lock/3 (function)
...!(%Post{title: "foo"}) %Post{id: 1, title: "foo", lock_version: 1} iex> valid_change = Post.changeset(:update, post, %{title: "bar"}) iex> stale_change = Post.c...
Extracting the embeds - Embedded Schemas (extras)
.... For example, if you want to build a contact form, you still want to parse and validate the data, but the data is likely not persisted anywhere. Instead, it is used to...
Updating records - Getting Started (extras)
...date because changeset is invalid. Errors %{first_name: [{"can't be blank", [validation: :required]}]} Applied changes %{} Params %{"first_name" => ""} Changes...
Primitive types - Ecto.Schema (module)
..._type}` and `{:map, inner_type}` type, replace `inner_type` with one of the valid types, such as `:string`. * For the `:decimal` type, `+Infinity`, `-Infinity...
Composing with data structures - Composable transactions with Multi (extras)
...bove, we have defined two update operations, named `:mary` and `:john`, and two validation operations, named `:check_mary` and `:check_john`. As we will see later, the na...
Options - Ecto.Schema.has_many/3 (macro)
...lds or an MFA tuple, such as `{Mod, fun, []}`. Both cases must resolve to a valid `order_by` expression. For example, if you set `Post.has_many :comments, pr...
Upserts and insert_all - Constraints and Upserts (extras)
...back. Therefore, even if we fail to introduce the post to the database due to a validation error, the user will be free to resubmit the form and we will just attempt to g...
has_many/has_one :through - Ecto.Schema.has_many/3 (macro)
...together. Of course, this assumes your database guarantees those references are valid, which can be done by defining foreign key constraints and references your datab...
Ecto.Changeset.cast_assoc/3 (function)
...hanges. This is done for reflection purposes, allowing developers to introspect validations and other metadata from the association. Once the parent changeset is given to ...
People relationships - Self-referencing many to many (extras)
...duction applications, we will most likely want to add additional attributes and validations. This is where our isolation of modules will help us maintain and organize the i...
Options - Ecto.Schema.many_to_many/3 (macro)
...lds or an MFA tuple, such as `{Mod, fun, []}`. Both cases must resolve to a valid `order_by` expression. See `Ecto.Query.order_by/3` to learn more about orde...
Ecto.Changeset.put_assoc/4 (function)
...pposite to `cast_assoc` and `embed_assoc`, the given map (or struct) is not validated in any way and will be inserted as is. This API is mostly used in scripts a...
Enhancements - Changelog for v3.x (extras)
* [Ecto.Repo] Use `persistent_term` for faster repository lookup * [Ecto.Repo] Document new `:pool_count` option
Bug fix - Changelog for v3.x (extras)
... [Ecto.Changeset] Raise when schemaless changeset or embedded schema is used in `unsafe_validate_unique/4` * [Ecto.Query] Respect virtual field type in subqueries * [Ecto.Query] Don'...