All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.miragon.miranum.connect.json.impl.JsonSchemaImpl Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
package io.miragon.miranum.connect.json.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.ValidationMessage;
import io.miragon.miranum.connect.json.api.JsonSchema;
import io.miragon.miranum.connect.json.api.ValidationResult;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
public class JsonSchemaImpl implements JsonSchema {

    private final com.networknt.schema.JsonSchema schema;
    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public List validate(final Object data) {
        final JsonNode node = this.mapper.valueToTree(data);
        return this.schema.validate(node)
                .stream()
                .map(this::map).toList();
    }


    @Override
    public List validate(final Object data, final Object rootData) {
        final JsonNode node = this.mapper.valueToTree(data);
        final JsonNode rootNode = this.mapper.valueToTree(rootData);
        return this.schema.validate(node, rootNode, "$")
                .stream()
                .map(this::map).toList();
    }

    @Override
    public JsonNode getSchema() {
        return this.schema.getSchemaNode();
    }

    private ValidationResult map(final ValidationMessage obj) {
        return new ValidationResult(
                obj.getType(),
                obj.getCode(),
                obj.getPath(),
                obj.getSchemaPath(),
                obj.getArguments(),
                obj.getDetails(),
                obj.getMessage()
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy