validator/option

Functions

is_some

pub fn is_some(
  error: a,
) -> fn(Option(b)) -> Result(b, tuple(a, List(a)))

Validate that a value is not None. Returns the value if Some.

Example

type PersonInput { PersonInput(name: Option(String)) }

type PersonValid { PersonValid(name: String) }

let validator = fn(person) {
	v.build1(PersonValid)
	|> v.validate(person.name, option.is_some("Name is null"))
}

optional

pub fn optional(
  validator: fn(a) -> Result(a, tuple(b, List(b))),
) -> fn(Option(a)) -> Result(Option(a), tuple(b, List(b)))

Validate an optional value.

Run the validator only if the value is Some. If the value is None then just return None back.

Example

let validator = fn(person) {
	v.build1(PersonValid)
	|> v.validate(
		person.name,
		option.optional(string.min_length("Short", 3))
	)
}