Skip to content

Commit

Permalink
Binder validators (#2239)
Browse files Browse the repository at this point in the history
* Add "validator" package with all classes inside it

Part of Binder task.

* Add BigDecimalRangeValidator

* Add BigIntegerRangeValidator

* Add ByteRangeValidator

* Add DateRangeValidator

* Add DateTimeRangeValidator

* Add DoubleRangeValidator

* Add EmailValidator

* Add FloatRangeValidator

* Add IntegerRangeValidator

* Add LongRangeValidator

* Add RangeValidator

* Add RegexpValidator

* Add ShortRangeValidator

* Add StringLengthValidator

* Correct java import

* Correct javadoc

* Sonar Qube fixes.
  • Loading branch information
Denis authored Aug 25, 2017
1 parent 9ed8a02 commit e37833f
Show file tree
Hide file tree
Showing 15 changed files with 1,076 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2000-2017 Vaadin Ltd.
*
* 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
*
* 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 com.vaadin.data.validator;

import java.util.Objects;

import com.vaadin.data.ValidationResult;
import com.vaadin.data.Validator;
import com.vaadin.server.SerializableFunction;

/**
* An abstract base class for typed validators.
*
* @param <T>
* The value type
* @author Vaadin Ltd.
* @since 8.0
*/
public abstract class AbstractValidator<T> implements Validator<T> {

private final SerializableFunction<T, String> messageProvider;

/**
* Constructs a validator with the given error message. The substring "{0}"
* is replaced by the value that failed validation.
*
* @param errorMessage
* the message to be included in a failed result, not null
*/
protected AbstractValidator(String errorMessage) {
Objects.requireNonNull(errorMessage, "error message cannot be null");
this.messageProvider = value -> errorMessage.replace("{0}",
String.valueOf(value));
}

/**
* Returns the error message for the given value.
*
* @param value
* an invalid value
* @return the formatted error message
*/
protected String getMessage(T value) {
return messageProvider.apply(value);
}

/**
* A helper method for creating a {@code Result} from a value and a validity
* flag. If the flag is true, returns {@code Result.ok}, otherwise yields
* {@code Result.error} bearing the error message returned by
* {@link #getMessage(Object)}.
* <p>
* For instance, the following {@code apply} method only accepts even
* numbers:
*
* <pre>
* &#64;Override
* public Result&lt;T&gt; apply(Integer value) {
* return toResult(value, value % 2 == 0);
* }
* </pre>
*
* @param value
* the validated value
* @param isValid
* whether the value is valid or not
* @return the validation result
*/
protected ValidationResult toResult(T value, boolean isValid) {
return isValid ? ValidationResult.ok()
: ValidationResult.error(getMessage(value));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2000-2017 Vaadin Ltd.
*
* 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
*
* 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 com.vaadin.data.validator;

import java.math.BigDecimal;
import java.util.Comparator;

/**
* Validator for validating that an {@link BigDecimal} is inside a given range.
*
* @author Vaadin Ltd.
* @since 8.0
*/
@SuppressWarnings("serial")
public class BigDecimalRangeValidator extends RangeValidator<BigDecimal> {

/**
* Creates a validator for checking that an BigDecimal is within a given
* range.
*
* By default the range is inclusive i.e. both minValue and maxValue are
* valid values. Use {@link #setMinValueIncluded(boolean)} or
* {@link #setMaxValueIncluded(boolean)} to change it.
*
*
* @param errorMessage
* the message to display in case the value does not validate.
* @param minValue
* The minimum value to accept or null for no limit
* @param maxValue
* The maximum value to accept or null for no limit
*/
public BigDecimalRangeValidator(String errorMessage, BigDecimal minValue,
BigDecimal maxValue) {
super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2000-2017 Vaadin Ltd.
*
* 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
*
* 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 com.vaadin.data.validator;

import java.math.BigInteger;
import java.util.Comparator;

/**
* Validator for validating that an {@link BigInteger} is inside a given range.
*
* @author Vaadin Ltd.
* @since 8.0
*/
@SuppressWarnings("serial")
public class BigIntegerRangeValidator extends RangeValidator<BigInteger> {

/**
* Creates a validator for checking that an BigInteger is within a given
* range.
*
* By default the range is inclusive i.e. both minValue and maxValue are
* valid values. Use {@link #setMinValueIncluded(boolean)} or
* {@link #setMaxValueIncluded(boolean)} to change it.
*
*
* @param errorMessage
* the message to display in case the value does not validate.
* @param minValue
* The minimum value to accept or null for no limit
* @param maxValue
* The maximum value to accept or null for no limit
*/
public BigIntegerRangeValidator(String errorMessage, BigInteger minValue,
BigInteger maxValue) {
super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2017 Vaadin Ltd.
*
* 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
*
* 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 com.vaadin.data.validator;

import java.util.Comparator;

/**
* Validator for validating that an {@link Byte} is inside a given range.
*
* @author Vaadin Ltd.
* @since 8.0
*/
@SuppressWarnings("serial")
public class ByteRangeValidator extends RangeValidator<Byte> {

/**
* Creates a validator for checking that an Byte is within a given range.
*
* By default the range is inclusive i.e. both minValue and maxValue are
* valid values. Use {@link #setMinValueIncluded(boolean)} or
* {@link #setMaxValueIncluded(boolean)} to change it.
*
*
* @param errorMessage
* the message to display in case the value does not validate.
* @param minValue
* The minimum value to accept or null for no limit
* @param maxValue
* The maximum value to accept or null for no limit
*/
public ByteRangeValidator(String errorMessage, Byte minValue,
Byte maxValue) {
super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2000-2017 Vaadin Ltd.
*
* 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
*
* 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 com.vaadin.data.validator;

import java.time.LocalDate;
import java.util.Comparator;

/**
* Validator for validating that a {@link LocalDate} is inside a given range.
*
* @author Vaadin Ltd.
* @since 8.0
*/
public class DateRangeValidator extends RangeValidator<LocalDate> {

/**
* Creates a validator for checking that a LocalDate is within a given
* range.
* <p>
* By default the range is inclusive i.e. both minValue and maxValue are
* valid values. Use {@link #setMinValueIncluded(boolean)} or
* {@link #setMaxValueIncluded(boolean)} to change it.
* </p>
*
* @param errorMessage
* the message to display in case the value does not validate.
* @param minValue
* The minimum value to accept or null for no limit
* @param maxValue
* The maximum value to accept or null for no limit
*/
public DateRangeValidator(String errorMessage, LocalDate minValue,
LocalDate maxValue) {
super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2000-2017 Vaadin Ltd.
*
* 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
*
* 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 com.vaadin.data.validator;

import java.time.LocalDateTime;
import java.util.Comparator;

/**
* Validator for validating that a {@link LocalDateTime} is inside a given
* range.
*
* @author Vaadin Ltd.
* @since 8.0
*/
public class DateTimeRangeValidator extends RangeValidator<LocalDateTime> {

/**
* Creates a validator for checking that a {@link LocalDateTime} is within a
* given range.
* <p>
* By default the range is inclusive i.e. both minValue and maxValue are
* valid values. Use {@link #setMinValueIncluded(boolean)} or
* {@link #setMaxValueIncluded(boolean)} to change it.
* </p>
*
* @param errorMessage
* the message to display in case the value does not validate.
* @param minValue
* The minimum value to accept or null for no limit
* @param maxValue
* The maximum value to accept or null for no limit
*/
public DateTimeRangeValidator(String errorMessage, LocalDateTime minValue,
LocalDateTime maxValue) {
super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2017 Vaadin Ltd.
*
* 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
*
* 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 com.vaadin.data.validator;

import java.util.Comparator;

/**
* Validator for validating that a {@link Double} is inside a given range.
*
* @author Vaadin Ltd.
* @since 8.0
*/
@SuppressWarnings("serial")
public class DoubleRangeValidator extends RangeValidator<Double> {

/**
* Creates a validator for checking that an Double is within a given range.
*
* By default the range is inclusive i.e. both minValue and maxValue are
* valid values. Use {@link #setMinValueIncluded(boolean)} or
* {@link #setMaxValueIncluded(boolean)} to change it.
*
*
* @param errorMessage
* the message to display in case the value does not validate.
* @param minValue
* The minimum value to accept or null for no limit
* @param maxValue
* The maximum value to accept or null for no limit
*/
public DoubleRangeValidator(String errorMessage, Double minValue,
Double maxValue) {
super(errorMessage, Comparator.naturalOrder(), minValue, maxValue);
}

}
Loading

0 comments on commit e37833f

Please sign in to comment.