http://git-wip-us.apache.org/repos/asf/bval/blob/40fc20f6/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java new file mode 100644 index 0000000..d1e3e19 --- /dev/null +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/PastOrPresentValidator.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.apache.bval.constraints; + +import java.time.Clock; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.MonthDay; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.Year; +import java.time.YearMonth; +import java.time.ZonedDateTime; +import java.time.chrono.ChronoLocalDate; +import java.time.chrono.ChronoLocalDateTime; +import java.time.chrono.ChronoZonedDateTime; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.function.Function; + +import javax.validation.ConstraintValidator; +import javax.validation.constraints.PastOrPresent; + +/** + * Defines built-in {@link ConstraintValidator} implementations for {@link PastOrPresent}. + * + * @param <T> + * validated type + */ +public abstract class PastOrPresentValidator<T extends Comparable<T>> extends TimeValidator<PastOrPresent, T> { + + public static class ForDate extends PastOrPresentValidator<Date> { + + public ForDate() { + super(clock -> Date.from(clock.instant())); + } + } + + public static class ForCalendar extends PastOrPresentValidator<Calendar> { + + public ForCalendar() { + super(clock -> GregorianCalendar.from(clock.instant().atZone(clock.getZone()))); + } + } + + public static class ForInstant extends PastOrPresentValidator<Instant> { + + public ForInstant() { + super(Instant::now); + } + } + + public static class ForChronoLocalDate extends PastOrPresentValidator<ChronoLocalDate> { + + public ForChronoLocalDate() { + super(LocalDate::now); + } + } + + public static class ForChronoLocalDateTime extends PastOrPresentValidator<ChronoLocalDateTime<?>> { + + public ForChronoLocalDateTime() { + super(LocalDateTime::now); + } + } + + public static class ForLocalTime extends PastOrPresentValidator<LocalTime> { + + public ForLocalTime() { + super(LocalTime::now); + } + } + + public static class ForOffsetDateTime extends PastOrPresentValidator<OffsetDateTime> { + + public ForOffsetDateTime() { + super(OffsetDateTime::now); + } + } + + public static class ForOffsetTime extends PastOrPresentValidator<OffsetTime> { + + public ForOffsetTime() { + super(OffsetTime::now); + } + } + + public static class ForChronoZonedDateTime extends PastOrPresentValidator<ChronoZonedDateTime<?>> { + + public ForChronoZonedDateTime() { + super(ZonedDateTime::now); + } + } + + public static class ForMonthDay extends PastOrPresentValidator<MonthDay> { + + public ForMonthDay() { + super(MonthDay::now); + } + } + + public static class ForYear extends PastOrPresentValidator<Year> { + + public ForYear() { + super(Year::now); + } + } + + public static class ForYearMonth extends PastOrPresentValidator<YearMonth> { + + public ForYearMonth() { + super(YearMonth::now); + } + } + + protected PastOrPresentValidator(Function<Clock, T> now) { + super(now, n -> n <= 0); + } +}
http://git-wip-us.apache.org/repos/asf/bval/blob/40fc20f6/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java new file mode 100644 index 0000000..0136d83 --- /dev/null +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/PastValidator.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.apache.bval.constraints; + +import java.time.Clock; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.MonthDay; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.Year; +import java.time.YearMonth; +import java.time.ZonedDateTime; +import java.time.chrono.ChronoLocalDate; +import java.time.chrono.ChronoLocalDateTime; +import java.time.chrono.ChronoZonedDateTime; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.function.Function; + +import javax.validation.ConstraintValidator; +import javax.validation.constraints.Past; + +/** + * Defines built-in {@link ConstraintValidator} implementations for {@link Past}. + * + * @param <T> + * validated type + */ +public abstract class PastValidator<T extends Comparable<T>> extends TimeValidator<Past, T> { + + public static class ForDate extends PastValidator<Date> { + + public ForDate() { + super(clock -> Date.from(clock.instant())); + } + } + + public static class ForCalendar extends PastValidator<Calendar> { + + public ForCalendar() { + super(clock -> GregorianCalendar.from(clock.instant().atZone(clock.getZone()))); + } + } + + public static class ForInstant extends PastValidator<Instant> { + + public ForInstant() { + super(Instant::now); + } + } + + public static class ForChronoLocalDate extends PastValidator<ChronoLocalDate> { + + public ForChronoLocalDate() { + super(LocalDate::now); + } + } + + public static class ForChronoLocalDateTime extends PastValidator<ChronoLocalDateTime<?>> { + + public ForChronoLocalDateTime() { + super(LocalDateTime::now); + } + } + + public static class ForLocalTime extends PastValidator<LocalTime> { + + public ForLocalTime() { + super(LocalTime::now); + } + } + + public static class ForOffsetDateTime extends PastValidator<OffsetDateTime> { + + public ForOffsetDateTime() { + super(OffsetDateTime::now); + } + } + + public static class ForOffsetTime extends PastValidator<OffsetTime> { + + public ForOffsetTime() { + super(OffsetTime::now); + } + } + + public static class ForChronoZonedDateTime extends PastValidator<ChronoZonedDateTime<?>> { + + public ForChronoZonedDateTime() { + super(ZonedDateTime::now); + } + } + + public static class ForMonthDay extends PastValidator<MonthDay> { + + public ForMonthDay() { + super(MonthDay::now); + } + } + + public static class ForYear extends PastValidator<Year> { + + public ForYear() { + super(Year::now); + } + } + + public static class ForYearMonth extends PastValidator<YearMonth> { + + public ForYearMonth() { + super(YearMonth::now); + } + } + + protected PastValidator(Function<Clock, T> now) { + super(now, n -> n < 0); + } +} http://git-wip-us.apache.org/repos/asf/bval/blob/40fc20f6/bval-jsr/src/main/java/org/apache/bval/constraints/PatternValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/PatternValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/PatternValidator.java index 9534e6b..8a53062 100644 --- a/bval-jsr/src/main/java/org/apache/bval/constraints/PatternValidator.java +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/PatternValidator.java @@ -27,7 +27,7 @@ import java.util.regex.PatternSyntaxException; * validator using a regular expression, * based on the jsr Pattern constraint annotation. */ -public class PatternValidator implements ConstraintValidator<Pattern, String> { +public class PatternValidator implements ConstraintValidator<Pattern, CharSequence> { protected java.util.regex.Pattern pattern; @Override @@ -46,7 +46,7 @@ public class PatternValidator implements ConstraintValidator<Pattern, String> { } @Override - public boolean isValid(String value, ConstraintValidatorContext context) { + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { return value == null || pattern.matcher(value).matches(); } } http://git-wip-us.apache.org/repos/asf/bval/blob/40fc20f6/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java b/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java new file mode 100644 index 0000000..02e3836 --- /dev/null +++ b/bval-jsr/src/main/java/org/apache/bval/constraints/TimeValidator.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.apache.bval.constraints; + +import java.lang.annotation.Annotation; +import java.time.Clock; +import java.util.function.Function; +import java.util.function.IntPredicate; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public abstract class TimeValidator<A extends Annotation, T extends Comparable<T>> implements ConstraintValidator<A, T> { + + private final Function<Clock, T> now; + private final IntPredicate test; + + protected TimeValidator(Function<Clock, T> now, IntPredicate test) { + super(); + this.now = now; + this.test = test; + } + + @Override + public final boolean isValid(T value, ConstraintValidatorContext context) { + return value == null || test.test(value.compareTo(now.apply(context.getClockProvider().getClock()))); + } +} http://git-wip-us.apache.org/repos/asf/bval/blob/40fc20f6/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties ---------------------------------------------------------------------- diff --git a/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties b/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties index 7bc433b..cc5cd6c 100644 --- a/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties +++ b/bval-jsr/src/main/resources/org/apache/bval/jsr/DefaultConstraints.properties @@ -22,22 +22,94 @@ javax.validation.constraints.AssertFalse=org.apache.bval.constraints.AssertFalseValidator javax.validation.constraints.AssertTrue=org.apache.bval.constraints.AssertTrueValidator + javax.validation.constraints.DecimalMax=org.apache.bval.constraints.DecimalMaxValidatorForNumber,\ org.apache.bval.constraints.DecimalMaxValidatorForString + javax.validation.constraints.DecimalMin=org.apache.bval.constraints.DecimalMinValidatorForNumber,\ org.apache.bval.constraints.DecimalMinValidatorForString + javax.validation.constraints.Digits=org.apache.bval.constraints.DigitsValidatorForNumber,\ org.apache.bval.constraints.DigitsValidatorForString -javax.validation.constraints.Future=org.apache.bval.constraints.FutureValidatorForDate,\ - org.apache.bval.constraints.FutureValidatorForCalendar + +javax.validation.constraints.Email=org.apache.bval.constraints.EmailValidator + +javax.validation.constraints.Future=\ + org.apache.bval.constraints.FutureValidator$ForCalendar,\ + org.apache.bval.constraints.FutureValidator$ForDate,\ + org.apache.bval.constraints.FutureValidator$ForChronoLocalDate,\ + org.apache.bval.constraints.FutureValidator$ForChronoLocalDateTime,\ + org.apache.bval.constraints.FutureValidator$ForChronoZonedDateTime,\ + org.apache.bval.constraints.FutureValidator$ForInstant,\ + org.apache.bval.constraints.FutureValidator$ForLocalTime,\ + org.apache.bval.constraints.FutureValidator$ForMonthDay,\ + org.apache.bval.constraints.FutureValidator$ForOffsetDateTime,\ + org.apache.bval.constraints.FutureValidator$ForOffsetTime,\ + org.apache.bval.constraints.FutureValidator$ForYear,\ + org.apache.bval.constraints.FutureValidator$ForYearMonth + +javax.validation.constraints.FutureOrPresent=\ + org.apache.bval.constraints.FutureOrPresentValidator$ForCalendar,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForDate,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForChronoLocalDate,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForChronoLocalDateTime,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForChronoZonedDateTime,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForInstant,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForLocalTime,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForMonthDay,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForOffsetDateTime,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForOffsetTime,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForYear,\ + org.apache.bval.constraints.FutureOrPresentValidator$ForYearMonth + javax.validation.constraints.Max=org.apache.bval.constraints.MaxValidatorForNumber,\ org.apache.bval.constraints.MaxValidatorForString + javax.validation.constraints.Min=org.apache.bval.constraints.MinValidatorForNumber,\ org.apache.bval.constraints.MinValidatorForString + +javax.validation.constraints.Negative=org.apache.bval.constraints.NumberSignValidator$ForNegative +javax.validation.constraints.NegativeOrZero=org.apache.bval.constraints.NumberSignValidator$ForNegative$OrZero + +javax.validation.constraints.NotEmpty=org.apache.bval.constraints.NotEmptyValidator,\ + org.apache.bval.constraints.NotEmptyValidatorForCharSequence,\ + org.apache.bval.constraints.NotEmptyValidatorForCollection,\ + org.apache.bval.constraints.NotEmptyValidatorForMap + javax.validation.constraints.NotNull=org.apache.bval.constraints.NotNullValidator javax.validation.constraints.Null=org.apache.bval.constraints.NullValidator -javax.validation.constraints.Past=org.apache.bval.constraints.PastValidatorForDate,\ - org.apache.bval.constraints.PastValidatorForCalendar + +javax.validation.constraints.Past=\ + org.apache.bval.constraints.PastValidator$ForCalendar,\ + org.apache.bval.constraints.PastValidator$ForDate,\ + org.apache.bval.constraints.PastValidator$ForChronoLocalDate,\ + org.apache.bval.constraints.PastValidator$ForChronoLocalDateTime,\ + org.apache.bval.constraints.PastValidator$ForChronoZonedDateTime,\ + org.apache.bval.constraints.PastValidator$ForInstant,\ + org.apache.bval.constraints.PastValidator$ForLocalTime,\ + org.apache.bval.constraints.PastValidator$ForMonthDay,\ + org.apache.bval.constraints.PastValidator$ForOffsetDateTime,\ + org.apache.bval.constraints.PastValidator$ForOffsetTime,\ + org.apache.bval.constraints.PastValidator$ForYear,\ + org.apache.bval.constraints.PastValidator$ForYearMonth + +javax.validation.constraints.PastOrPresent=\ + org.apache.bval.constraints.PastOrPresentValidator$ForCalendar,\ + org.apache.bval.constraints.PastOrPresentValidator$ForDate,\ + org.apache.bval.constraints.PastOrPresentValidator$ForChronoLocalDate,\ + org.apache.bval.constraints.PastOrPresentValidator$ForChronoLocalDateTime,\ + org.apache.bval.constraints.PastOrPresentValidator$ForChronoZonedDateTime,\ + org.apache.bval.constraints.PastOrPresentValidator$ForInstant,\ + org.apache.bval.constraints.PastOrPresentValidator$ForLocalTime,\ + org.apache.bval.constraints.PastOrPresentValidator$ForMonthDay,\ + org.apache.bval.constraints.PastOrPresentValidator$ForOffsetDateTime,\ + org.apache.bval.constraints.PastOrPresentValidator$ForOffsetTime,\ + org.apache.bval.constraints.PastOrPresentValidator$ForYear,\ + org.apache.bval.constraints.PastOrPresentValidator$ForYearMonth + +javax.validation.constraints.Positive=org.apache.bval.constraints.NumberSignValidator$ForPositive +javax.validation.constraints.PositiveOrZero=org.apache.bval.constraints.NumberSignValidator$ForPositive$OrZero + javax.validation.constraints.Size=org.apache.bval.constraints.SizeValidatorForCharSequence,\ org.apache.bval.constraints.SizeValidatorForMap,\ org.apache.bval.constraints.SizeValidatorForCollection,\ @@ -50,4 +122,5 @@ javax.validation.constraints.Size=org.apache.bval.constraints.SizeValidatorForCh org.apache.bval.constraints.SizeValidatorForArrayOfLong,\ org.apache.bval.constraints.SizeValidatorForArrayOfObject,\ org.apache.bval.constraints.SizeValidatorForArrayOfShort + javax.validation.constraints.Pattern=org.apache.bval.constraints.PatternValidator
