dataval/parser
Parsers turn raw strings into typed values.
A parser always returns a value of the target type, even when the input is invalid. It’s a deliberate trade-off to keep the pipeline composable and type-safe.
Example
import dataval/field
import gleam/string
type Faction {
Alliance
Horde
}
type MyCustomErrors {
UnknownFaction
}
fn faction_parser(faction: String) {
case string.lowercase(faction) {
"alliance" -> field.Parsed(Alliance, [])
"horde" -> field.Parsed(Horde, [])
_ ->
field.Parsed(Alliance, [
field.CustomError(UnknownFaction),
])
}
}
fn faction_field() {
field.new("faction")
|> field.with_parser(faction_parser)
}
pub fn main() {
let faction = faction_field()
assert Ok(Alliance) == field.validate(faction, "alliance")
assert Ok(Horde) == field.validate(faction, "horde")
assert Error([field.CustomError(UnknownFaction)])
== field.validate(faction, "nope")
}
Values
pub fn bool(value: String) -> field.Parsed(Bool, custom)
pub fn float(value: String) -> field.Parsed(Float, custom)
pub fn int(value: String) -> field.Parsed(Int, custom)