Skip to content

Commit

Permalink
SchemaEditor added
Browse files Browse the repository at this point in the history
  • Loading branch information
sijoonlee committed Aug 28, 2020
1 parent 1b1bd01 commit a2bdb4f
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 65 deletions.
2 changes: 1 addition & 1 deletion example/example4/usingRegex.json
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]"
}
4 changes: 4 additions & 0 deletions example/example5/usingRule.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"field1" : "aaa",
"field2" : "null"
}
14 changes: 14 additions & 0 deletions example/example5/usingRule_schema.json
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"}
]
}
]


3 changes: 3 additions & 0 deletions example/example6/usingEditor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"field1" : "aaa"
}
14 changes: 14 additions & 0 deletions example/example6/usingEditor_schema.json
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"}
]
}
]


80 changes: 48 additions & 32 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
## Simple JSON Schema and its Validator
- How I structured JSON schema is affected by [Apache Avro Schema](https://avro.apache.org/)
- Not exactly same, though
- There is an existing draft of [JSON schema](https://tools.ietf.org/html/draft-zyp-json-schema-04)
- But, in my opinion, this is far from easy-to-use

## if you want to see examples first
- Please see **example** folder and **test** folder
## How to wrtie/use Schema

## Terminology
- There are two basic structures of Json
Expand Down Expand Up @@ -165,37 +158,59 @@
- use property "rule" and prefix "$REGEX$"
- use escape characters where they are needed
- example
```
[
{
"type": "object",
"namespace": "simple",
"name": "test4",
"doc": "validation schema",
"fields" : [
{ "name": "SimpleEmail", "type": "String", "rule": "$REGEX$\\S+@\\S+\\.\\S+"}
```
[
{
"type": "object",
"namespace": "simple",
"name": "test4",
"doc": "validation schema",
"fields" : [
{ "name": "SimpleEmail", "type": "String", "rule": "$REGEX$\\S+@\\S+\\.\\S+"}
]
}
]
}
]
```
## You can use 'equal' / 'not equal' rules for value check
- use prefix $EQUAL$ or $NOT_EQUAL$
- following letters will the value to be the condition
- Following example will
- examine if the "type" field has "Lead" value
- examine if the "key" field doesn't have "null" value
```
[
{
"type": "object",
"namespace": "schema.salesforce",
"name": "leadFromLead",
"doc": "schema for lead for salesforce",
"fields": [
{ "name": "type", "type": "String", "rule": "$EQUAL$Lead" }
{ "name": "key", "type": "String", "rule": "$NOT_EQUAL$null" }
]
}
]
```
## You can check if required fields exist
- use property "required" and set as "true"
- don't forget double quotes around true
- example
```
[
{
"type": "object",
"namespace": "simple",
"name": "test4",
"doc": "validation schema",
"fields" : [
{ "name": "$REGEX$\\/applicant-details\\/applicant-([0-9])+\\/address-history", "type": "String", "required": "true" }
```
[
{
"type": "object",
"namespace": "simple",
"name": "test4",
"doc": "validation schema",
"fields" : [
{ "name": "address-history", "type": "String", "required": "true" }
]
}
]
}
]
```
```
## How to use Schema Classes
1. Instantiate validator with schema json file
Expand All @@ -208,8 +223,9 @@
JsonElement json = [some process]
boolean isValid = schemaValidator.runWithJsonElement(json, "simple.test"); // the second argument 'simple.test' is the full name of the schema record of root element
```
3. or Run validator with Json file
3. Run validator with Json file
```
boolean isValid = schemaValidator.runWithJsonFile("example/simpleStructure.json", "simple.test");
```
4. Run editor to manipulate schema first and pass the edited schema to Validator
88 changes: 88 additions & 0 deletions src/main/java/com/github/sijoonlee/SchemaEditor.java
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;
}
}
26 changes: 25 additions & 1 deletion src/main/java/com/github/sijoonlee/SchemaRecord.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.sijoonlee;

import java.lang.reflect.Array;
import java.util.*;

/* Gson will populate data into this class from Schema json file
Expand Down Expand Up @@ -42,6 +41,7 @@ public String getDoc() {
public ArrayList<SchemaField> getFields() {
return fields;
}

public ArrayList<String> getRequiredFieldNames() {
ArrayList<String> requiredFields = new ArrayList<>();
for(SchemaField field : fields) {
Expand All @@ -52,6 +52,30 @@ public ArrayList<String> getRequiredFieldNames() {
return requiredFields;
}

public boolean setFieldRequiredAs(String fieldName, boolean value) {
boolean found = false;
for(SchemaField field : fields) {
if(field.name.equals(fieldName)){
field.required = String.valueOf(value);
found = true;
break;
}
}
return found;
}

public boolean setFieldRuleAs(String fieldName, String rule) {
boolean found = false;
for(SchemaField field : fields) {
if(field.name.equals(fieldName)){
field.rule = rule;
found = true;
break;
}
}
return found;
}

public String getType(){
return type;
}
Expand Down
Loading

0 comments on commit a2bdb4f

Please sign in to comment.