-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #457 from RepreZen/task/ZEN-4339
[ZEN-4339] Implement optional 2nd phase of validation with KZOP
- Loading branch information
Showing
28 changed files
with
548 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
com.reprezen.swagedit.core/src/com/reprezen/swagedit/core/schema/JsonSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...reprezen.swagedit.core/src/com/reprezen/swagedit/core/validation/JsonSchemaValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016 ModelSolv, Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* ModelSolv, Inc. - initial API and implementation and/or initial documentation | ||
*******************************************************************************/ | ||
package com.reprezen.swagedit.core.validation; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.dadacoalition.yedit.YEditLog; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.github.fge.jsonschema.core.exceptions.ProcessingException; | ||
import com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration; | ||
import com.github.fge.jsonschema.core.load.configuration.LoadingConfigurationBuilder; | ||
import com.github.fge.jsonschema.core.report.ProcessingReport; | ||
import com.github.fge.jsonschema.main.JsonSchema; | ||
import com.github.fge.jsonschema.main.JsonSchemaFactory; | ||
import com.google.common.collect.Sets; | ||
import com.reprezen.swagedit.core.Activator; | ||
import com.reprezen.swagedit.core.editor.JsonDocument; | ||
|
||
public abstract class JsonSchemaValidator { | ||
|
||
private final LoadingConfiguration loadingConfiguration; | ||
private final JsonSchemaFactory factory; | ||
private final JsonNode schema; | ||
|
||
public JsonSchemaValidator(JsonNode schema, Map<String, JsonNode> preloadSchemas) { | ||
this.schema = schema; | ||
this.loadingConfiguration = getLoadingConfiguration(preloadSchemas); | ||
this.factory = JsonSchemaFactory.newBuilder() // | ||
.setLoadingConfiguration(loadingConfiguration) // | ||
.freeze(); | ||
} | ||
|
||
private LoadingConfiguration getLoadingConfiguration(Map<String, JsonNode> preloadSchemas) { | ||
LoadingConfigurationBuilder loadingConfigurationBuilder = LoadingConfiguration.newBuilder(); | ||
for (String nextSchemaUri : preloadSchemas.keySet()) { | ||
loadingConfigurationBuilder.preloadSchema(nextSchemaUri, preloadSchemas.get(nextSchemaUri)); | ||
} | ||
return loadingConfigurationBuilder.freeze(); | ||
} | ||
|
||
public Set<SwaggerError> validate(JsonDocument document) { | ||
final ErrorProcessor processor = new ErrorProcessor(document.getYaml(), schema); | ||
final Set<SwaggerError> errors = Sets.newHashSet(); | ||
|
||
JsonSchema jsonSchema = null; | ||
try { | ||
jsonSchema = factory.getJsonSchema(schema); | ||
} catch (ProcessingException e) { | ||
YEditLog.logException(e); | ||
return errors; | ||
} | ||
|
||
try { | ||
errors.addAll(processor.processReport(jsonSchema.validate(document.asJson(), true))); | ||
} catch (ProcessingException e) { | ||
errors.addAll(processor.processMessage(e.getProcessingMessage())); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
public ProcessingReport validateSubSchema(JsonNode document, String schemaPointer) { | ||
JsonSchema jsonSchema = null; | ||
try { | ||
jsonSchema = factory.getJsonSchema(schema, schemaPointer); | ||
} catch (ProcessingException e) { | ||
Activator.getDefault().logError(e.getLocalizedMessage(), e); | ||
return null; | ||
} | ||
|
||
try { | ||
return jsonSchema.validate(document, true); | ||
} catch (ProcessingException e) { | ||
Activator.getDefault().logError(e.getLocalizedMessage(), e); | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.