Repository: icebob/fastest-validator
Pull request #185 (2020-08-24 12:35:41)

[FIX] "any" rule should have source code
-> #184
It is better that all the rules have source code (include `any`). Because it's difficult to handle rules that don't have source code


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e30822..bfd3b55 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,10 +1,3 @@
---------------------------------------------------
-<a name="1.6.1"></a>
-# 1.6.1 (2020-08-11)
-
-## Changes
-- Fix issue with `ObjectID` rule
-
 --------------------------------------------------
 <a name="1.6.0"></a>
 # 1.6.0 (2020-08-06)
diff --git a/lib/rules/any.js b/lib/rules/any.js
index 9a32ab0..0bd3d86 100644
--- a/lib/rules/any.js
+++ b/lib/rules/any.js
@@ -3,5 +3,12 @@
 /**	Signature: function(value, field, parent, errors, context)
  */
 module.exports = function(/*{ schema, messages }, path, context*/) {
-	return {};
+	const src = [];
+	src.push(`
+		return value;
+	`);
+
+	return {
+		source: src.join("\n")
+	};
 };
diff --git a/lib/validator.js b/lib/validator.js
index 241998a..4556e79 100644
--- a/lib/validator.js
+++ b/lib/validator.js
@@ -254,16 +254,11 @@ class Validator {
 
 			context.index++;
 			const res = rule.ruleFunction.call(this, rule, path, context);
-			if (res.source) {
-				res.source = res.source.replace(/%%INDEX%%/g, rule.index);
-				const fn = new Function("value", "field", "parent", "errors", "context", res.source);
-				context.fn[rule.index] = fn;
-				sourceCode.push(this.wrapRequiredCheckSourceCode(rule, innerSrc.replace(/%%INDEX%%/g, rule.index), context, resVar));
-				sourceCode.push(this.makeCustomValidator({vName: resVar, path: customPath, schema: rule.schema, context, messages: rule.messages, ruleIndex: rule.index}));
-
-			} else {
-				sourceCode.push(this.wrapRequiredCheckSourceCode(rule, "", context));
-			}
+			res.source = res.source.replace(/%%INDEX%%/g, rule.index);
+			const fn = new Function("value", "field", "parent", "errors", "context", res.source);
+			context.fn[rule.index] = fn;
+			sourceCode.push(this.wrapRequiredCheckSourceCode(rule, innerSrc.replace(/%%INDEX%%/g, rule.index), context, resVar));
+			sourceCode.push(this.makeCustomValidator({vName: resVar, path: customPath, schema: rule.schema, context, messages: rule.messages, ruleIndex: rule.index}));
 		}
 
 		return sourceCode.join("\n");
diff --git a/package-lock.json b/package-lock.json
index 144ffbe..56aab4d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "fastest-validator",
-  "version": "1.6.1",
+  "version": "1.6.0",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index b09e959..9df4db3 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "fastest-validator",
-  "version": "1.6.1",
+  "version": "1.6.0",
   "description": "The fastest JS validator library for NodeJS",
   "main": "index.js",
   "browser": "dist/index.min.js",


Test patch:
diff --git a/test/rules/any.spec.js b/test/rules/any.spec.js
index b526f08..4d86a7f 100644
--- a/test/rules/any.spec.js
+++ b/test/rules/any.spec.js
@@ -2,6 +2,7 @@
 
 const Validator = require("../../lib/validator");
 const v = new Validator();
+const anyRule = require("../../lib/rules/any");
 
 describe("Test rule: any", () => {
 
@@ -32,4 +33,8 @@ describe("Test rule: any", () => {
 		expect(check([])).toEqual(true);
 		expect(check({})).toEqual(true);
 	});
+
+	it("should have source code", () => {
+		expect(anyRule().source).toBeTruthy();
+	});
 });