-
Notifications
You must be signed in to change notification settings - Fork 2
/
06-list-fields.js
51 lines (46 loc) · 1.32 KB
/
06-list-fields.js
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
import { withFields, string, number, boolean, fields } from "@commodo/fields";
// We've wrapped this code sample into an async function, so we can
// make function calls with the await keyword, and make our code look nicer.
(async () => {
try {
const Stats = withFields({
views: number(),
books: number(),
bestYears: number({
list: true,
validation: (value) => {
// Since the received value can be null, we also check if the value is an array.
if (Array.isArray(value) && value.length > 2) {
throw new Error(
"You can only set two best years in the author's carrers."
);
}
}
})
})();
const Author = withFields({
firstName: string(),
lastName: string(),
age: number(),
isFamous: boolean(),
stats: fields({ instanceOf: Stats })
})();
const author = new Author();
author.populate({
firstName: "John",
lastName: "Doe",
age: 25,
isFamous: false,
stats: {
views: 150,
books: 7,
bestYears: [2010, 2020, 2022]
}
});
console.log("Best years:", author.stats.bestYears);
await author.validate();
} catch (e) {
console.log("Error message: ", e.message);
console.log("Error data: ", e.data);
}
})();