forked from plusone/plusone-validator
docs: 更新示例 (plusone/plusone-validator#18 from gitea)
Co-authored-by: ZhouXY108 <luquanlion@outlook.com> Co-committed-by: ZhouXY108 <luquanlion@outlook.com>
This commit is contained in:
23
README.md
23
README.md
@@ -25,14 +25,9 @@ class CustomerValidator extends BaseValidator<Customer> {
|
|||||||
.notNull("生日不能为空")
|
.notNull("生日不能为空")
|
||||||
.must(LocalDate.now().minusYears(16)::isAfter, "用户必须大于16周岁");
|
.must(LocalDate.now().minusYears(16)::isAfter, "用户必须大于16周岁");
|
||||||
ruleFor(Customer::getAddress).length(20, 250, "地址长度必须在20-250之间");
|
ruleFor(Customer::getAddress).length(20, 250, "地址长度必须在20-250之间");
|
||||||
ruleFor((Customer customer) -> Pair.of(customer.getVipLevel(), customer.getBirthday()))
|
ruleFor(Customer::getVipLevel, Customer::getBirthday)
|
||||||
.must(CustomerValidator::validateAge, "5级以上会员必须满18周岁");
|
.must((vipLevel, birthday) -> vipLevel <= 5 || LocalDate.now().minusYears(18).isAfter(birthday),
|
||||||
}
|
"5级以上会员必须满18周岁");
|
||||||
|
|
||||||
private static boolean validateAge(Pair<Integer, LocalDate> vipLevelAndBirthday) {
|
|
||||||
Integer vipLevel = vipLevelAndBirthday.getLeft();
|
|
||||||
LocalDate birthday = vipLevelAndBirthday.getRight();
|
|
||||||
return vipLevel <= 5 || LocalDate.now().minusYears(18).isAfter(birthday);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CustomerValidator getInstance() {
|
public static CustomerValidator getInstance() {
|
||||||
@@ -73,15 +68,9 @@ class CustomerMapValidator extends MapValidator<String, Object> {
|
|||||||
.notNull("生日不能为空")
|
.notNull("生日不能为空")
|
||||||
.must(LocalDate.now().minusYears(16)::isAfter, "用户必须大于16周岁");
|
.must(LocalDate.now().minusYears(16)::isAfter, "用户必须大于16周岁");
|
||||||
ruleForString("address").length(20, 250, "地址长度必须在20-250之间");
|
ruleForString("address").length(20, 250, "地址长度必须在20-250之间");
|
||||||
this.<Pair<Integer, LocalDate>>ruleFor((Map<String, Object> customer) ->
|
this.<Integer, LocalDate>ruleForPair("vipLevel", "birthday")
|
||||||
Pair.of(MapUtils.getInteger(customer, "vipLevel"), (LocalDate) customer.get("birthday")))
|
.must((vipLevel, birthday) -> vipLevel <= 5 || LocalDate.now().minusYears(18).isAfter(birthday),
|
||||||
.must(CustomerMapValidator::validateAge, "5级以上会员必须满18周岁");
|
"5级以上会员必须满18周岁");
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean validateAge(Pair<Integer, LocalDate> vipLevelAndBirthday) {
|
|
||||||
Integer vipLevel = vipLevelAndBirthday.getLeft();
|
|
||||||
LocalDate birthday = vipLevelAndBirthday.getRight();
|
|
||||||
return vipLevel <= 5 || LocalDate.now().minusYears(18).isAfter(birthday);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CustomerMapValidator getInstance() {
|
public static CustomerMapValidator getInstance() {
|
||||||
|
Reference in New Issue
Block a user