feat: 添加 MapValidator#ruleForComparable

This commit is contained in:
zhouxy108 2025-05-28 09:40:00 +08:00
parent 6cd47e369e
commit 37f4af67cd
2 changed files with 23 additions and 1 deletions

View File

@ -83,6 +83,12 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
return ruleForString(m -> (String) m.get(key));
}
protected final <E extends Comparable<E>> ComparablePropertyValidator<Map<K, V>, E> ruleForComparable(K key) {
@SuppressWarnings("unchecked")
Function<Map<K, V>, E> getter = m -> (E) m.get(key);
return ruleForComparable(getter);
}
protected final <E> CollectionPropertyValidator<Map<K, V>, E> ruleForCollection(K key) {
@SuppressWarnings("unchecked")
Function<Map<K, V>, Collection<E>> getter = m -> (Collection<E>) m.get(key);

View File

@ -1,3 +1,19 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* 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
*
* https://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 xyz.zhouxy.plusone.map.validator;
import static org.junit.jupiter.api.Assertions.*;
@ -102,7 +118,7 @@ class ParamsValidator extends MapValidator<String, Object> {
.notNull(d -> ExampleException.withMessage("The doubleProperty cannot be null, but it was %s", d));
ruleForString(STRING_PROPERTY)
.notNull();
ruleFor(DATE_TIME_PROPERTY)
ruleForComparable(DATE_TIME_PROPERTY)
.notNull("The dateTimeProperty cannot be null");
ruleFor(OBJECT_PROPERTY)
.notNull(() -> ExampleException.withMessage("The objectProperty cannot be null"));