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

com.predic8.membrane.core.openapi.validators.ValidationErrors Maven / Gradle / Ivy

There is a newer version: 5.7.3
Show newest version
/*
 *  Copyright 2022 predic8 GmbH, www.predic8.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.predic8.membrane.core.openapi.validators;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;

import java.util.*;
import java.util.stream.*;

import static com.predic8.membrane.core.openapi.util.Utils.*;
import static com.predic8.membrane.core.openapi.validators.ValidationErrors.Direction.*;

public class ValidationErrors {

    private final static ObjectMapper om = new ObjectMapper();

    private final List errors = new ArrayList<>();

    public enum Direction { REQUEST, RESPONSE }

    public static ValidationErrors create(ValidationContext ctx, String message) {
        ValidationErrors ve = new ValidationErrors();
        ve.add(ctx, message);
        return ve;
    }

    public List getErrors() {
        return errors;
    }

    public ValidationErrors add(ValidationError error) {
        if (error != null)
            errors.add(error);
        return this;
    }

    public void add(List errors) {
        this.errors.addAll(errors);
    }

    public ValidationErrors add(ValidationErrors ve) {
        if (ve != null)
            this.errors.addAll(ve.errors);
        return this;
    }

    public ValidationErrors add(ValidationContext ctx, String message) {
        errors.add(new ValidationError(ctx, message));
        return this;
    }

    public int size() {
        return errors.size();
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public ValidationError get(int i) {
        return errors.get(i);
    }

    public Stream stream() {
        return errors.stream();
    }

    public byte[] getErrorMessage(Direction direction) {

        if (errors.size() == 0)
            return "No validation errors!".getBytes();

        Map>> m = getValidationErrorsGroupedByLocation(direction);
        Map wrapper = new LinkedHashMap<>();

        ValidationContext ctx = errors.get(0).getContext();
        setFieldIfNotNull(wrapper, "method", ctx.getMethod());
        setFieldIfNotNull(wrapper, "uriTemplate", ctx.getUriTemplate());
        setFieldIfNotNull(wrapper, "path", ctx.getPath());

        wrapper.put("validationErrors", m);

        try {
            return om.writerWithDefaultPrettyPrinter().writeValueAsBytes(wrapper);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "Error!".getBytes();
    }

    private Map>> getValidationErrorsGroupedByLocation(Direction direction) {
        Map>> m = new HashMap<>();
        errors.forEach(ve -> {
            List> ves = new ArrayList<>();
            ves.add(ve.getContentMap());
            m.merge(getLocationFor(direction, ve), ves, (vesOld, vesNew) -> {
                vesOld.addAll(vesNew);
                return vesOld;
            });
        });
        return m;
    }

    private String getLocationFor(Direction direction, ValidationError ve) {
        if (direction.equals(REQUEST)) {
            return ve.getContext().getLocationForRequest();
        }
        return ve.getContext().getLocationForResponse();
    }

    public boolean hasErrors() {
        return size() > 0;
    }

    @Override
    public String toString() {
        return "ValidationErrors{" +
                "errors=" + errors +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy