jsonschema fge json-schema-validator advanced capability custom class validation

Entering parameters to verify the product schema

Custom string format can be used. For details, see 

  fpe's addFormatValidator

 ajv's addFormat capability

Learned from chatgpt, Google for a long time, did not see a good answer.

Q: "jsonschema custom object validation"

A: As follows

Not updated since 2014 

<dependency>
  <groupId>com.github.fge</groupId>
  <artifactId>json-schema-validator</artifactId>
  <version>2.2.14</version>
</dependency>

 

Create a custom AbstractKeywordValidator

To customize object validation in JSON Schema, you need to write a custom validator. Here is an example that defines a custom object validation named "myObject" that verifies that the object contains a property named "foo":

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.util.Dictionary;
import com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidator;

public class MyObjectValidator extends AbstractKeywordValidator {
    private static final Dictionary<String> messages = messages();

    public MyObjectValidator(final JsonNode digest) {
        super("myObject");
    }

    @Override
    public void validate(final ProcessingReport report, final JsonNode schema, final JsonNode instance) throws ProcessingException {
        if (!instance.has("foo")) {
            report.error(newMsg(schema, messages.get("err.object.noFoo")));
        }
    }
}

Register to JSON Schema

Then you need to register this validator with JSON Schema:

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration;
import com.github.fge.jsonschema.core.load.uri.URITranslatorConfiguration;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.report.ProcessingReportBuilder;
import com.github.fge.jsonschema.core.report.ReportProvider;
import com.github.fge.jsonschema.core.report.ReportProviderConfiguration;
import com.github.fge.jsonschema.core.report.ReportProviderFactory;
import com.github.fge.jsonschema.core.report.Slf4jLogLevel;
import com.github.fge.jsonschema.core.report.Slf4jReportProvider;
import com.github.fge.jsonschema.core.util.URIUtils;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.main.JsonValidator;
import com.github.fge.jsonschema.main.JsonValidatorBuilder;
import com.github.fge.jsonschema.processors.syntax.SyntaxValidator;

public class MySchemaFactory {
    private static final JsonValidator VALIDATOR;
    private static final JsonSchemaFactory FACTORY;

    static {
        final ReportProviderConfiguration reportProviderConfiguration = new ReportProviderConfiguration()
                .setLogProvider(Slf4jReportProvider.provider())
                .setLogLevel(Slf4jLogLevel.DEBUG);
        final ReportProviderFactory reportProviderFactory = new ReportProviderFactory(reportProviderConfiguration);
        final ReportProvider reportProvider = reportProviderFactory.createReportProvider();
        final ProcessingReportBuilder reportBuilder = reportProvider.newReportBuilder();
        final LoadingConfiguration loadingConfiguration = LoadingConfiguration.newBuilder()
                .setURITranslatorConfiguration(URITranslatorConfiguration.newBuilder()
                        .setNamespace(URIUtils.toURI("http://example.com/schemas"))
                        .freeze())
                .freeze();
        final JsonValidatorBuilder validatorBuilder = JsonValidator.newBuilder()
                .setReportProvider(reportProvider)
                .setLoadingConfiguration(loadingConfiguration)
                .setSyntaxValidator(SyntaxValidator.none());
        VALIDATOR = validatorBuilder.build();
        FACTORY = JsonSchemaFactory.newBuilder()
                .setValidator(VALIDATOR)
                .addKeywordValidator("myObject", new MyObjectValidator(null))
                .freeze();
    }

    public static JsonSchema getSchema(final JsonNode schemaNode) throws ProcessingException {
        return FACTORY.getJsonSchema(schemaNode);
    }
}

check

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchema;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MyObjectValidatorTest {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Test
    public void testValid() throws ProcessingException {
        final JsonNode schemaNode = MAPPER.readTree("{\"type\": \"object\", \"myObject\": true}");
        final JsonSchema schema = MySchemaFactory.getSchema(schemaNode);
        assertTrue(schema.validate(MAPPER.readTree("{\"foo\": \"bar\"}")).isSuccess());
    }

    @Test
    public void testInvalid() throws ProcessingException {
        final JsonNode schemaNode = MAPPER.readTree("{\"type\": \"object\", \"myObject\": true}");
        final JsonSchema schema = MySchemaFactory.getSchema(schemaNode);
        assertFalse(schema.validate(MAPPER.readTree("{\"bar\": \"foo\"}")).isSuccess());
    }
}

Guess you like

Origin blog.csdn.net/fei33423/article/details/130874426