io.github.livr.Validator Maven / Gradle / Ivy
package io.github.livr;
import com.google.common.collect.Lists;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
/**
* Created by vladislavbaluk on 9/29/2017.
*/
public class Validator {
Map DEFAULT_RULES = new HashMap<>();
JSONObject livrRules = new JSONObject();
Map> validators;
Map validatorBuilders;
JSONObject errors;
boolean isPrepared = false;
boolean isAutoTrim = false;
private JSONParser parser = new JSONParser();
public Validator(Map defaultRules) {
DEFAULT_RULES = defaultRules;
}
public Validator init(JSONObject livrRules, boolean isAutoTrim) {
this.isPrepared = false;
this.livrRules = livrRules;
this.validators = new HashMap<>();
this.validatorBuilders = new HashMap<>();
this.errors = null;
this.isAutoTrim = isAutoTrim;
this.registerRules(DEFAULT_RULES);
return this;
}
public Validator init(String livrRules, boolean isAutoTrim) throws ParseException {
this.isPrepared = false;
this.livrRules = (JSONObject) parser.parse(livrRules);
this.validators = new HashMap<>();
this.validatorBuilders = new HashMap<>();
this.errors = null;
this.isAutoTrim = isAutoTrim;
this.registerRules(DEFAULT_RULES);
return this;
}
public Validator() {
}
public Validator registerDefaultRules(Map rules) {
DEFAULT_RULES.putAll(rules);
return this;
}
public Map getDefaultRules() {
return DEFAULT_RULES;
}
public void registerAliasedDefaultRule(JSONObject alias) throws IOException {
if (((String) alias.get("name")).isEmpty()) throw new IOException("Alias name required");
String name = (String) alias.get("name");
DEFAULT_RULES.put(name, _buildAliasedRule(alias));
}
Function _buildAliasedRule(JSONObject alias) throws IOException {
if (((String) alias.get("name")).isEmpty()) throw new IOException("Alias name required");
if (alias.get("rules") == null) throw new IOException("Alias rules required");
JSONObject livr = new JSONObject();
Object rules = alias.get("rules");
livr.put("value", rules);
Function, Function> aliasFunction = objects -> {
Map ruleBuilders = (Map) ((objects.size() > 1) ? objects.get(1) : objects.get(0));
try {
Validator validator = new Validator(ruleBuilders).init(livr, false).prepare();
return (Function) (wrapper) -> {
try {
JSONObject json = new JSONObject();
json.put("value", wrapper.getValue());
JSONObject result = validator.validate(json);
if (result != null) {
wrapper.getFieldResultArr().add(result.get("value"));
return "";
} else {
return alias.get("error") == null ? validator.getErrors().get("value") : alias.get("error");
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
};
} catch (IOException e) {
e.printStackTrace();
}
return null;
};
return aliasFunction;
}
public Validator prepare() throws IOException {
JSONObject allRules = this.livrRules;
for (String field : (Set) allRules.keySet()) {
Object fieldRules = allRules.get(field);
JSONArray rules1 = null;
if (fieldRules.getClass() == JSONArray.class) {
rules1 = (JSONArray) fieldRules;
}
if (fieldRules.getClass() == JSONObject.class) {
rules1 = new JSONArray();
rules1.add(fieldRules);
}
if (fieldRules.getClass() == String.class) {
rules1 = new JSONArray();
JSONObject json = new JSONObject();
json.put(fieldRules, new JSONArray());
rules1.add(json);
}
if (rules1 == null)
return this;
List validators = new ArrayList<>();
for (int i = 0; i < rules1.size(); i++) {
Map parsed = this._parseRule(rules1.get(i));
validators.add(this._buildValidator((String) parsed.get("name"), (List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy