-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
298 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"/applicant-details/applicant-1/address-history" : "init", | ||
"/applicant-details/applicant-2/address-history" : "deleted", | ||
"Regex for value": "A", | ||
"Regex for value": "a", | ||
"SimpleEmail" : "[email protected]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"field1" : "aaa", | ||
"field2" : "null" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
"type": "object", | ||
"namespace": "simple", | ||
"name": "test5", | ||
"doc": "validation schema", | ||
"fields" : [ | ||
{ "name": "field1", "type": "String", "rule": "$EQUAL$abc"}, | ||
{ "name": "field2", "type": "String", "rule": "$NOT_EQUAL$null"} | ||
] | ||
} | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"field1" : "aaa" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
"type": "object", | ||
"namespace": "simple", | ||
"name": "test6", | ||
"doc": "validation schema", | ||
"fields" : [ | ||
{ "name": "field1", "type": "String"}, | ||
{ "name": "field2", "type": "String"} | ||
] | ||
} | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.github.sijoonlee; | ||
|
||
import com.github.sijoonlee.util.JsonUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
|
||
import java.util.ArrayList; | ||
|
||
|
||
/* | ||
* This Class is to modify the existing schema, that is loaded from a file | ||
* The following operations are supported | ||
* - Set a field as required | ||
* - Set a field value rule | ||
* */ | ||
|
||
public class SchemaEditor { | ||
private ArrayList<SchemaRecord> schemas; | ||
private static final Logger log = LoggerFactory.getLogger(SchemaEditor.class.getName()); | ||
|
||
public SchemaEditor(String schemaPath) { | ||
schemas = new ArrayList<>(); | ||
JsonArray jsonArray = JsonUtil.convertJsonFileToJsonArray(schemaPath); // this is because Schema's root element is always Json array | ||
Gson gson = new Gson(); | ||
for (JsonElement elm : jsonArray) { | ||
// convert each item in array into SchemaRecord instance and push it to ArrayList | ||
schemas.add(gson.fromJson(elm, SchemaRecord.class)); | ||
} | ||
} | ||
|
||
public void setFieldRuleNotEqualTo(String recordFullName, String fieldName, String value) { | ||
boolean found = false; | ||
for(SchemaRecord record : schemas){ | ||
if (record.getFullNamePath().equals(recordFullName)){ | ||
if(record.setFieldRuleAs(fieldName, "$NOT_EQUAL$"+value)){ | ||
found = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (found) { | ||
log.info(String.format("The field %s's rule is set", fieldName)); | ||
} else { | ||
log.info(String.format("The field %s is not there", fieldName)); | ||
} | ||
} | ||
|
||
public void setFieldRuleEqualTo(String recordFullName, String fieldName, String value) { | ||
boolean found = false; | ||
for(SchemaRecord record : schemas){ | ||
if (record.getFullNamePath().equals(recordFullName)){ | ||
if(record.setFieldRuleAs(fieldName, "$EQUAL$"+value)){ | ||
found = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (found) { | ||
log.info(String.format("The field %s's rule is set", fieldName)); | ||
} else { | ||
log.info(String.format("The field %s is not there", fieldName)); | ||
} | ||
} | ||
|
||
public void setFieldRequired(String recordFullName, String fieldName) { | ||
boolean found = false; | ||
for(SchemaRecord record : schemas){ | ||
if (record.getFullNamePath().equals(recordFullName)){ | ||
if(record.setFieldRequiredAs(fieldName, true)){ | ||
found = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (found) { | ||
log.info(String.format("The field %s is set to be required", fieldName)); | ||
} else { | ||
log.info(String.format("The field %s is not there", fieldName)); | ||
} | ||
} | ||
|
||
public ArrayList<SchemaRecord> getSchemas() { | ||
return schemas; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.