cquill/changeset
Types
A changeset represents a set of changes to be validated and applied. It tracks the original data, changes, and any validation errors.
pub type Changeset {
Changeset(
data: option.Option(dict.Dict(String, dynamic.Dynamic)),
changes: dict.Dict(String, dynamic.Dynamic),
errors: dict.Dict(String, List(ValidationError)),
valid: Bool,
validated_fields: List(String),
)
}
Constructors
-
Changeset( data: option.Option(dict.Dict(String, dynamic.Dynamic)), changes: dict.Dict(String, dynamic.Dynamic), errors: dict.Dict(String, List(ValidationError)), valid: Bool, validated_fields: List(String), )Arguments
- data
-
The original data (if updating existing record)
- changes
-
The changes/new values being applied
- errors
-
Validation errors by field name
- valid
-
Whether the changeset is valid (no errors)
- validated_fields
-
Fields that were validated
Length constraint details
pub type LengthConstraint {
MinLength(min: Int)
MaxLength(max: Int)
ExactLength(length: Int)
RangeLength(min: Int, max: Int)
}
Constructors
-
MinLength(min: Int) -
MaxLength(max: Int) -
ExactLength(length: Int) -
RangeLength(min: Int, max: Int)
A validation error for a specific field
pub type ValidationError {
ValidationError(message: String, validation: ValidationType)
}
Constructors
-
ValidationError(message: String, validation: ValidationType)Arguments
- message
-
The error message
- validation
-
The validation type that failed
Types of validations that can fail
pub type ValidationType {
Required
Format
Length(constraint: LengthConstraint)
Inclusion
Exclusion
Range
Custom(name: String)
Uniqueness
Confirmation
Acceptance
}
Constructors
-
RequiredField is required but missing or empty
-
FormatField format doesn’t match pattern
-
Length(constraint: LengthConstraint)Field length is invalid
-
InclusionField value is not in allowed list
-
ExclusionField value is in excluded list
-
RangeNumeric value out of range
-
Custom(name: String)Custom validation failed
-
UniquenessField must be unique (checked externally)
-
ConfirmationConfirmation field doesn’t match
-
AcceptanceField must be accepted (for terms, etc.)
Values
pub fn add_error(
changeset: Changeset,
field: String,
message: String,
validation: ValidationType,
) -> Changeset
Add an error to the changeset
pub fn all_errors(
changeset: Changeset,
) -> List(#(String, ValidationError))
Get all errors as a list of (field, error) tuples
pub fn apply(
changeset: Changeset,
) -> Result(
dict.Dict(String, dynamic.Dynamic),
dict.Dict(String, List(ValidationError)),
)
Apply the changeset and return the result Returns Ok(changes) if valid, Error(errors) if invalid
pub fn change(
existing: dict.Dict(String, dynamic.Dynamic),
changes: dict.Dict(String, dynamic.Dynamic),
) -> Changeset
Create a changeset for updating existing data
pub fn clear_errors(
changeset: Changeset,
field: String,
) -> Changeset
Clear all errors for a specific field
pub fn delete_change(
changeset: Changeset,
field: String,
) -> Changeset
Delete a change from the changeset
pub fn error_count(changeset: Changeset) -> Int
Count the total number of validation errors across all fields Returns the sum of all errors on all fields
pub fn force_change(
changeset: Changeset,
field: String,
value: dynamic.Dynamic,
) -> Changeset
Force a change even if validation would fail Use with caution — this bypasses validation for the field
pub fn format_errors(changeset: Changeset) -> String
Format all errors as a human-readable string
pub fn format_field_errors(
changeset: Changeset,
field: String,
) -> String
Format errors for a specific field
pub fn get_change(
changeset: Changeset,
field: String,
) -> option.Option(dynamic.Dynamic)
Get a change value from the changeset
pub fn get_changes(
changeset: Changeset,
) -> dict.Dict(String, dynamic.Dynamic)
Get the changes from the changeset (regardless of validity)
pub fn get_data(
changeset: Changeset,
) -> option.Option(dict.Dict(String, dynamic.Dynamic))
Get the original data (if this is an update changeset)
pub fn get_errors(
changeset: Changeset,
field: String,
) -> List(ValidationError)
Get errors for a specific field
pub fn has_error(changeset: Changeset, field: String) -> Bool
Check if a specific field has errors
pub fn has_errors(changeset: Changeset) -> Bool
Check if the changeset has any validation errors This is a convenience function to check if any field has errors
pub fn merge_changes(
changeset: Changeset,
) -> dict.Dict(String, dynamic.Dynamic)
Merge changes from the changeset with the original data For updates, this combines old data with new changes
pub fn new(
changes: dict.Dict(String, dynamic.Dynamic),
) -> Changeset
Create a new changeset from input data (for inserts)
pub fn put_change(
changeset: Changeset,
field: String,
value: dynamic.Dynamic,
) -> Changeset
Put a change into the changeset
pub fn validate_acceptance(
changeset: Changeset,
field: String,
) -> Changeset
Validate that a boolean field is true (for accepting terms, etc.)
pub fn validate_confirmation(
changeset: Changeset,
field: String,
confirmation_field: String,
) -> Changeset
Validate that a confirmation field matches the original field e.g., password and password_confirmation
pub fn validate_custom(
changeset: Changeset,
field: String,
name: String,
validator: fn(Changeset) -> Result(Nil, String),
) -> Changeset
Apply a custom validation function The function should return Ok(Nil) if valid, or Error(message) if invalid
pub fn validate_exclusion(
changeset: Changeset,
field: String,
excluded: List(String),
) -> Changeset
Validate that a field value is NOT in a list of excluded values
pub fn validate_format(
changeset: Changeset,
field: String,
pattern: String,
) -> Changeset
Validate that a string field matches a regex pattern
pub fn validate_if_present(
changeset: Changeset,
field: String,
validator: fn(Changeset) -> Changeset,
) -> Changeset
Validate a field only if it is present (non-nil) Useful for optional fields that have constraints when provided
pub fn validate_inclusion(
changeset: Changeset,
field: String,
allowed: List(String),
) -> Changeset
Validate that a field value is in a list of allowed values
pub fn validate_length(
changeset: Changeset,
field: String,
min min: Int,
max max: Int,
) -> Changeset
Validate string length with min and max constraints
pub fn validate_length_exact(
changeset: Changeset,
field: String,
length: Int,
) -> Changeset
Validate string length with exact length constraint
pub fn validate_length_max(
changeset: Changeset,
field: String,
max: Int,
) -> Changeset
Validate string length with maximum constraint
pub fn validate_length_min(
changeset: Changeset,
field: String,
min: Int,
) -> Changeset
Validate string length with minimum constraint
pub fn validate_number_range(
changeset: Changeset,
field: String,
min min: option.Option(Int),
max max: option.Option(Int),
) -> Changeset
Validate that a numeric field is within a range