nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: icebob/fastest-validator
Pull request #308 (2022-08-29 07:57:23)

fix record rule
fixes https://github.com/icebob/fastest-validator/issues/307

hugebdu: @FFKL please review

Patch:
diff --git a/lib/rules/record.js b/lib/rules/record.js
index 544d69e..fb25f1f 100644
--- a/lib/rules/record.js
+++ b/lib/rules/record.js
@@ -19,25 +19,32 @@ module.exports = function compileRecordRule({ schema, messages }, path, context)
 	const valueRuleName = schema.value || "any";
 
 	sourceCode.push(`
-		let tmp;
+		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 = `
-		tmp = ${context.async ? "await " : ""}context.fn[%%INDEX%%](key, field ? field + "." + key : key, value, errors, context);
+		sanitizedKey = ${context.async ? "await " : ""}context.fn[%%INDEX%%](key, field ? field + "." + key : key, record, errors, context);
 	`;
-	sourceCode.push(this.compileRule(keyRule, context, null, keyInnerSource, "tmp"));
+	sourceCode.push(this.compileRule(keyRule, context, null, keyInnerSource, "sanitizedKey"));
+	sourceCode.push("sanitizedValue = value = record[key];");
 
 	const valueRule = this.getRuleFromSchema(valueRuleName);
 	const valueInnerSource = `
-		tmp = ${context.async ? "await " : ""}context.fn[%%INDEX%%](value[key], field ? field + "." + key : key, value, errors, context);
+		sanitizedValue = ${context.async ? "await " : ""}context.fn[%%INDEX%%](value, field ? field + "." + key : key, record, errors, context);
 	`;
-	sourceCode.push(this.compileRule(valueRule, context, `${path}[key]`, valueInnerSource, "tmp"));
+	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")


Test patch:
diff --git a/test/rules/record.spec.js b/test/rules/record.spec.js
index 53e8454..259bb1d 100644
--- a/test/rules/record.spec.js
+++ b/test/rules/record.spec.js
@@ -60,6 +60,23 @@ describe("Test rule: record", () => {
 		expect(check({ John: "Doe", Jane: "Doe" })).toEqual(true);
 	});
 
+	describe("Test sanitization", () => {
+
+		it("should return sanitized record", async () => {
+			const check = v.compile({
+				field: {
+					type: "record",
+					key: {type: "string", alpha: true, trim: true},
+					value: {type: "string", alpha: true, default: "Smith", optional: true},
+				}
+			});
+
+			const value = { field: { John: "Doe", " Jane  ": null } };
+			expect(check(value)).toEqual(true);
+			expect(value).toEqual({ field: { John: "Doe", Jane: "Smith" } });
+		});
+	});
+
 	it.each([
 		{ rule: { type: "any" }, value: {} },
 		{ rule: { type: "array" }, value: [1, 2] },