-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathvalidations.js
113 lines (110 loc) · 4.11 KB
/
validations.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
import path from 'node:path';
import { Util } from './util.js';
/**
* @typedef {import('../../types/mcdev.d.js').validationRuleList} validationRuleList
* @typedef {import('../../types/mcdev.d.js').validationRuleTest} validationRuleTest
* @typedef {import('../../types/mcdev.d.js').CodeExtract} CodeExtract
*/
/** @type {validationRuleList} */
let customRules = {};
let customRuleImport;
try {
customRuleImport = await import('file://' + path.join(process.cwd(), '.mcdev-validations.js'));
} catch {
Util.logger.debug('.mcdev-validations.js not found');
}
/**
*
* @param {any} definition type definition
* @param {any} item MetadataItem
* @param {string} targetDir folder in which the MetadataItem is deployed from (deploy/cred/bu)
* @param {CodeExtract[]} [codeExtractItemArr] array of code snippets
* @returns {Promise.<any>} MetadataItem
*/
export default async function validation(definition, item, targetDir, codeExtractItemArr) {
try {
if (customRuleImport) {
customRules = customRuleImport
? await customRuleImport.validation(
definition,
item,
targetDir,
Util,
codeExtractItemArr
)
: {};
}
} catch (ex) {
Util.logger.errorStack(
ex,
'Could not load custom validation rules from .mcdev-validations.js'
);
}
/** @type {validationRuleList} */
const defaultRules = {
noGuidKeys: {
failedMsg: 'Please update the key to a readable value. Currently still in GUID format.',
/**
* @type {validationRuleTest}
*/
passed: function () {
const key = item[definition.keyField];
if (key) {
const regex = /^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/i;
return !regex.test(String(key).toLowerCase());
} else {
Util.logger.debug('validation-noGuidKeys: key not found');
return true;
}
},
},
noRootFolder: {
failedMsg: 'Root folder not allowed. Current folder: ' + item.r__folder_Path,
/**
* @type {validationRuleTest}
*/
passed: function () {
/** @type {string} */
const folderPath = item.r__folder_Path;
if (!folderPath) {
// some types do not support folders
return true;
}
const doNotEvaluate = ['automation', 'attributeSet', 'list'];
if (
doNotEvaluate.includes(definition.type) ||
(definition.type === 'asset' && item?.assetType?.name === 'webpage')
) {
// this subtype is not visible in the interface and hence always technically sits in the root
return true;
}
return folderPath.includes('/');
},
},
noAmpscriptHtmlTag: {
failedMsg:
'Please use %%[]%% instead of <script runat="server" language="ampscript"></script> for AMPscript',
/**
* @type {validationRuleTest}
*/
passed: function () {
if (definition.type === 'asset' && Array.isArray(codeExtractItemArr)) {
for (const codeExtractItem of codeExtractItemArr) {
if (
codeExtractItem.content.includes(
'<script runat="server" language="ampscript">'
)
) {
return false;
}
}
return true;
} else {
return true; // test case should pass since non-asset metadatatypes get a pass
}
},
},
};
return Object.assign({}, defaultRules, customRules);
}