Repository: icebob/fastest-validator
Pull request #132 (2020-04-28 18:53:34)

HexString support


Patch:
diff --git a/README.md b/README.md
index 641875f..2ceeead 100644
--- a/README.md
+++ b/README.md
@@ -818,6 +818,7 @@ Property | Default  | Description
 `numeric`   | `null`   | The value must be a numeric string.
 `alphanum`   | `null`   | The value must be an alphanumeric string.
 `alphadash`   | `null`   | The value must be an alphabetic string that contains dashes.
+`hex`   | `null`   | The value must be a hex string.
 `trim`   | `null`   | If `true`, the value will be trimmed. _It's a sanitizer, it will change the value in the original object._
 `trimLeft`   | `null`   | If `true`, the value will be left trimmed. _It's a sanitizer, it will change the value in the original object._
 `trimRight`   | `null`   | If `true`, the value will be right trimmed. _It's a sanitizer, it will change the value in the original object._
@@ -1091,6 +1092,7 @@ Name                | Default text
 `stringAlpha`	| The '{field}' field must be an alphabetic string.
 `stringAlphanum`	| The '{field}' field must be an alphanumeric string.
 `stringAlphadash`	| The '{field}' field must be an alphadash string.
+`stringHex`	| The '{field}' field must be a hex string.
 `number`	| The '{field}' field must be a number.
 `numberMin`	| The '{field}' field must be greater than or equal to {expected}.
 `numberMax`	| The '{field}' field must be less than or equal to {expected}.
diff --git a/index.d.ts b/index.d.ts
index 5ab65c9..50aaac9 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -369,6 +369,10 @@ declare module 'fastest-validator' {
 		 * The value must be an alphabetic string that contains dashes
 		 */
 		alphadash?: boolean;
+		/**
+		 * The value must be a hex string that contains dashes
+		 */
+		hex?: boolean;
 		/**
 		 * if true and the type is not a String, converts with String()
 		 * @default false
@@ -786,7 +790,7 @@ declare module 'fastest-validator' {
 
 	type CheckerFunction<T = unknown> = (value: T, schema: ValidationSchema, path: string, parent: object | null, context: Context) => true | ValidationError[];
 
-	type CompilationFunction = (this: Validator, rule: CompilationRule, path: string, context: Context) => { sanitized?: boolean, source: string };
+	type CompilationFunction = (rule: CompilationRule, path: string, context: Context) => { sanitized?: boolean, source: string };
 
 	class Validator {
 		/**
diff --git a/lib/messages.js b/lib/messages.js
index d463b00..3b529d2 100644
--- a/lib/messages.js
+++ b/lib/messages.js
@@ -15,6 +15,7 @@ module.exports = {
 	stringAlpha: "The '{field}' field must be an alphabetic string.",
 	stringAlphanum: "The '{field}' field must be an alphanumeric string.",
 	stringAlphadash: "The '{field}' field must be an alphadash string.",
+	stringHex: "The '{field}' field must be a hex string.",
 
 	number: "The '{field}' field must be a number.",
 	numberMin: "The '{field}' field must be greater than or equal to {expected}.",
diff --git a/lib/rules/string.js b/lib/rules/string.js
index 40478d7..a5273c9 100644
--- a/lib/rules/string.js
+++ b/lib/rules/string.js
@@ -4,6 +4,7 @@ const NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/;
 const ALPHA_PATTERN = /^[a-zA-Z]+$/;
 const ALPHANUM_PATTERN = /^[a-zA-Z0-9]+$/;
 const ALPHADASH_PATTERN = /^[a-zA-Z0-9_-]+$/;
+const HEX_PATTERN = /^[0-9a-fA-F]+$/;
 
 /**	Signature: function(value, field, parent, errors, context)
  */
@@ -190,6 +191,14 @@ module.exports = function checkString({ schema, messages, customValidation }, pa
 		`);
 	}
 
+	if(schema.hex === true) {
+		src.push(`
+			if(value.length % 2 !== 0 || !${HEX_PATTERN.toString()}.test(value)) {
+				${this.makeError({ type: "stringHex",  actual: "origValue", messages })}
+			}
+		`);
+	}
+
 	src.push(`
 		${customValidation("value")}
 		return value;


Test patch:
diff --git a/test/rules/string.spec.js b/test/rules/string.spec.js
index 3dbf851..1464902 100644
--- a/test/rules/string.spec.js
+++ b/test/rules/string.spec.js
@@ -141,6 +141,17 @@ describe("Test rule: string", () => {
 
 	});
 
+	it("check hex string", () => {
+		const check = v.compile({ $$root: true, type: "string", hex: true});
+		const message = "The '' field must be a hex string.";
+
+		expect(check("abc")).toEqual([{type: "stringHex", actual: "abc", message }]);
+		expect(check("01020h")).toEqual([{type: "stringHex", actual: "01020h", message }]);
+
+		expect(check("0123456789abcdef")).toEqual(true);
+		expect(check("0123456789abcDEF")).toEqual(true);
+	});
+
 	it("should convert & check values", () => {
 		const check = v.compile({ $$root: true, type: "string", convert: true });
 		expect(check("")).toEqual(true);