-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcheck-frontmatter-type.ts
91 lines (81 loc) · 2.44 KB
/
check-frontmatter-type.ts
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
90
91
interface FrontMatter {
title: string | null;
tags: string[];
secret: boolean;
updatedAt: string | null;
id: string | null;
organizationUrlName: string | null;
slide: boolean;
}
interface CheckType {
getMessage: (item: FrontMatter) => string;
isValid: (item: FrontMatter) => boolean;
}
export const checkFrontmatterType = (frontMatter: FrontMatter): string[] => {
const checkFrontMatterTypes = [
checkTitle,
checkTags,
checkSecret,
checkUpdatedAt,
checkId,
checkOrganizationUrlName,
checkSlide,
];
return getErrorMessages(frontMatter, checkFrontMatterTypes);
};
const checkTitle: CheckType = {
getMessage: () => "titleは文字列で入力してください",
isValid: ({ title }) => {
return title === null || typeof title === "string";
},
};
const checkTags: CheckType = {
getMessage: () => "tagsは配列で入力してください",
isValid: ({ tags }) => {
return Array.isArray(tags);
},
};
const checkSecret: CheckType = {
getMessage: () => "privateの設定はtrue/falseで入力してください",
isValid: ({ secret }) => {
return typeof secret === "boolean";
},
};
const checkUpdatedAt: CheckType = {
getMessage: () => "updated_atは文字列で入力してください",
isValid: ({ updatedAt }) => {
return updatedAt === null || typeof updatedAt === "string";
},
};
const checkOrganizationUrlName: CheckType = {
getMessage: () => "organization_url_nameは文字列で入力してください",
isValid: ({ organizationUrlName }) => {
return (
organizationUrlName === null || typeof organizationUrlName === "string"
);
},
};
const checkSlide: CheckType = {
getMessage: () =>
"slideの設定はtrue/falseで入力してください(破壊的な変更がありました。詳しくはリリースをご確認ください https://github.com/increments/qiita-cli/releases/tag/v0.5.0)",
isValid: ({ slide }) => {
return typeof slide === "boolean";
},
};
const checkId: CheckType = {
getMessage: () => "idは文字列で入力してください",
isValid: ({ id }) => {
return id === null || typeof id === "string";
},
};
const getErrorMessages = (
frontMatter: FrontMatter,
checkTypes: CheckType[],
): string[] => {
return checkTypes.reduce((errorMessages: string[], checkType) => {
if (!checkType.isValid(frontMatter)) {
errorMessages.push(checkType.getMessage(frontMatter));
}
return errorMessages;
}, []);
};