本文整理匯總了Java中com.github.fge.jsonschema.main.JsonSchemaFactory類的典型用法代碼示例。如果您正苦於以下問題:Java JsonSchemaFactory類的具體用法?Java JsonSchemaFactory怎麽用?Java JsonSchemaFactory使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JsonSchemaFactory類屬於com.github.fge.jsonschema.main包,在下文中一共展示了JsonSchemaFactory類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: validate
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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: checkSchema
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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();
}
}
示例3: isValid
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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();
}
示例4: isValid
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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;
}
}
示例5: testDefaultConfig
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的package包/類
@Test
public final void testDefaultConfig()
throws Exception {
final ObjectMapper m = new ObjectMapper()
.configure(JsonParser.Feature.ALLOW_COMMENTS, true)
.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true);
final JsonNode jsonInput = m.readTree(
Paths.get(PathUtil.getBaseDir(), "config", "defaults.json").toFile()
);
final JsonNode jsonSchema = m.readTree(
Paths.get(PathUtil.getBaseDir(), "config", "config-schema.json").toFile()
);
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonValidator validator = factory.getValidator();
final ProcessingReport report = validator.validate(jsonSchema, jsonInput);
assertTrue(report.toString(), report.isSuccess());
}
示例6: validateSwaggerSpec
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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: execute
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的package包/類
@Override
protected ExecuteResult execute(final WebServiceConnector connector) {
final String body = connector.getBody();
final ObjectMapper mapper = new ObjectMapper();
try {
final JsonNode obj = mapper.readTree(body);
final JsonNode schema = mapper.readTree(this.schema);
final ProcessingReport report = JsonSchemaFactory.byDefault()
.getJsonSchema(schema)
.validate(obj);
return report.isSuccess() ? getSuccessOutput() : getFailedOutput();
} catch (IOException | ProcessingException e) {
return getFailedOutput();
}
}
示例8: validate
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的package包/類
public ValidationErrors validate(String schema, String requestBody) {
ValidationErrors errors = new ValidationErrors();
try {
JsonNode schemaNode = JsonLoader.fromString(schema);
JsonNode dataNode = JsonLoader.fromString(requestBody);
ProcessingReport report = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode).validate(dataNode);
if (!report.isSuccess())
report.iterator().forEachRemaining(m -> errors.addMessage("[ %s ] [ body ] %s", bodyType, m.getMessage()));
} catch (Exception e) {
errors.addMessage("[ %s ] [ body ] Unexpected error of [ %s ] while validating JSON content", bodyType, e.getMessage());
}
return errors;
}
示例9: _validate
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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);
}
}
示例10: validateSchema
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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);
}
}
示例11: loadSchema
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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;
}
示例12: getSchema
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的package包/類
@Override
public CJsonSchema getSchema(final IActivityLogger aLogger,
final JSONObject aSchema) throws SchemaException {
// create schema
try {
JsonSchemaFactory wFactory = JsonSchemaFactory.byDefault();
aLogger.logDebug(this, "getSchema",
"create JsonNode fro JSONObject for the schema");
ObjectMapper wMapper = new ObjectMapper();
JsonNode wSchema = wMapper.readTree(aSchema.toString());
aLogger.logDebug(this, "getSchema", "create JsonSchema");
return new CJsonSchema(wFactory.getJsonSchema(wSchema), aSchema);
} catch (Exception e) {
throw new SchemaException(e, e.getMessage());
}
}
示例13: GenericOpenRtbValidator
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的package包/類
/**
* Constructs a JSON validator using the given schema read as a resource.
*
* @param schemaResource
* the schema resource
*/
GenericOpenRtbValidator(String schemaResource) {
try {
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonNode jsonNode = JsonLoader.fromResource(schemaResource);
schema = factory.getJsonSchema(jsonNode);
} catch (IOException | ProcessingException e) {
throw new IllegalStateException("could not initialize validator due to previous exception", e);
}
}
示例14: validateExtensionTest
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的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());
}
示例15: checkJsonSchema
import com.github.fge.jsonschema.main.JsonSchemaFactory; //導入依賴的package包/類
@Test
public void checkJsonSchema() throws Exception {
File file = new File(JSON_SCHEMA_FILE_PATH);
System.out.println("Validating syntax of " + file.getAbsolutePath());
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(file);
SyntaxValidator syntaxValidator = JsonSchemaFactory.byDefault().getSyntaxValidator();
ProcessingReport report = syntaxValidator.validateSchema(jsonNode);
System.out.println(report);
Assert.assertTrue(syntaxValidator.schemaIsValid(jsonNode));
}