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

ca.ibodrov.mica.server.data.ValidatedInput Maven / Gradle / Ivy

There is a newer version: 0.0.25
Show newest version
package ca.ibodrov.mica.server.data;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.networknt.schema.ValidationMessage;
import com.walmartlabs.concord.server.sdk.validation.ValidationErrorXO;
import com.walmartlabs.concord.server.sdk.validation.ValidationErrorsException;

import java.util.Set;

import static java.util.Comparator.comparing;

public record ValidatedInput(Set messages) {

    @JsonIgnore
    public boolean isValid() {
        return messages.isEmpty();
    }

    @JsonIgnore
    public ValidationErrorsException toException() {
        if (isValid()) {
            throw new IllegalStateException("Input is valid");
        }

        return new ValidationErrorsException()
                .withErrors(messages.stream()
                        .sorted(comparing(ValidationMessage::getPath).thenComparing(ValidationMessage::getMessage))
                        .map(m -> new ValidationErrorXO(m.getMessage()))
                        .toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy