Repository: icebob/fastest-validator
Pull request #190 (2020-09-18 22:32:23)

array-of-x Shorthand


Patch:
diff --git a/README.md b/README.md
index 2035e1d..cee1958 100644
--- a/README.md
+++ b/README.md
@@ -331,6 +331,15 @@ const schema = {
 }
 ```
 
+### Array of X 
+```js
+const schema = {
+    foo: "string[]" // means array of string
+}
+
+check({ foo: ["bar"] }) // true
+```
+
 ### Nested objects
 
 ```js
diff --git a/lib/validator.js b/lib/validator.js
index 90a3411..50171b9 100644
--- a/lib/validator.js
+++ b/lib/validator.js
@@ -272,31 +272,7 @@ class Validator {
 	 */
 	getRuleFromSchema(schema) {
 		if (typeof schema === "string") {
-			const str = schema;
-			const p = str.split("|").map(s => s.trim());
-			schema = {
-				type: p[0]
-			};
-			p.slice(1).map(s => {
-				const idx = s.indexOf(":");
-				if (idx !== -1) {
-					const key = s.substr(0, idx).trim();
-					let value = s.substr(idx + 1).trim();
-					if (value === "true" || value === "false")
-						value = value === "true";
-					else if (!Number.isNaN(Number(value))) {
-						value = Number(value);
-					}
-					schema[key] = value;
-				} else {
-					// boolean value
-					if (s.startsWith("no-"))
-						schema[s.slice(3)] = false;
-					else
-						schema[s] = true;
-				}
-			});
-
+			schema = this.parseShortHand(schema);
 		} else if (Array.isArray(schema)) {
 			if (schema.length == 0)
 				throw new Error("Invalid schema.");
@@ -346,6 +322,45 @@ class Validator {
 
 		return rule;
 	}
+   
+	/**
+	 * Parse rule from shorthand string
+	 * @param {String} str shorthand string
+	 * @param {Object} schema schema reference
+	 */
+
+	parseShortHand(str) {
+		const p = str.split("|").map((s) => s.trim());
+		let type = p[0];
+		let schema;
+		if (type.endsWith("[]")) {
+			schema = this.getRuleFromSchema({ type: "array", items: type.slice(0, -2) }).schema;
+		} else {
+			schema = {
+				type: p[0],
+			};
+		}
+
+		p.slice(1).map((s) => {
+			const idx = s.indexOf(":");
+			if (idx !== -1) {
+				const key = s.substr(0, idx).trim();
+				let value = s.substr(idx + 1).trim();
+				if (value === "true" || value === "false")
+					value = value === "true";
+				else if (!Number.isNaN(Number(value))) {
+					value = Number(value);
+				}
+				schema[key] = value;
+			} else {
+				// boolean value
+				if (s.startsWith("no-")) schema[s.slice(3)] = false;
+				else schema[s] = true;
+			}
+		});
+
+		return schema;
+	}
 
 	/**
 	 * Generate error source code.


Test patch:
diff --git a/test/validator.spec.js b/test/validator.spec.js
index d954a24..48a5245 100644
--- a/test/validator.spec.js
+++ b/test/validator.spec.js
@@ -242,6 +242,14 @@ describe("Test getRuleFromSchema method", () => {
 			expect(res.schema).toEqual({ type: "string", empty: false, alpha: false, trim: true, some: "1234kg" });
 		});
 
+		it("should convert arrayOf syntax", () => {
+			const res = v.getRuleFromSchema("string[]");
+			expect(res.schema).toEqual({ type: "array", items: "string" });
+
+			const res2 = v.getRuleFromSchema("string[]|optional|min:1");
+			expect(res2.schema).toEqual({ type: "array", optional: true, items: "string", min: 1 });
+		});
+
 	});
 
 	describe("Test objects shorthand rule ($$type)", () => {