Constraints are a way of validating an input type. This validation can be used in both attributes and arguments. The kinds of constraints you can apply depends on the type the data. You can find all types in `Ash.Type` . Each type has its own page on which the available constraints are listed. For example in `Ash.Type.String` you can find 5 constraints:
-`:max_length`
-`:min_length`
-`:match`
-`:trim?`
-`:allow_empty?`
You can also discover these constraints from iex:
```bash
$ iex -S mix
iex(1)> Ash.Type.String.constraints
[
max_length: [
type: :non_neg_integer,
doc: "Enforces a maximum length on the value"
],
min_length: [
type: :non_neg_integer,
doc: "Enforces a minimum length on the value"
],
match: [
type: {:custom, Ash.Type.String, :match, []},
doc: "Enforces that the string matches a passed in regex"
],
trim?: [type: :boolean, doc: "Trims the value.", default: true],
allow_empty?: [
type: :boolean,
doc: "If false, the value is set to `nil` if it's empty.",
default: false
]
]
```
## Attributes with Constraints
To show how constraints can be used in a attribute, here is an example attribute describing a username:
If when creating or updating this attribute one of the constraints are not met, an error will be given telling you which constraint was broken. See below:
If you input argument is going to be used as a attribute directly, its best to put the constraint in the `attributes` block. But if you are combining multiple arguments to synthesize an attribute, then you should apply constraints to the arguments.
Above we have defined a custom action which takes 2 arguments `:title` and `:age` this action creates a username where the age of the user is embedded. However we have placed a limitation via the constraints so that only when age >= 18 and age <= 99 is the action allowed to occur. Lets see this in action.