本文整理汇总了Java中com.github.fge.jsonschema.main.JsonSchema类的典型用法代码示例。如果您正苦于以下问题:Java JsonSchema类的具体用法?Java JsonSchema怎么用?Java JsonSchema使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonSchema类属于com.github.fge.jsonschema.main包,在下文中一共展示了JsonSchema类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validate
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
@SneakyThrows
private boolean validate(Map<String, Object> data, String jsonSchema) {
String stringData = objectMapper.writeValueAsString(data);
log.debug("Validation data. map: {}, jsonData: {}", data, stringData);
JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
JsonNode dataNode = JsonLoader.fromString(stringData);
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonSchema schema = factory.getJsonSchema(schemaNode);
val report = schema.validate(dataNode);
boolean isSuccess = report.isSuccess();
if (!isSuccess) {
log.info("Validation data report: {}", report.toString().replaceAll("\n", " | "));
}
return isSuccess;
}
示例2: swaggerDefinitions
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
private SwaggerDefinitions swaggerDefinitions(Definitions definitions)
{
SwaggerDefinitions result = new SwaggerDefinitions();
for (TypeDefinition typeDefinition : definitions)
{
com.mauriciotogneri.jsonschema.JsonSchema jsonSchema = new com.mauriciotogneri.jsonschema.JsonSchema(typeDefinition);
JsonObject schema = jsonSchema.schema();
JsonObject schemaDefinitions = schema.get("definitions").getAsJsonObject();
for (Entry<String, JsonElement> entry : schemaDefinitions.entrySet())
{
result.put(entry.getKey(), entry.getValue().getAsJsonObject());
}
}
return result;
}
示例3: checkSchema
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
private void checkSchema(File output)
{
try
{
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
URL schemaPath = getClass().getResource("/swagger-schema.json");
JsonSchema schema = factory.getJsonSchema(schemaPath.toString());
JsonNode json = JsonLoader.fromFile(output);
ProcessingReport report = schema.validate(json);
System.out.println(report);
}
catch (Exception e)
{
e.printStackTrace();
}
}
示例4: isValid
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
public static boolean isValid(String json, String schema) throws Exception {
JsonNode schemaNode = JsonLoader.fromString(schema);
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonSchema jsonSchema = factory.getJsonSchema(schemaNode);
JsonNode jsonNode = JsonLoader.fromString(json);
ProcessingReport report = jsonSchema.validate(jsonNode);
logger.debug("report: {}", report);
return report.isSuccess();
}
示例5: isValid
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
public boolean isValid(JsonNode jsonNode) {
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
log.verboseOutput("Checking against schema. Available at 'https://raw.githubusercontent.com/pivio/pivio-client/master/src/main/resources/pivio-schema.json'.", configuration.isVerbose());
try {
String schemaContent = readFromJARFile("/pivio-schema.json");
JsonNode schemaNode = new ObjectMapper().readTree(schemaContent);
final JsonSchema jsonSchema = factory.getJsonSchema(schemaNode);
ProcessingReport processingReport = jsonSchema.validate(jsonNode);
for (ProcessingMessage processingMessage : processingReport) {
String pointer = processingMessage.asJson().get("instance").get("pointer").asText();
log.output(pointer + " : " + processingMessage.getMessage());
}
return processingReport.isSuccess();
} catch (ProcessingException | IOException e ) {
log.output("Error processing Json. "+e.getMessage());
return false;
}
}
示例6: validateSwaggerSpec
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
/**
* Validates the input Swagger JsonNode against Swagger Specification schema.
*
* @throws OpenApiConversionException
*/
private static void validateSwaggerSpec(JsonNode swaggerJsonNode)
throws OpenApiConversionException {
ProcessingReport report = null;
try {
URL url = Resources.getResource(SCHEMA_RESOURCE_PATH);
String swaggerSchema = Resources.toString(url, StandardCharsets.UTF_8);
JsonNode schemaNode = Yaml.mapper().readTree(swaggerSchema);
JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode);
report = schema.validate(swaggerJsonNode);
} catch (Exception ex) {
throw new OpenApiConversionException("Unable to parse the content. " + ex.getMessage(), ex);
}
if (!report.isSuccess()) {
String message = "";
Iterator itr = report.iterator();
if (itr.hasNext()) {
message += ((ProcessingMessage) itr.next()).toString();
}
while(itr.hasNext())
{
message += "," + ((ProcessingMessage) itr.next()).toString();
}
throw new OpenApiConversionException(
String.format("Invalid OpenAPI file. Please fix the schema errors:\n%s", message));
}
}
示例7: _validate
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
protected ProcessingReport _validate(JsonNode json) throws IOException {
final JsonNode belSchema = JsonLoader.fromResource("/bel-json-graph.schema.json");
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema;
try {
schema = factory.getJsonSchema(belSchema);
return schema.validate(json, true);
} catch (ProcessingException e) {
throw new IOException(e);
}
}
示例8: validateSchema
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
private JsonSchema validateSchema(JsonNode jsonSchema)
throws UnRAVLException {
try {
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(jsonSchema);
SyntaxValidator syntaxValidator = factory.getSyntaxValidator();
if (!syntaxValidator.schemaIsValid(jsonSchema)) {
throw new UnRAVLException("JSON schema is invalid");
}
ProcessingReport report = syntaxValidator
.validateSchema(jsonSchema);
boolean success = report.isSuccess();
if (!success) {
throw new UnRAVLAssertionException(report.toString());
}
return schema;
} catch (ProcessingException e) {
throw new UnRAVLException(e);
}
}
示例9: loadSchema
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
/**
* Load a schema from given resourceName.
*
* @param resourceName
* @return the schema
* @throws ProcessingException
* @throws IOException
*/
public static JsonSchema loadSchema(String resourceName) throws ProcessingException, IOException {
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
SyntaxValidator validator = factory.getSyntaxValidator();
JsonNode node = loadJsonNode(resourceName);
ProcessingReport report = validator.validateSchema(node);
if (!report.isSuccess()) {
throw Error.get(UtilConstants.ERR_JSON_SCHEMA_INVALID);
}
JsonSchema schema = factory.getJsonSchema("resource:/" + resourceName);
if (null == schema) {
throw Error.get(UtilConstants.ERR_JSON_SCHEMA_INVALID);
}
return schema;
}
示例10: jsonSchemaValidation
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
/**
* Validates input node against given schema. Returns NULL if no errors
* reported, else returns string representing violations.
*
* @param schema the json schema (see #loadSchema)
* @param node the json node to validate
* @return null if there are no errors, else string with all errors and
* warnings
* @throws ProcessingException
*/
public static String jsonSchemaValidation(JsonSchema schema, JsonNode node) throws ProcessingException, JsonProcessingException {
ProcessingReport report = schema.validate(node);
Iterator<ProcessingMessage> i = report.iterator();
StringBuilder buff = new StringBuilder();
while (!report.isSuccess() && i != null && i.hasNext()) {
ProcessingMessage pm = i.next();
// attempting to pretty print the json
ObjectMapper mapper = new ObjectMapper();
String prettyPrintJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pm.asJson());
buff.append(prettyPrintJson).append("\n\n");
}
return report.isSuccess() ? null : buff.toString();
}
示例11: generateBaseExtensionDefinition
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
@Test
@Ignore("Used to generate the initial extension definition")
public void generateBaseExtensionDefinition() throws Exception {
ObjectMapper mapper = Json.mapper();
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
com.fasterxml.jackson.module.jsonSchema.JsonSchema schema = schemaGen.generateSchema(Extension.class);
System.out.println(mapper.writeValueAsString(schema));
}
示例12: validateExtensionTest
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
@Test
public void validateExtensionTest() throws Exception {
ObjectMapper mapper = Json.mapper();
String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);
Extension extension = new Extension.Builder()
.extensionId("my-extension")
.name("Name")
.description("Description")
.uses(OptionalInt.empty())
.version("1.0.0")
.addAction(new ExtensionAction.Builder()
.id("action-1")
.name("action-1-name")
.description("Action 1 Description")
.actionType("extension")
.pattern(Action.Pattern.From)
.descriptor(new ExtensionDescriptor.Builder()
.entrypoint("direct:hello")
.kind(ExtensionAction.Kind.ENDPOINT)
.build())
.build())
.build();
JsonNode node = mapper.valueToTree(extension);
ProcessingReport report = schema.validate(node);
assertFalse(report.toString(), report.iterator().hasNext());
}
示例13: getSchema
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
private JsonSchema getSchema() throws IOException, ProcessingException {
String fileName = String.format("%s/conf/schemas/%s.json",
env.rootPath().getAbsolutePath(), configuration.schema());
JsonNode schemaNode = JsonLoader.fromFile(new File(fileName));
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
return factory.getJsonSchema(schemaNode);
}
示例14: getSchema
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
@Override
public JsonSchema getSchema(Class<?> type) throws SerializationException {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
this.getSchema(type, outputStream);
JsonNode jsonSchema = JsonLoader
.fromReader(new InputStreamReader(new ByteArrayInputStream(outputStream.toByteArray())));
return JsonSchemaFactory.byDefault().getJsonSchema(jsonSchema);
} catch (IOException | ProcessingException e) {
throw new SerializationException(e);
}
}
示例15: validate
import com.github.fge.jsonschema.main.JsonSchema; //导入依赖的package包/类
public static boolean validate(String data, ObjectMapper objectMapper, BenderSchema benderSchema)
throws ConfigurationException {
ProcessingReport report;
try {
/*
* Create object
*/
JsonNode node = objectMapper.readTree(data);
/*
* Create JSON schema
*/
JsonNode jsonSchema = benderSchema.getSchema();
/*
* Validate
*/
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(jsonSchema);
report = schema.validate(node);
} catch (IOException | ProcessingException ioe) {
throw new ConfigurationException("unable to validate config", ioe);
}
if (report.isSuccess()) {
return true;
} else {
throw new ConfigurationException("invalid config file",
report.iterator().next().asException());
}
}