-
Notifications
You must be signed in to change notification settings - Fork 65
/
strapi-server.ts
77 lines (68 loc) · 2.57 KB
/
strapi-server.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
import { PropType } from "strapi-typed";
import { CommentsPluginConfig } from "./types";
import { isArray, isEmpty, isString } from "lodash";
import { REGEX, CONFIG_PARAMS } from "./server/utils/constants";
import server from "./server";
import contentTypes from "./content-types";
export = () => ({
...server,
contentTypes,
config: {
...server.config,
default: () => ({
...server.config.default,
}),
validator: (config: CommentsPluginConfig) => {
// Check enabledCollections values
const enabledCollections: PropType<
CommentsPluginConfig,
"enabledCollections"
> = config[CONFIG_PARAMS.ENABLED_COLLECTIONS];
const enabledCollectionsValid: boolean =
enabledCollections.filter((_: string) => !REGEX.uid.test(_)).length ===
0;
if (!enabledCollectionsValid) {
throw new Error(
`'enabledCollections' must contain Content Types identifiers in the format like 'api::<collection name>.<content type name>'`
);
}
// Check moderatorRoles values
const moderatorRoles: PropType<CommentsPluginConfig, "moderatorRoles"> =
config[CONFIG_PARAMS.MODERATOR_ROLES];
const moderatorRolesValid: boolean =
moderatorRoles.filter((_: string) => !isString(_)).length === 0;
if (!moderatorRolesValid) {
throw new Error(`'moderatorRoles' must roles keys`);
}
// Check approvalFlow values
const approvalFlow: PropType<CommentsPluginConfig, "approvalFlow"> =
config[CONFIG_PARAMS.APPROVAL_FLOW];
const approvalFlowValid: boolean =
approvalFlow.filter((_: string) => !REGEX.uid.test(_)).length === 0;
if (!approvalFlowValid) {
throw new Error(
`'approvalFlow' must contain Content Types identifiers in the format like 'api::<collection name>.<content type name>'`
);
}
// Check entryLabel keys and values
const entryLabels: PropType<CommentsPluginConfig, "entryLabel"> =
config[CONFIG_PARAMS.ENTRY_LABEL];
const entryLabelKeysValid: boolean =
Object.keys(entryLabels)
.filter((_: string) => _ !== "*")
.filter(
(_: string) =>
!(
REGEX.uid.test(_) &&
isArray(entryLabels[_]) &&
!isEmpty(entryLabels[_])
)
).length === 0;
if (!entryLabelKeysValid) {
throw new Error(
`'entryLabel' must contain records in the format like 'api::<collection name>.<content type name>': ['Field1', 'field_2']`
);
}
},
},
});