-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathvalidation.ai
More file actions
38 lines (36 loc) · 1019 Bytes
/
validation.ai
File metadata and controls
38 lines (36 loc) · 1019 Bytes
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
class User {
@string(min_len=3, max=20)
name: str,
@number(min=18, strict_int=true, strict_float=true)
age: int,
@in(["male", "female"])
gender: str = "male",
}
let u1 = User("Le", 20.0, "male") |err| {
print("Validate error:", err);
};
// Validate error: {
// loc: [name], input: Le, type: validation_error,
// msg: String length is less than the minimum length of 3
// }
let u2 = User("Lee", 17, "male") |err| {
print("Validate error:", err);
};
// Validate error: {
// loc: [age], input: 17, type: validation_error,
// msg: Number is less than the minimum value of 18
// }
let u3 = User("Lee", 20, "boy") |err| {
print("Validate error:", err);
};
// Validate error: {
// loc: [gender], input: boy, type: validation_error,
// msg: Value is not in the list of allowed values
// }
let u4 = User("Lee") |err| {
print("Validate error:", err);
};
// Validate error: {
// loc: [age], input: nil, type: missing,
// msg: Field required
// }