-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathrecord.js
52 lines (45 loc) · 1.64 KB
/
record.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
function patchKeyRuleMessages(rule) {
for (const type in rule.messages) {
if (type.startsWith("string")) {
rule.messages[type] = rule.messages[type].replace(" field ", " key ");
}
}
}
module.exports = function compileRecordRule({ schema, messages }, path, context) {
const sourceCode = [];
sourceCode.push(`
if (typeof value !== "object" || value === null || Array.isArray(value)) {
${this.makeError({ type: "record", actual: "value", messages })}
return value;
}
`);
const keyRuleName = schema.key || "string";
const valueRuleName = schema.value || "any";
sourceCode.push(`
const record = value;
let sanitizedKey, sanitizedValue;
const result = {};
for (let key in value) {
`);
sourceCode.push("sanitizedKey = value = key;");
const keyRule = this.getRuleFromSchema(keyRuleName);
patchKeyRuleMessages(keyRule);
const keyInnerSource = `
sanitizedKey = ${context.async ? "await " : ""}context.fn[%%INDEX%%](key, field ? field + "." + key : key, record, errors, context);
`;
sourceCode.push(this.compileRule(keyRule, context, null, keyInnerSource, "sanitizedKey"));
sourceCode.push("sanitizedValue = value = record[key];");
const valueRule = this.getRuleFromSchema(valueRuleName);
const valueInnerSource = `
sanitizedValue = ${context.async ? "await " : ""}context.fn[%%INDEX%%](value, field ? field + "." + key : key, record, errors, context);
`;
sourceCode.push(this.compileRule(valueRule, context, `${path}[key]`, valueInnerSource, "sanitizedValue"));
sourceCode.push("result[sanitizedKey] = sanitizedValue;");
sourceCode.push(`
}
`);
sourceCode.push("return result;");
return {
source: sourceCode.join("\n")
};
};