Skip to content

Commit

Permalink
added new validator
Browse files Browse the repository at this point in the history
  • Loading branch information
synapticloop committed Dec 30, 2015
1 parent 3b6c899 commit f19ad9d
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<target name="test-h2zero" description="test the h2zero generation">
<delete dir="generate/src/main/java/*" />
<taskdef resource="h2zero.properties" classpathref="classpath-h2zero-dist" />
<h2zero inFile="src/test/resources/sample.h2zero" outDir="." verbose="true" />
<h2zero inFile="src/test/resources/sample.h2zero" outDir="." verbose="false" />
<!-- now try and javac the generated code
<javac srcdir="${basedir}/src/test/java" destdir="${basedir}/build/classes" debug="on" classpathref="compile.classpath" includeantruntime="false" compiler="javac1.6" source="1.6" target="1.6" />
-->
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Nov 06 16:01:55 GMT 2015
#Wed Nov 25 10:24:41 GMT 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-bin.zip
14 changes: 13 additions & 1 deletion src/main/java/synapticloop/h2zero/H2ZeroParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import synapticloop.h2zero.validator.field.FieldDefaultValueValidator;
import synapticloop.h2zero.validator.field.FieldIgnoredKeysValidator;
import synapticloop.h2zero.validator.field.FieldNameDuplicateValidator;
import synapticloop.h2zero.validator.field.FieldNotNullLengthValidator;
import synapticloop.h2zero.validator.finder.FinderAutoIndexValidator;
import synapticloop.h2zero.validator.finder.FinderInQueryValidator;
import synapticloop.h2zero.validator.finder.FinderNameValidator;
Expand Down Expand Up @@ -67,6 +68,11 @@
import synapticloop.h2zero.validator.updater.UpdaterSetClauseValidator;
import synapticloop.h2zero.validator.updater.UpdaterWhereClauseValidator;

/**
* This is the parser for the h2zero generator
*
* @author synapticloop
*/

