Java Code Examples for io.swagger.models.parameters.SerializableParameter#getMaxItems()

The following examples show how to use io.swagger.models.parameters.SerializableParameter#getMaxItems() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArrayParameterValidator.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
private Status doValidate(final Collection<String> values,
                        final SerializableParameter parameter) {

    if (parameter.getMaxItems() != null && values.size() > parameter.getMaxItems()) {
        return new Status("ERR11006", parameter.getName(), parameter.getMaxItems(), values.size());
    }

    if (parameter.getMinItems() != null && values.size() < parameter.getMinItems()) {
        return new Status("ERR11007", parameter.getName(), parameter.getMinItems(), values.size());
    }

    if (Boolean.TRUE.equals(parameter.isUniqueItems()) &&
            values.stream().distinct().count() != values.size()) {
        return new Status("ERR11008", parameter.getName());
    }

    if (parameter.getEnum() != null && !parameter.getEnum().isEmpty()) {
        final Set<String> enumValues = new HashSet<>(parameter.getEnum());
        Optional<String> value =
            values.stream()
                .filter(v -> !enumValues.contains(v))
                .findFirst();
        if(value.isPresent()) {
            return new Status("ERR11009", value.get(), parameter.getName(), parameter.getEnum());
        }
    }

    Optional<Status> optional =
            values.stream()
            .map(v -> schemaValidator.validate(v, parameter.getItems()))
            .filter(s -> s != null)
            .findFirst();
    if(optional.isPresent()) {
        return optional.get();
    } else {
        return null;
    }
}