Skip to content

Commit

Permalink
#14 deprecated functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mpostelnicu committed Sep 14, 2017
1 parent d27cacc commit 1d354d7
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jocds-cli/src/test/java/test/OcdsValidatorTestRelease.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class OcdsValidatorTestRelease {
@Test
public void testReleaseValidation() {

OcdsValidatorStringRequest request = new OcdsValidatorStringRequest(OcdsValidatorConstants.Versions.OCDS_1_1_0,
OcdsValidatorStringRequest request = new OcdsValidatorStringRequest(OcdsValidatorConstants.Versions.OCDS_1_1_1,
OcdsValidatorConstants.EXTENSIONS, OcdsValidatorConstants.Schemas.RELEASE);
request.setJson(getJsonFromResource("/full-release.json"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public static final class Versions {

}

public static final class CustomSchemaKeywords {
/**
* See http://standard.open-contracting.org/latest/en/schema/deprecation/
*/
public static final String DEPRECATED = "deprecated";
}

public static final class Schemas {
public static final String RELEASE = "release";
public static final String RELEASE_PACKAGE = "release-package";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jackson.NodeType;
import com.github.fge.jsonpatch.JsonPatchException;
import com.github.fge.jsonpatch.mergepatch.JsonMergePatch;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ListProcessingReport;
import com.github.fge.jsonschema.core.report.ListReportProvider;
import com.github.fge.jsonschema.core.report.LogLevel;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.library.DraftV4Library;
import com.github.fge.jsonschema.library.Keyword;
import com.github.fge.jsonschema.library.Library;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import com.github.fge.msgsimple.source.MapMessageSource;
import com.github.fge.msgsimple.source.MessageSource;
import com.vdurmont.semver4j.Requirement;
import com.vdurmont.semver4j.Semver;
import java.io.IOException;
Expand All @@ -27,6 +37,8 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.PostConstruct;
import org.devgateway.jocds.jsonschema.checker.DeprecatedSyntaxChecker;
import org.devgateway.jocds.jsonschema.validator.DeprecatedValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -57,6 +69,8 @@ public class OcdsValidatorService {
@Autowired
private ObjectMapper jacksonObjectMapper;

private ValidationConfiguration validationConfiguration;

private JsonNode getUnmodifiedSchemaNode(OcdsValidatorRequest request) {
try {
logger.debug("Loading unmodified schema node of type " + request.getSchemaType() + " version "
Expand Down Expand Up @@ -236,6 +250,7 @@ private JsonSchema getSchema(OcdsValidatorRequest request) {
schemaNode = applyExtensions(schemaNode, request);
try {
JsonSchema schema = JsonSchemaFactory.newBuilder()
.setValidationConfiguration(validationConfiguration)
.setReportProvider(new ListReportProvider(LogLevel.ERROR, LogLevel.NONE)).freeze()
.getJsonSchema(schemaNode);
logger.debug("Saving to cache schema with extensions " + request.getKey());
Expand Down Expand Up @@ -289,6 +304,45 @@ private void init() {
initSchemaNamePrefix();
initMajorLatestFullVersion();
initExtensions();
initJsonSchemaFactory();
}

private Keyword createDeprecatedKeyword() {
return Keyword.newBuilder(OcdsValidatorConstants.CustomSchemaKeywords.DEPRECATED)
.withSyntaxChecker(DeprecatedSyntaxChecker.getInstance())
.withSimpleDigester(NodeType.OBJECT)
.withValidatorClass(DeprecatedValidator.class)
.freeze();
}

/**
* This method initializes the custom json schema factory
*/
private void initJsonSchemaFactory() {
/*
* Build a library, based on the v4 library, with this new keyword
*/
final Library library = DraftV4Library.get().thaw()
.addKeyword(createDeprecatedKeyword()).freeze();

validationConfiguration = ValidationConfiguration.newBuilder()
.setDefaultLibrary("https://www.open-contracting.org/data-standard/", library)
.setValidationMessages(createJsonSchemaCustomMessages()).freeze();

}

/**
* These are custom messages used by the validator to report warnings and errors
*/
private MessageBundle createJsonSchemaCustomMessages() {
final String key = "missingDivisors";
final String value = "integer value is not a multiple of all divisors";
final MessageSource source = MapMessageSource.newBuilder()
.put(key, value).build();
final MessageBundle bundle
= MessageBundles.getBundle(JsonSchemaValidationBundle.class)
.thaw().appendSource(source).freeze();
return bundle;
}

public ProcessingReport validate(OcdsValidatorStringRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2017. Development Gateway and contributors. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for details.
*/

package org.devgateway.jocds.jsonschema.checker;

import com.github.fge.jackson.NodeType;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.keyword.syntax.checkers.AbstractSyntaxChecker;
import com.github.fge.jsonschema.core.keyword.syntax.checkers.SyntaxChecker;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.tree.SchemaTree;
import com.github.fge.msgsimple.bundle.MessageBundle;
import java.util.Collection;
import org.devgateway.jocds.OcdsValidatorConstants;

public final class DeprecatedSyntaxChecker extends AbstractSyntaxChecker {

private static final SyntaxChecker INSTANCE = new DeprecatedSyntaxChecker();

public static SyntaxChecker getInstance() {
return INSTANCE;
}

private DeprecatedSyntaxChecker() {
super(OcdsValidatorConstants.CustomSchemaKeywords.DEPRECATED, NodeType.OBJECT);
}

@Override
protected void checkValue(final Collection<JsonPointer> pointers,
final MessageBundle bundle, final ProcessingReport report,
final SchemaTree tree)
throws ProcessingException {
System.out.println(tree);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2017. Development Gateway and contributors. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for details.
*/

package org.devgateway.jocds.jsonschema.validator;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.processing.Processor;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidator;
import com.github.fge.jsonschema.processors.data.FullData;
import com.github.fge.msgsimple.bundle.MessageBundle;

/**
* Keyword validator for {@code additionalProperties}
*/
public final class DeprecatedValidator
extends AbstractKeywordValidator {

private final JsonNode deprecated;

public DeprecatedValidator(final JsonNode digest) {
super("deprecated");
deprecated = digest.get(keyword);
}

@Override
public void validate(final Processor<FullData, FullData> processor,
final ProcessingReport report, final MessageBundle bundle,
final FullData data)
throws ProcessingException {

if (deprecated == null) {
return;
}

report.warn(newMsg(data, bundle,
"warn.jocds.deprecatedValidator")
.putArgument("unwanted", deprecated));
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder(keyword + ": ");

return sb.toString();
}

}

0 comments on commit 1d354d7

Please sign in to comment.