Skip to content

Commit

Permalink
feat: string validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackder committed Aug 29, 2024
1 parent cd9fe3e commit 7e4bf88
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions src/toy.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,87 @@ pub fn string_email(dec: Decoder(String)) -> Decoder(String) {
}
}

pub fn string_nonempty(dec: Decoder(String)) -> Decoder(String) {
fn(data) {
case dec(data) {
#(default, Ok(data)) -> {
let len = string.length(data)
case len > 0 {
True -> #(default, Ok(data))
False -> #(
default,
Error([
ToyError(
error: ValidationFailed(
check: "string_nonempty",
expected: "non_empty",
found: "[]",
),
path: [],
),
]),
)
}
}
with_decode_error -> with_decode_error
}
}
}

pub fn string_min(dec: Decoder(String), minimum: Int) -> Decoder(String) {
fn(data) {
case dec(data) {
#(default, Ok(data)) -> {
let len = string.length(data)
case len >= minimum {
True -> #(default, Ok(data))
False -> #(
default,
Error([
ToyError(
error: ValidationFailed(
check: "string_min",
expected: ">=" <> int.to_string(minimum),
found: int.to_string(len),
),
path: [],
),
]),
)
}
}
with_decode_error -> with_decode_error
}
}
}

pub fn string_max(dec: Decoder(String), maximum: Int) -> Decoder(String) {
fn(data) {
case dec(data) {
#(default, Ok(data)) -> {
let len = string.length(data)
case len < maximum {
True -> #(default, Ok(data))
False -> #(
default,
Error([
ToyError(
error: ValidationFailed(
check: "string_max",
expected: "<" <> int.to_string(maximum),
found: int.to_string(len),
),
path: [],
),
]),
)
}
}
with_decode_error -> with_decode_error
}
}
}

// Int validation

pub fn int_min(dec: Decoder(Int), minimum: Int) -> Decoder(Int) {
Expand Down Expand Up @@ -458,6 +539,88 @@ pub fn float_range(
}
}

// List validation

pub fn list_nonempty(dec: Decoder(List(a))) -> Decoder(List(a)) {
fn(data) {
case dec(data) {
#(default, Ok(data)) -> {
case data {
[_, ..] -> #(default, Ok(data))
_ -> #(
default,
Error([
ToyError(
error: ValidationFailed(
check: "list_nonempty",
expected: "non_empty",
found: "[]",
),
path: [],
),
]),
)
}
}
with_decode_error -> with_decode_error
}
}
}

pub fn list_min(dec: Decoder(List(a)), minimum: Int) -> Decoder(List(a)) {
fn(data) {
case dec(data) {
#(default, Ok(data)) -> {
let len = list.length(data)
case len >= minimum {
True -> #(default, Ok(data))
False -> #(
default,
Error([
ToyError(
error: ValidationFailed(
check: "list_min",
expected: ">=" <> int.to_string(minimum),
found: int.to_string(len),
),
path: [],
),
]),
)
}
}
with_decode_error -> with_decode_error
}
}
}

pub fn list_max(dec: Decoder(List(a)), maximum: Int) -> Decoder(List(a)) {
fn(data) {
case dec(data) {
#(default, Ok(data)) -> {
let len = list.length(data)
case len < maximum {
True -> #(default, Ok(data))
False -> #(
default,
Error([
ToyError(
error: ValidationFailed(
check: "list_max",
expected: "<" <> int.to_string(maximum),
found: int.to_string(len),
),
path: [],
),
]),
)
}
}
with_decode_error -> with_decode_error
}
}
}

// Generic validation

pub fn map(dec: Decoder(a), fun: fn(a) -> b) -> Decoder(b) {
Expand Down

0 comments on commit 7e4bf88

Please sign in to comment.