Repository: icebob/fastest-validator
Pull request #196 (2020-10-15 00:47:20)

allow converts objectID to string
objectID pass between service.actions should be converted to string automatically, all params of `moleculer-db-adapter-mongoose`'s actions does not accept objectID instance. If dev want an objectID instance, he should call with `convert:true`


Patch:
diff --git a/README.md b/README.md
index b5b920c..9eabde4 100644
--- a/README.md
+++ b/README.md
@@ -1108,8 +1108,7 @@ const schema = {
 ### Properties
 Property | Default  | Description
 -------- | -------- | -----------
-`convert`  | `false`   | If `true`, the validator converts ObjectID HexString representation to ObjectID `instance`
-
+`convert`  | `false`   | If `true`, the validator converts ObjectID HexString representation to ObjectID `instance`, if `hexString` the validator converts to HexString
 
 # Custom validator
 You can also create your custom validator.
diff --git a/index.d.ts b/index.d.ts
index f59dd99..e3584a7 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -343,7 +343,7 @@ declare module "fastest-validator" {
 		/**
 		 * Convert HexStringObjectID to ObjectID
 		 */
-		convert?: boolean;
+		convert?: boolean | "hexString";
 	}
 
 	/**
diff --git a/lib/rules/objectID.js b/lib/rules/objectID.js
index 36df94e..ae9e2d5 100644
--- a/lib/rules/objectID.js
+++ b/lib/rules/objectID.js
@@ -14,10 +14,12 @@ module.exports = function({ schema, messages, index }, path, context) {
 			${this.makeError({ type: "objectID", actual: "value", messages })}
 			return;
 		}
-
-		${schema.convert ? "return new ObjectID(value)" : "return value;" } ;
 	`);
 
+	if (schema.convert === true) src.push("return new ObjectID(value)");
+	else if (schema.convert === "hexString") src.push("return value.toString()");
+	else src.push("return value");
+
 	return {
 		source: src.join("\n")
 	};


Test patch:
diff --git a/test/rules/objectID.spec.js b/test/rules/objectID.spec.js
index 764c4e8..b4736e4 100644
--- a/test/rules/objectID.spec.js
+++ b/test/rules/objectID.spec.js
@@ -12,12 +12,12 @@ describe("Test rule: objectID", () => {
 
 		expect(check({ id: "5f082780b00cc7401fb8"})).toEqual([{ type: "objectID", field: "id", actual: "5f082780b00cc7401fb8", message }]);
 		expect(check({ id: new ObjectID() })).toEqual(true);
-      
+
 		const o = { id: "5f082780b00cc7401fb8e8fc" };
 		expect(check(o)).toEqual(true);
 		expect(o.id).toBe("5f082780b00cc7401fb8e8fc");
 	});
-   
+
 	it("should convert hexString-objectID to ObjectID", () => {
 		const check = v.compile({ id: { type: "objectID", ObjectID, convert: true } });
 		const  oid = new ObjectID();
@@ -28,6 +28,17 @@ describe("Test rule: objectID", () => {
 		expect(o.id).toEqual(oid);
 	});
 
+	it("should convert hexString-objectID to hexString", () => {
+		const check = v.compile({ id: { type: "objectID", ObjectID, convert: "hexString" } });
+		const  oid = new ObjectID();
+		const  oidStr = oid.toHexString();
+		const o = { id: oid };
+
+		expect(check({ id: oidStr })).toEqual(true);
+		expect(check(o)).toEqual(true);
+		expect(o.id).toEqual(oidStr);
+	});
+
 	it("should catch hexString problems when convert: true", () => {
 		const message = "The 'id' field must be an valid ObjectID";
 		const check = v.compile({ id: { type: "objectID", ObjectID, convert: true } });