use of org.apache.solr.common.util.ValidatingJsonMap in project lucene-solr by apache.
the class Api method getCommandSchema.
/**This method helps to cache the schema validator object
*/
public Map<String, JsonSchemaValidator> getCommandSchema() {
if (commandSchema == null) {
synchronized (this) {
if (commandSchema == null) {
ValidatingJsonMap commands = getSpec().getMap("commands", null);
commandSchema = commands != null ? ImmutableMap.copyOf(ApiBag.getParsedSchema(commands)) : ImmutableMap.of();
}
}
}
return commandSchema;
}
use of org.apache.solr.common.util.ValidatingJsonMap in project lucene-solr by apache.
the class ApiBag method verifyCommands.
private void verifyCommands(ValidatingJsonMap spec) {
ValidatingJsonMap commands = spec.getMap("commands", null);
if (commands == null)
return;
getParsedSchema(commands);
}
use of org.apache.solr.common.util.ValidatingJsonMap in project lucene-solr by apache.
the class JsonValidatorTest method checkSchema.
private void checkSchema(String name) {
ValidatingJsonMap spec = ApiBag.getSpec(name).getSpec();
Map commands = (Map) spec.get("commands");
for (Object o : commands.entrySet()) {
Map.Entry cmd = (Map.Entry) o;
try {
JsonSchemaValidator validator = new JsonSchemaValidator((Map) cmd.getValue());
} catch (Exception e) {
throw new RuntimeException("Error in command " + cmd.getKey() + " in schema " + name, e);
}
}
}
use of org.apache.solr.common.util.ValidatingJsonMap in project lucene-solr by apache.
the class JsonValidatorTest method testSchemaValidation.
public void testSchemaValidation() {
ValidatingJsonMap spec = ApiBag.getSpec("collections.Commands").getSpec();
Map createSchema = spec.getMap("commands", NOT_NULL).getMap("create-alias", NOT_NULL);
JsonSchemaValidator validator = new JsonSchemaValidator(createSchema);
List<String> errs = validator.validateJson(Utils.fromJSONString("{name : x, collections: [ c1 , c2]}"));
assertNull(toJSONString(errs), errs);
errs = validator.validateJson(Utils.fromJSONString("{name : x, collections: [c1] }"));
assertNull(toJSONString(errs), errs);
errs = validator.validateJson(Utils.fromJSONString("{name : x, x:y, collections: [ c1 , c2]}"));
assertNotNull(toJSONString(errs), errs);
assertTrue(toJSONString(errs), errs.get(0).contains("Unknown"));
errs = validator.validateJson(Utils.fromJSONString("{name : 123, collections: c1 }"));
assertNotNull(toJSONString(errs), errs);
assertTrue(toJSONString(errs), errs.get(0).contains("expected"));
errs = validator.validateJson(Utils.fromJSONString("{x:y, collections: [ c1 , c2]}"));
assertTrue(toJSONString(errs), StrUtils.join(errs, '|').contains("Unknown"));
errs = validator.validateJson(Utils.fromJSONString("{name : x, collections: [ 1 , 2]}"));
assertFalse(toJSONString(errs), errs.isEmpty());
assertTrue(toJSONString(errs), errs.get(0).contains("expected"));
validator = new JsonSchemaValidator("{" + " type:object," + " properties: {" + " age : {type: number}," + " adult : {type: boolean}," + " name: {type: string}}}");
errs = validator.validateJson(Utils.fromJSONString("{name:x, age:21, adult:true}"));
assertNull(errs);
errs = validator.validateJson(Utils.fromJSONString("{name:x, age:'21', adult:'true'}"));
assertNotNull(errs);
errs = validator.validateJson(Utils.fromJSONString("{name:x, age:'x21', adult:'true'}"));
assertEquals(1, errs.size());
try {
validator = new JsonSchemaValidator("{" + " type:object," + " properties: {" + " age : {type: int}," + " adult : {type: Boolean}," + " name: {type: string}}}");
fail("should have failed");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Unknown type"));
}
try {
new JsonSchemaValidator("{" + " type:object," + " x : y," + " properties: {" + " age : {type: number}," + " adult : {type: boolean}," + " name: {type: string}}}");
fail("should have failed");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Unknown key"));
}
try {
new JsonSchemaValidator("{" + " type:object," + " propertes: {" + " age : {type: number}," + " adult : {type: boolean}," + " name: {type: string}}}");
fail("should have failed");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Unknown key : propertes"));
}
validator = new JsonSchemaValidator("{" + " type:object," + " properties: {" + " age : {type: number}," + " sex: {type: string, enum:[M, F]}," + " adult : {type: boolean}," + " name: {type: string}}}");
errs = validator.validateJson(Utils.fromJSONString("{name: 'Joe Average' , sex:M}"));
assertNull("errs are " + errs, errs);
errs = validator.validateJson(Utils.fromJSONString("{name: 'Joe Average' , sex:m}"));
assertEquals(1, errs.size());
assertTrue(errs.get(0).contains("Value of enum"));
String schema = "{\n" + " 'type': 'object',\n" + " 'properties': {\n" + " 'links': {\n" + " 'type': 'array',\n" + " 'items':{" + " 'type': 'object',\n" + " 'properties': {\n" + " 'rel': {\n" + " 'type': 'string'\n" + " },\n" + " 'href': {\n" + " 'type': 'string'\n" + " }\n" + " }\n" + " }\n" + " }\n" + "\n" + " }\n" + "}";
validator = new JsonSchemaValidator(schema);
validator.validateJson(Utils.fromJSONString("{\n" + " 'links': [\n" + " {\n" + " 'rel': 'x',\n" + " 'href': 'x'\n" + " },\n" + " {\n" + " 'rel': 'x',\n" + " 'href': 'x'\n" + " },\n" + " {\n" + " 'rel': 'x',\n" + " 'href': 'x'\n" + " }\n" + " ]\n" + "}"));
schema = "{\n" + "'type' : 'object',\n" + "'oneOf' : ['a', 'b']\n" + "}";
validator = new JsonSchemaValidator(schema);
errs = validator.validateJson(Utils.fromJSONString("" + "{'c':'val'}"));
assertNotNull(errs);
errs = validator.validateJson(Utils.fromJSONString("" + "{'a':'val'}"));
assertNull(errs);
}
use of org.apache.solr.common.util.ValidatingJsonMap in project lucene-solr by apache.
the class ApiBag method validateAndRegister.
private void validateAndRegister(Api api, Map<String, String> nameSubstitutes) {
ValidatingJsonMap spec = api.getSpec();
Api introspect = new IntrospectApi(api, isCoreSpecific);
List<String> methods = spec.getList("methods", ENUM_OF, SUPPORTED_METHODS);
for (String method : methods) {
PathTrie<Api> registry = apis.get(method);
if (registry == null)
apis.put(method, registry = new PathTrie<>(ImmutableSet.of("_introspect")));
ValidatingJsonMap url = spec.getMap("url", NOT_NULL);
ValidatingJsonMap params = url.getMap("params", null);
if (params != null) {
for (Object o : params.keySet()) {
ValidatingJsonMap param = params.getMap(o.toString(), NOT_NULL);
param.get("type", ENUM_OF, KNOWN_TYPES);
}
}
List<String> paths = url.getList("paths", NOT_NULL);
ValidatingJsonMap parts = url.getMap("parts", null);
if (parts != null) {
Set<String> wildCardNames = getWildCardNames(paths);
for (Object o : parts.keySet()) {
if (!wildCardNames.contains(o.toString()))
throw new RuntimeException("" + o + " is not a valid part name");
ValidatingJsonMap pathMeta = parts.getMap(o.toString(), NOT_NULL);
pathMeta.get("type", ENUM_OF, ImmutableSet.of("enum", "string", "int", "number", "boolean"));
}
}
verifyCommands(api.getSpec());
for (String path : paths) {
registry.insert(path, nameSubstitutes, api);
registerIntrospect(nameSubstitutes, registry, path, introspect);
}
}
}
Aggregations