Repository: icebob/fastest-validator
Pull request #180 (2020-07-30 09:43:25)

[feat] single line string validation


Patch:
diff --git a/README.md b/README.md
index d424b10..66a270e 100644
--- a/README.md
+++ b/README.md
@@ -917,6 +917,7 @@ Property | Default  | Description
 `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.
+`singleLine`   | `null`   | The value must be a single line 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._
@@ -1296,6 +1297,7 @@ Name                | Default text
 `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.
+`stringSingleLine`	| The '{field}' field must be a single line 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 a028935..a01766e 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -401,9 +401,15 @@ declare module "fastest-validator" {
 		 */
 		alphadash?: boolean;
 		/**
-		 * The value must be a hex string that contains dashes
+		 * The value must be a hex string
+		 * @default false
 		 */
 		hex?: boolean;
+		/**
+		 * The value must be a singleLine string
+		 * @default false
+		 */
+		singleLine?: boolean;
 		/**
 		 * if true and the type is not a String, converts with String()
 		 * @default false
diff --git a/lib/messages.js b/lib/messages.js
index be46b6d..039175d 100644
--- a/lib/messages.js
+++ b/lib/messages.js
@@ -16,6 +16,7 @@ module.exports = {
 	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.",
+	stringSingleLine: "The '{field}' field must be a single line 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 1863e91..127ac1d 100644
--- a/lib/rules/string.js
+++ b/lib/rules/string.js
@@ -207,6 +207,14 @@ module.exports = function checkString({ schema, messages }, path, context) {
 		`);
 	}
 
+	if(schema.singleLine === true) {
+		src.push(`
+			if(value.includes("\\n")) {
+				${this.makeError({ type: "stringSingleLine", messages })}
+			}
+		`);
+	}
+
 	src.push(`
 		return value;
 	`);


Test patch:
diff --git a/test/rules/string.spec.js b/test/rules/string.spec.js
index 7bbc38b..defaf14 100644
--- a/test/rules/string.spec.js
+++ b/test/rules/string.spec.js
@@ -178,6 +178,18 @@ describe("Test rule: string", () => {
 		expect(check("0123456789abcDEF")).toEqual(true);
 	});
 
+	it("check singleLine string", () => {
+		const check = v.compile({ $$root: true, type: "string", singleLine: true});
+		const message = "The '' field must be a single line string.";
+
+		expect(check("abc")).toEqual(true);
+		expect(check("abc\n")).toEqual([{type: "stringSingleLine", message }]);
+		expect(check(`
+			abc
+			def
+		`)).toEqual([{type: "stringSingleLine", message }]);
+	});
+
 	it("should convert & check values", () => {
 		const check = v.compile({ $$root: true, type: "string", convert: true });
 		expect(check("")).toEqual(true);
diff --git a/test/typescript/rules/string.spec.ts b/test/typescript/rules/string.spec.ts
index 55ec352..ce0e95b 100644
--- a/test/typescript/rules/string.spec.ts
+++ b/test/typescript/rules/string.spec.ts
@@ -181,6 +181,19 @@ describe('TypeScript Definitions', () => {
             expect(check(new Date())).toEqual(true);
         });
 
+        it("check singleLine string", () => {
+            const schema: RuleString = { $$root: true, type: "string", singleLine: true }
+            const check = v.compile(schema);
+            const message = "The '' field must be a single line string.";
+    
+            expect(check("abc")).toEqual(true);
+            expect(check("abc\n")).toEqual([{type: "stringSingleLine", message }]);
+            expect(check(`
+                abc
+                def
+            `)).toEqual([{type: "stringSingleLine", message }]);
+        });
+
         describe('Test sanitization', () => {
 
             it('should convert & check values', () => {