jsonschema fge json-schema-validator 高级能力 自定义类校验

入参校验产品化 schema_个人渣记录仅为自己搜索用的博客-CSDN博客

自定义的string format可以使用. 详见 

  fpe的 addFormatValidator

 ajv 的 addFormat能力

借鉴自chatgpt, 谷歌了半天,没看到好答案.

Q: "jsonschema 自定义 object  校验"

A: 如下

2014年后未更新 

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

扫描二维码关注公众号,回复: 15392008 查看本文章

创建自定义AbstractKeywordValidator

要自定义 JSON Schema 中的对象校验,您需要编写一个自定义验证器。以下是一个示例,它定义了一个名为“myObject”的自定义对象校验,该校验验证对象是否包含一个名为“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")));
        }
    }
}

注册到 JSON Schema

然后,您需要将此验证器注册到 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);
    }
}

校验

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());
    }
}

猜你喜欢

转载自blog.csdn.net/fei33423/article/details/130874426