public class H2ZeroParser {
private Database database = null;
Expand All @@ -81,7 +87,6 @@ public class H2ZeroParser {
validators.add(new OptionsGeneratorsValidator());

// overall validators

validators.add(new ForeignKeyTableValidator());
validators.add(new UniqeAndIndexValidator());

Expand All @@ -97,6 +102,7 @@ public class H2ZeroParser {
validators.add(new FieldDefaultValueValidator());
validators.add(new FieldNameDuplicateValidator());
validators.add(new FieldIgnoredKeysValidator());
validators.add(new FieldNotNullLengthValidator());


// Finder validators
Expand All @@ -111,22 +117,26 @@ public class H2ZeroParser {
validators.add(new FinderQueryParameterNameValidator());
validators.add(new FinderQueryParameterNumberValidator());


// inserter validators
validators.add(new InserterQueryParameterNameValidator());
validators.add(new InserterNameValidator());
validators.add(new InserterKeyValidator());


// deleter validators
validators.add(new DeleterNameValidator());
validators.add(new DeleterWhereClauseValidator());


// updater validators
validators.add(new UpdaterQueryParameterNameValidator());
validators.add(new UpdaterNameValidator());
validators.add(new UpdaterWhereClauseValidator());
validators.add(new UpdaterSetClauseValidator());
validators.add(new UpdaterKeyValidator());


// counter validators
validators.add(new CounterQueryParameterNameValidator());
validators.add(new CounterSelectClauseValidator());
Expand All @@ -135,6 +145,7 @@ public class H2ZeroParser {
validators.add(new CounterKeyValidator());
validators.add(new CounterNameValidator());


// question validators
validators.add(new QuestionQueryParameterNameValidator());
validators.add(new QuestionSelectClauseValidator());
Expand All @@ -143,6 +154,7 @@ public class H2ZeroParser {
validators.add(new QuestionKeyValidator());
validators.add(new QuestionNameValidator());


// constant validators
validators.add(new ConstantTableValidator());
validators.add(new ConstantDeleterValidator());
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/synapticloop/h2zero/model/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public class Database {
private Set<String> tableNames = new HashSet<String>();
private int defaultStatementCacheSize = 1024;

/**
* Parse and create a new database object from the passed in JSON object
*
* @param jsonObject the database jsonObject
*
* @throws H2ZeroParseException if there was an error parsing the json object
*/
public Database(JSONObject jsonObject) throws H2ZeroParseException {
JSONObject databaseJson = null;
try {
Expand Down Expand Up @@ -147,6 +154,23 @@ public BaseField getField(String fieldName) {
return(null);
}

/**
* Get a field from a specific table
*
* @param tableName the name of the table
* @param fieldName the name of the field
*
* @return the basefield for the table, or null if it doesn't exist
*/
public BaseField getTableField(String tableName, String fieldName) {
Table table = tableLookup.get(tableName);
if(null == table) {
return(null);
} else {
return(table.getField(fieldName));
}
}

public String getPackagePath() { return(NamingHelper.convertToPath(packageName)); }
public static Table getTableLookup(String tableName) { return(tableLookup.get(tableName)); }

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/synapticloop/h2zero/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -16,6 +18,7 @@
import synapticloop.h2zero.model.util.FieldLookupHelper;
import synapticloop.h2zero.model.util.JSONKeyConstants;
import synapticloop.h2zero.util.JsonHelper;
import synapticloop.h2zero.util.KeyHelper;
import synapticloop.h2zero.util.NamingHelper;

/**
Expand All @@ -30,6 +33,21 @@ public class Table extends BaseSchemaObject {
ignoredKeys.add("cacheFindAll");
}

public static Set<String> ALLOWABLE_KEYS = new HashSet<String>();
static {
ALLOWABLE_KEYS.add("name");
ALLOWABLE_KEYS.add("comments");
ALLOWABLE_KEYS.add("fields");
ALLOWABLE_KEYS.add("constants");
ALLOWABLE_KEYS.add("fieldFinders");
ALLOWABLE_KEYS.add("finders");
ALLOWABLE_KEYS.add("questions");
ALLOWABLE_KEYS.add("updaters");
ALLOWABLE_KEYS.add("counters");
ALLOWABLE_KEYS.add("deleters");
ALLOWABLE_KEYS.add("inserters");
}

private static Map<String, String> replacementKeys = new HashMap<String, String>();
static {
replacementKeys.put("comment", "comments");
Expand Down Expand Up @@ -98,6 +116,8 @@ public Table(JSONObject jsonObject, int defaultStatementCacheSize) throws H2Zero
}
}

// now we are going to go through and determine all of the
KeyHelper.findMissingKeys(this, jsonObject, ALLOWABLE_KEYS);
// now for the fields
populateFields(jsonObject);

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/synapticloop/h2zero/model/form/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Form {
public Form(Database database, JSONObject jsonObject) throws H2ZeroParseException {
this.database = database;
this.name = JsonHelper.getStringValue(jsonObject, "name", null);
System.out.println(this.name);
this.respondTo = JsonHelper.getStringValue(jsonObject, "respondTo", null);

if(null == name) {
Expand Down Expand Up @@ -75,12 +76,18 @@ private void populateFields(JSONObject jsonObject) throws H2ZeroParseException {
ref = JsonHelper.getStringValue(fieldObject, "ref", null);
System.out.println("Form field ref of " + ref);
if(ref != null) {
baseField = database.getField(ref);
if(ref.contains(".")) {
String[] split = ref.split("\\.", 2);
baseField = database.getTableField(split[0], split[1]);
} else {
baseField = database.getField(ref);
}

if(null == baseField) {
throw new H2ZeroParseException("A ref of '" + ref + "' was used, but not field could be found with this name.");
} else {
// we have the field and are good to go
formFields.add(new FormField(baseField));
formFields.add(new FormField(this.name, baseField));
}
} else {
//maybe we have a name???
Expand All @@ -89,7 +96,7 @@ private void populateFields(JSONObject jsonObject) throws H2ZeroParseException {
if(null == fieldName) {
throw new H2ZeroParseException("A form field must have a name.");
} else {
formFields.add(new FormField(fieldObject));
formFields.add(new FormField(this.name, fieldObject));
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/main/java/synapticloop/h2zero/model/form/FormField.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,28 @@
public class FormField {
private static final String SYNAPTICLOOP_H2ZERO_BASE_FORM_FIELD = "synapticloop.h2zero.base.form.field.";

private String formName;
private String name;
private boolean nullable;
private boolean confirm;
private int minLength;
private int maxLength;
private String validator;

public FormField(JSONObject jsonObject) throws H2ZeroParseException {
public FormField(String formName, JSONObject jsonObject) throws H2ZeroParseException {
this.formName = formName;
this.name = JsonHelper.getStringValue(jsonObject, "name", null);
this.nullable = JsonHelper.getBooleanValue(jsonObject, "nullable", false);
this.nullable = JsonHelper.getBooleanValue(jsonObject, "confirm", false);
this.minLength = JsonHelper.getIntValue(jsonObject, "minLength", 0);
this.maxLength = JsonHelper.getIntValue(jsonObject, "length", 0);
this.validator = JsonHelper.getStringValue(jsonObject, "validator", null);
System.out.println(jsonObject);
System.out.println(jsonObject.toString(2));
validateFormField();
}

public FormField(BaseField baseField) throws H2ZeroParseException {
public FormField(String formName, BaseField baseField) throws H2ZeroParseException {
this.formName = formName;
this.name = baseField.getName();
this.nullable = baseField.getNullable();
this.minLength = baseField.getMinLength();
Expand All @@ -65,7 +68,7 @@ private void validateFormField() throws H2ZeroParseException {
}

if (!this.nullable && this.minLength == 0) {
throw new H2ZeroParseException("The form field is not allowed to be null, but the minLength is 0.");
throw new H2ZeroParseException("The form field '" + name + "' on form '" + formName + "' is not allowed to be null, but the minLength is 0, either set a \"minlength\" on the form, or on the field.");
}

if (null == validator) {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/synapticloop/h2zero/util/KeyHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package synapticloop.h2zero.util;

import java.util.Iterator;
import java.util.Set;

import org.json.JSONObject;

import synapticloop.h2zero.model.BaseSchemaObject;
import synapticloop.h2zero.util.SimpleLogger.LoggerType;

public class KeyHelper {

public static void findMissingKeys(BaseSchemaObject baseSchemaObject, JSONObject jsonObject, Set<String> allowableKeys) {
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if(!allowableKeys.contains(key)) {
SimpleLogger.logWarn(LoggerType.PARSE, baseSchemaObject.getClass(), "Found a key with name '" + key + "', which is not utilised.");
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package synapticloop.h2zero.validator.field;

import java.util.List;

import synapticloop.h2zero.model.Database;
import synapticloop.h2zero.model.Options;
import synapticloop.h2zero.model.Table;
import synapticloop.h2zero.model.field.BaseField;
import synapticloop.h2zero.validator.BaseValidator;

/**
* This validator checks to see whether there are some fields which are marked
* as nullable, however do not have a minimum length set on them
*
* @author synapticloop
*
*/
public class FieldNotNullLengthValidator extends BaseValidator {

@Override
public void validate(Database database, Options options) {
List<Table> tables = database.getTables();
for (Table table : tables) {
List<BaseField> fields = table.getFields();
for (BaseField baseField : fields) {
if(!baseField.getNullable() && baseField.getMinLength() == 0 && "String".equals(baseField.getJavaType())) {
addWarnMessage("Table field '" + table.getName() + "." + baseField.getName() + "' is not allowed to be null, but has a minimum length of 0");
}
}
}
}

}
8 changes: 2 additions & 6 deletions src/test/resources/sample.h2zero
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
{ "name": "fl_is_alive", "type": "boolean", "nullable": true, "default": "'0'", "index": true },
{ "name": "num_age", "type": "int", "nullable": false },
{ "name": "nm_username", "type": "varchar", "length": "64", "nullable": false, "unique": true },
{ "name": "txt_address_email", "type": "varchar", "length": "256", "nullable": false, "unique": true },
{ "name": "txt_password", "type": "varchar", "length": "32", "nullable": false, "unique": false },
{ "name": "txt_address_email", "type": "varchar", "length": "256", "minLength": 6, "nullable": false, "unique": true },
{ "name": "txt_password", "type": "varchar", "length": "32", "minLength": 8, "nullable": false, "unique": false },
{ "name": "dtm_signup", "type": "datetime", "nullable": true }
],

Expand Down Expand Up @@ -228,9 +228,5 @@
]
}
],

"forms": [
]

}
}

0 comments on commit f19ad9d

Please sign in to comment.