From 37f4af67cdc7d52ba0918e9b44c05b40b2e5ad92 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 28 May 2025 09:40:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20`MapValidator#rule?= =?UTF-8?q?ForComparable`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/validator/MapValidator.java | 6 ++++++ .../map/validator/MapValidatorTests.java | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java index 2eda053..e724bf2 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java @@ -83,6 +83,12 @@ public abstract class MapValidator extends BaseValidator> { return ruleForString(m -> (String) m.get(key)); } + protected final > ComparablePropertyValidator, E> ruleForComparable(K key) { + @SuppressWarnings("unchecked") + Function, E> getter = m -> (E) m.get(key); + return ruleForComparable(getter); + } + protected final CollectionPropertyValidator, E> ruleForCollection(K key) { @SuppressWarnings("unchecked") Function, Collection> getter = m -> (Collection) m.get(key); diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/map/validator/MapValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/map/validator/MapValidatorTests.java index 3cc7327..b2a2afe 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/map/validator/MapValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/map/validator/MapValidatorTests.java @@ -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 { .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"));