-
Notifications
You must be signed in to change notification settings - Fork 3
/
string.gleam
89 lines (74 loc) · 1.82 KB
/
string.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gleam/option.{type Option, None, Some}
import gleam/float
import gleam/int
import gleam/regex
import gleam/string
import valid/common
fn is_not_empty_check(value: String) -> Option(String) {
case string.is_empty(value) {
True -> None
False -> Some(value)
}
}
/// Validate if a string is not empty
pub fn is_not_empty(error: e) {
common.custom(error, is_not_empty_check)
}
/// Validate if a string parses to an Int. Returns the Int if so.
pub fn is_int(error: e) {
common.custom(error, fn(value) {
int.parse(value)
|> option.from_result
})
}
/// Validate if a string parses to an Float. Returns the Float if so.
pub fn is_float(error: e) {
common.custom(error, fn(value) {
float.parse(value)
|> option.from_result
})
}
fn is_email_check(value: String) -> Option(String) {
let pattern = "^[\\w\\d]+@[\\w\\d\\.]+$"
case regex.from_string(pattern) {
Ok(re) -> {
case regex.check(with: re, content: value) {
True -> Some(value)
False -> None
}
}
Error(_) -> None
}
}
/// Validate if a string is an email.
///
/// This checks if a string follows a simple pattern `_@_`.
pub fn is_email(error: e) {
common.custom(error, is_email_check)
}
fn min_length_check(min: Int) {
fn(value: String) -> Option(String) {
let len = string.length(value)
case len < min {
True -> None
False -> Some(value)
}
}
}
/// Validate the min length of a string
pub fn min_length(error: e, min: Int) {
common.custom(error, min_length_check(min))
}
fn max_length_check(max: Int) {
fn(value: String) -> Option(String) {
let len = string.length(value)
case len > max {
True -> None
False -> Some(value)
}
}
}
/// Validate the max length of a string
pub fn max_length(error: e, max: Int) {
common.custom(error, max_length_check(max))
}