//
//  FormValidator.java
//  blogwt
//
//  Created by William Shakour (billy1380) on 15 Nov 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.server.api.validation;

import java.util.HashMap;
import java.util.Map;

import com.willshex.blogwt.shared.api.datatype.Field;
import com.willshex.blogwt.shared.api.datatype.Form;
import com.willshex.blogwt.shared.api.validation.ApiError;
import com.willshex.gson.json.service.server.InputValidationException;

/**
 * @author William Shakour (billy1380)
 *
 */
public class FormValidator {
	private static final String type = Form.class.getSimpleName();

	public static Form validate (Form form, String name)
			throws InputValidationException {
		if (form == null)
			ApiValidator.throwServiceError(InputValidationException.class,
					ApiError.InvalidValueNull, type + ": " + name);

		Map<String, Field> fields = new HashMap<String, Field>();
		for (Field field : form.fields) {
			// create lookup
			fields.put(field.name, field);
		}

		// verify captch

		return form;
	}
}

@@ -211,57 +211,8 @@
 n;%0A%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 impo
@@ -826,66 +826,321 @@
 %0A%0A%09%09
-Map%3CString, Field%3E fields = new HashMap%3CString, F
+if (form.fields == null)%0A%09%09%09ApiValidator.throwServiceError(InputValidationException.class,%0A%09%09%09%09%09ApiError.InvalidValueNull, type + %22: %22 + name + %22.fields%22);%0A%0A%09%09if (form.fields.size() == 0)%0A%09%09%09ApiValidator.throwServiceError(InputValidationException.class,%0A%09%09%09%09%09ApiError.EmptyForm, type + %22: %22 + name + %22.f
 ield
-%3E(
+s%22
 );%0A
+%0A
 %09%09fo
@@ -1178,56 +1178,57 @@
 %0A%09%09%09
-// create lookup%0A%09%09%09fields.put
+FieldValidator.validate
 (field
-.
+, 
 name
-,
  
++ %22.
 field
+%5Bn%5D%22
 );%0A%09
@@ -1237,24 +1237,232 @@
 %0A%0A%09%09
-// verify captch
+if (form.name != null%0A%09%09%09%09&& (form.name.length() == 0 %7C%7C form.name.length() %3E 512))%0A%09%09%09ApiValidator.throwServiceError(InputValidationException.class,%0A%09%09%09%09%09ApiError.BadLength, Integer.toString(1),%0A%09%09%09%09%09Integer.toString(512));
 %0A%0A%09%09

validate form using field validator