valguard

Types

Key/Value pair for encoding validation errors. Key indicates the name of the field that failed validation and Value is the error message.

pub type ValidationError {
  ValidationError(key: String, value: String)
}

Constructors

  • ValidationError(key: String, value: String)

Values

pub fn append_error(
  result: Result(Nil, ValidationError),
  list: List(ValidationError),
) -> List(ValidationError)

Takes a validation result and list of validation errors If result is error, it adds it to the list

pub fn collect_errors(
  inputs: List(Result(Nil, ValidationError)),
) -> List(ValidationError)

Takes a list of validation results and returns a list of the errors Returns an empty list if no errors

pub fn date_is_valid(
  datetime: String,
  message message: String,
) -> Result(Nil, String)

Validates that a date is valid by checking it can be parsed correctly

pub fn email_is_valid(
  email: String,
  message message: String,
) -> Result(Nil, String)

Validates if entered email is a valid email address

pub fn float_max(
  value: Float,
  max maximum: Float,
  message message: String,
) -> Result(Nil, String)

Validates a float is at most a maximum value. Returns an error if value is greater than the maximum

pub fn float_min(
  value: Float,
  min minimum: Float,
  message message: String,
) -> Result(Nil, String)

Validates a float is at least a minimum value. Returns an error if value is less than the minimum

pub fn float_required(
  value: Float,
  message message: String,
) -> Result(Nil, String)

Requires a float to be less than or greater than 0.0 to be considered required

pub fn int_max(
  value: Int,
  max maximum: Int,
  message message: String,
) -> Result(Nil, String)

Validates an int is at most a maximum value. Returns an error if value is greater than the maximum

pub fn int_min(
  value: Int,
  min minimum: Int,
  message message: String,
) -> Result(Nil, String)

Validates an int is at least a minimum value. Returns an error if value is less than the minimum

pub fn int_required(
  value: Int,
  message message: String,
) -> Result(Nil, String)

Requires a int to be less than or greater than 0 to be considered required

pub fn list(
  key: String,
  list: List(fn() -> Result(Nil, String)),
) -> Result(Nil, ValidationError)

Validates a list of requirements lazily.

Takes a key and list of validation functions.

Returns Ok(Nil) if success or Error(String) at first issue

pub fn prepare(
  errors: List(ValidationError),
) -> Result(Nil, List(ValidationError))

Takes a list of validation errors and converts it to a result Returns ok if list is empty

pub fn prepare_with(
  errors: List(ValidationError),
  custom_type: fn(List(ValidationError)) -> custom_type,
) -> Result(Nil, custom_type)

Takes a list of validation errors and converts it to a result wrapping the errors in a custom type. Returns Ok(Nil) if list is empty.

This functions similar to valguard.prepare.

pub fn string_max(
  value: String,
  max maximum: Int,
  message message: String,
) -> Result(Nil, String)

Validates a string is at most a maximum length

pub fn string_min(
  value: String,
  min minimum: Int,
  message message: String,
) -> Result(Nil, String)

Validates a string is a minimum length

pub fn string_required(
  value: String,
  message message: String,
) -> Result(Nil, String)

Requires a string to not be empty to be considered required

pub fn with(
  key: String,
  value: value,
  list: List(fn(value) -> Result(Nil, String)),
) -> Result(Nil, ValidationError)

Validates a list of requirements lazily.

Takes a key, value and list of validation functions.

Runs validation functions lazily and returns the result. Returns Ok(Nil) if success or Error(String) at first issue

pub fn with_optional(
  key: String,
  option: option.Option(value),
  list: List(fn(value) -> Result(Nil, String)),
) -> Result(Nil, ValidationError)

Validates a list of requirements lazily.

Takes a key, an optional value and list of validation functions.

Runs validation functions lazily and returns the result. Returns Ok(Nil) if optional value is None or all validations are successful. Returns Error(String) at first issue.

Search Document