From 6256a8bdfc009aca0d737a4c7b1e45f6450b6db4 Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Thu, 2 Mar 2017 01:11:30 +0900
Subject: [PATCH] Custom validator support

---
 src/api/validator.ts | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/api/validator.ts b/src/api/validator.ts
index d75fe6420b..2562535c02 100644
--- a/src/api/validator.ts
+++ b/src/api/validator.ts
@@ -2,7 +2,7 @@ import * as mongo from 'mongodb';
 
 type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object';
 
-export default <T>(value: any, isRequired: boolean, type: Type): [T, string] => {
+export default <T>(value: any, isRequired: boolean, type: Type, validator?: (any) => boolean): [T, string] => {
 	if (value === undefined || value === null) {
 		if (isRequired) {
 			return [null, 'is-required']
@@ -49,5 +49,11 @@ export default <T>(value: any, isRequired: boolean, type: Type): [T, string] =>
 			break;
 	}
 
+	if (validator) {
+		if (!validator(value)) {
+			return [null, 'invalid-format'];
+		}
+	}
+
 	return [value, null];
 };