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

uk.gov.service.payments.commons.api.validation.RequestValidator Maven / Gradle / Ivy

There is a newer version: 1.0.20241216100655
Show newest version
package uk.gov.service.payments.commons.api.validation;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.lang.String.format;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.math.NumberUtils.isDigits;

public class RequestValidator {

    public static List checkIsNumeric(JsonNode payload, String... fieldNames) {
        return applyCheck(payload, isNotNumeric(), fieldNames, "Field [%s] must be a number");
    }

    public static List checkIsBoolean(JsonNode payload, String... fieldNames) {
        return applyCheck(payload, isNotBoolean(), fieldNames, "Field [%s] must be a boolean");
    }

    public static List checkIsString(JsonNode payload, String... fieldNames) {
        return applyCheck(payload, isNotString(), fieldNames, "Field [%s] must be a string");
    }

    public static List checkIfExistsOrEmpty(JsonNode payload, String... fieldNames) {
        return applyCheck(payload, notExistOrEmpty(), fieldNames, "Field [%s] is required");
    }    
    
    public static List checkIfNull(JsonNode payload, String... fieldNames) {
        return applyCheck(payload, isNullValue(), fieldNames, "Field [%s] is required");
    }

    public static List checkMaxLength(JsonNode payload, int maxLength, String... fieldNames) {
        return applyCheck(payload, exceedsMaxLength(maxLength), fieldNames, "Field [%s] must have a maximum length of " + maxLength + " characters");
    }

    public static List checkIsAllowedValue(JsonNode payload, Set allowedValues, String... fieldNames) {
        return applyCheck(payload, isNotAllowedValue(allowedValues), fieldNames, "Field [%s] must be one of "
                + allowedValues.stream().sorted().collect(Collectors.joining(", ", "[", "]")));
    }

    private static List applyCheck(JsonNode payload, Function check, String[] fieldNames, String errorMessage) {
        List errors = new ArrayList<>();
        for (String fieldName : fieldNames) {
            if (check.apply(payload.get(fieldName))) {
                errors.add(format(errorMessage, fieldName));
            }
        }
        return errors;
    }

    private static Function exceedsMaxLength(int maxLength) {
        return jsonNode -> jsonNode.asText().length() > maxLength;
    }

    private static Function notExistOrEmpty() {
        return (jsonElement) -> {
            if (jsonElement instanceof NullNode) {
                return isNullValue().apply(jsonElement);
            } else if (jsonElement instanceof ArrayNode) {
                return notExistOrEmptyArray().apply(jsonElement);
            } else if (jsonElement instanceof ObjectNode) {
                return notExistOrEmptyObject().apply(jsonElement);
            } else {
                return notExistOrBlankText().apply(jsonElement);
            }
        };
    }

    private static Function notExistOrEmptyArray() {
        return jsonElement -> (
                jsonElement == null ||
                        ((jsonElement instanceof ArrayNode) && (jsonElement.size() == 0))
        );
    }

    private static Function notExistOrEmptyObject() {
        return jsonElement -> (
                jsonElement == null ||
                        ((jsonElement instanceof ObjectNode) && jsonElement.isEmpty())
        );
    }

    private static Function notExistOrBlankText() {
        return jsonElement -> (
                jsonElement == null ||
                        isBlank(jsonElement.asText())
        );
    }

    private static Function isNullValue() {
        return jsonElement -> (
                jsonElement == null || jsonElement.isNull()
        );
    }

    private static Function isNotNumeric() {
        return jsonNode -> !isDigits(jsonNode.asText());
    }

    private static Function isNotBoolean() {
        return jsonNode -> jsonNode == null || !jsonNode.isBoolean();
    }

    private static Function isNotAllowedValue(Set allowedValues) {
        return jsonNode -> jsonNode == null || !allowedValues.contains(jsonNode.asText());
    }

    private static Function isNotString() {
        return jsonNode -> !jsonNode.isTextual();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy