use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class ArrayValidator method validate.
@Override
public Object validate(Api api, ApiConsumer consumer, ApiRequest request, DefaultApiServiceValidator validator, String name, String label, JsonObject spec, Object value) {
JsonObject message = isRequired(validator, api, request.getLang(), label, spec, value);
if (message != null) {
return message;
}
if (value == null) {
return null;
}
boolean updateRequest = false;
JsonArray array = null;
if (value instanceof JsonArray) {
array = (JsonArray) value;
} else {
try {
array = new JsonArray(String.valueOf(value));
} catch (JsonException e) {
return ValidationUtils.feedback(null, spec, Spec.Type, e.getMessage());
}
updateRequest = true;
}
if (array.isEmpty()) {
return ValidationUtils.feedback(null, spec, null, validator.getMessage(api, request.getLang(), RequiredMessage, label));
}
String sType = Json.getString(spec, Spec.SType, MapValidator.Type);
TypeValidator tValidator = validator.getValidator(sType);
for (int i = 0; i < array.count(); i++) {
Object feedback = tValidator.validate(api, consumer, request, validator, name, label + "->index " + i, spec, array.get(i));
if (feedback != null) {
return feedback;
}
}
if (updateRequest) {
request.set(name, array);
}
return null;
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class StringValidator method validate.
@Override
public Object validate(Api api, ApiConsumer consumer, ApiRequest request, DefaultApiServiceValidator validator, String name, String label, JsonObject spec, Object value) {
Object message = isRequired(validator, api, request.getLang(), label, spec, value);
if (message != null) {
return message;
}
if (value == null) {
return null;
}
JsonObject feedback = null;
String sValue = String.valueOf(value);
String displayValue = Json.getBoolean(spec, Spec.Secret, false) ? SecretValue : sValue;
// validate length
int min = Json.getInteger(spec, Spec.Min, 1);
if (sValue.length() < min) {
feedback = ValidationUtils.feedback(feedback, spec, Spec.Min, validator.getMessage(api, request.getLang(), MinMessage, label, String.valueOf(min), displayValue));
}
int max = Json.getInteger(spec, Spec.Max, Integer.MAX_VALUE);
if (sValue.length() > max) {
feedback = ValidationUtils.feedback(feedback, spec, Spec.Max, validator.getMessage(api, request.getLang(), MaxMessage, label, String.valueOf(max), displayValue));
}
if (feedback != null) {
return feedback;
}
// validate vType
String vType = Json.getString(spec, Spec.VType);
if (!Lang.isNullOrEmpty(vType)) {
TypeValidator vTypeValiator = validator.getVType(vType);
if (vTypeValiator != null) {
message = vTypeValiator.validate(api, consumer, request, validator, name, label, spec, sValue);
if (message != null) {
return message;
}
}
}
JsonArray lov = Json.getArray(spec, Spec.ListOfValues);
if (lov == null) {
return null;
}
if (!lov.contains(sValue)) {
return ValidationUtils.feedback(feedback, spec, Spec.ListOfValues, validator.getMessage(api, request.getLang(), LovMessage, label, lov.join(Lang.COMMA), displayValue));
}
return null;
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class BlueNimble method loadVariables.
private void loadVariables() {
JsonObject oVars = Json.getObject(Config, CliSpec.Config.Variables);
if (oVars == null) {
oVars = new JsonObject();
Config.set(CliSpec.Config.Variables, oVars);
}
if (!oVars.containsKey(DefaultVars.StorageProvider)) {
oVars.set(DefaultVars.StorageProvider, "default");
}
if (!oVars.containsKey(DefaultVars.DatabaseProvider)) {
oVars.set(DefaultVars.DatabaseProvider, "default");
}
if (!oVars.containsKey(DefaultVars.CacheProvider)) {
oVars.set(DefaultVars.CacheProvider, "default");
}
if (!oVars.containsKey(DefaultVars.QueryAll)) {
oVars.set(DefaultVars.QueryAll, new JsonObject().set("where", new JsonObject()));
}
if (!oVars.containsKey(DefaultVars.QueryCount)) {
oVars.set(DefaultVars.QueryCount, new JsonObject().set("where", new JsonObject()).set("select", new JsonArray().set(null, "count(1)")));
}
if (!oVars.containsKey(DefaultVars.TemplateServices)) {
oVars.set(DefaultVars.TemplateServices, "database/javascript");
}
if (!oVars.containsKey(DefaultVars.TemplateApi)) {
oVars.set(DefaultVars.TemplateApi, "database/javascript");
}
if (!oVars.containsKey(DefaultVars.SpecLanguage)) {
oVars.set(DefaultVars.SpecLanguage, SpecLangs.Json);
}
if (oVars.containsKey(DefaultVars.Paraphrase)) {
try {
super.setParaphrase(Json.getString(oVars, DefaultVars.Paraphrase), false);
} catch (Exception e) {
printer().error("Can't read user paraphrase. Cause: " + e.getMessage());
}
}
if (!oVars.containsKey(DefaultVars.SslTrust)) {
oVars.set(DefaultVars.SslTrust, "true");
}
if (!oVars.containsKey(DefaultVars.ApiSecurityEnabled)) {
oVars.set(DefaultVars.ApiSecurityEnabled, "true");
}
if (!oVars.containsKey(DefaultVars.RemoteHeadersAccept)) {
oVars.set(DefaultVars.RemoteHeadersAccept, ApiContentTypes.Json);
}
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
for (Object key : oVars.keySet()) {
vars.put(String.valueOf(key), oVars.get(key));
}
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class JsonDeleteHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("json variable name required");
}
if (args.length < 2) {
throw new CommandExecutionException("json property required");
}
String var = args[0];
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
String prop = args[1];
int indexOfDot = prop.indexOf(Lang.DOT);
Object o = vars.get(var);
if (o == null) {
throw new CommandExecutionException("variable '" + var + "' not found");
}
if (!(o instanceof JsonObject) && !(o instanceof JsonArray)) {
throw new CommandExecutionException("variable '" + var + "' isn't a valid json object or array");
}
if ((o instanceof JsonArray) && indexOfDot > 0) {
throw new CommandExecutionException("property '" + prop + "' should be a valid integer since the json variable is an array");
}
if (indexOfDot <= 0) {
if (o instanceof JsonObject) {
((JsonObject) o).remove(prop);
} else {
((JsonArray) o).remove(Integer.parseInt(prop));
}
return new DefaultCommandResult(CommandResult.OK, o);
}
JsonObject json = (JsonObject) o;
String[] path = Lang.split(prop, Lang.DOT);
prop = path[path.length - 1];
path = Lang.moveRight(path, 1);
Object child = Json.find(json, path);
if (child == null) {
throw new CommandExecutionException(Lang.join(path, Lang.DOT) + " not found");
}
if (child instanceof JsonObject) {
((JsonObject) child).remove(prop);
return new DefaultCommandResult(CommandResult.OK, json);
} else if (child instanceof JsonArray) {
int iProp = -1;
try {
iProp = Integer.valueOf(prop);
} catch (Exception ex) {
// ignore
}
JsonArray array = (JsonArray) child;
if (iProp > -1 && array.count() > iProp) {
((JsonArray) child).remove(iProp);
return new DefaultCommandResult(CommandResult.OK, json);
}
}
return new DefaultCommandResult(CommandResult.OK, json);
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class JsonJoinHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("json variable name required");
}
String sep = Lang.SPACE;
if (args.length > 1) {
sep = args[1];
}
String var = args[0];
String prop = null;
int indexOfSlash = var.indexOf(Lang.SLASH);
if (indexOfSlash > 0) {
prop = var.substring(indexOfSlash + 1);
var = var.substring(0, indexOfSlash);
}
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
Object o = vars.get(var);
if (o == null) {
throw new CommandExecutionException("variable '" + var + "' not found");
}
if (!(o instanceof JsonObject)) {
throw new CommandExecutionException("variable '" + var + "' isn't a valid json object");
}
JsonObject json = (JsonObject) o;
Object a = json.find(prop, Lang.SLASH);
if (!(a instanceof JsonArray)) {
throw new CommandExecutionException("property '" + prop + "' isn't a valid array");
}
JsonArray array = (JsonArray) a;
try {
return new DefaultCommandResult(CommandResult.OK, join(array, sep));
} catch (JsonException e) {
throw new CommandExecutionException(e.getMessage(), e);
}
}
Aggregations