Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance decimal validation to match actual Jakarta Validation behavior and fix related issues #1131

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,92 +28,52 @@
@API(since = "0.6.8", status = Status.MAINTAINED)
public final class JavaDecimalConstraint {
@Nullable
private final BigDecimal positiveMin;
private final BigDecimal min;

@Nullable
private final Boolean positiveMinInclusive;
private final Boolean minInclusive;

@Nullable
private final BigDecimal positiveMax;
private final BigDecimal max;

@Nullable
private final Boolean positiveMaxInclusive;

@Nullable
private final BigDecimal negativeMin;

@Nullable
private final Boolean negativeMinInclusive;

@Nullable
private final BigDecimal negativeMax;

@Nullable
private final Boolean negativeMaxInclusive;
private final Boolean maxInclusive;

@Nullable
private final Integer scale;

public JavaDecimalConstraint(
@Nullable BigDecimal positiveMin,
@Nullable Boolean positiveMinInclusive,
@Nullable BigDecimal positiveMax,
@Nullable Boolean positiveMaxInclusive,
@Nullable BigDecimal negativeMin,
@Nullable Boolean negativeMinInclusive,
@Nullable BigDecimal negativeMax,
@Nullable Boolean negativeMaxInclusive,
@Nullable BigDecimal min,
@Nullable Boolean minInclusive,
@Nullable BigDecimal max,
@Nullable Boolean maxInclusive,
@Nullable Integer scale
) {
this.positiveMin = positiveMin;
this.positiveMinInclusive = positiveMinInclusive;
this.positiveMax = positiveMax;
this.positiveMaxInclusive = positiveMaxInclusive;
this.negativeMin = negativeMin;
this.negativeMinInclusive = negativeMinInclusive;
this.negativeMax = negativeMax;
this.negativeMaxInclusive = negativeMaxInclusive;
this.min = min;
this.minInclusive = minInclusive;
this.max = max;
this.maxInclusive = maxInclusive;
this.scale = scale;
}

@Nullable
public BigDecimal getPositiveMin() {
return positiveMin;
}

@Nullable
public Boolean getPositiveMinInclusive() {
return positiveMinInclusive;
}

@Nullable
public BigDecimal getPositiveMax() {
return positiveMax;
}

@Nullable
public Boolean getPositiveMaxInclusive() {
return positiveMaxInclusive;
}

@Nullable
public BigDecimal getNegativeMin() {
return negativeMin;
public BigDecimal getMin() {
return min;
}

@Nullable
public Boolean getNegativeMinInclusive() {
return negativeMinInclusive;
public Boolean getMinInclusive() {
return minInclusive;
}

@Nullable
public BigDecimal getNegativeMax() {
return negativeMax;
public BigDecimal getMax() {
return max;
}

@Nullable
public Boolean getNegativeMaxInclusive() {
return negativeMaxInclusive;
public Boolean getMaxInclusive() {
return maxInclusive;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,52 +133,22 @@ public CombinableArbitrary<?> generate(ArbitraryGeneratorContext context) {

BigDecimal value = toBigDecimal(it);

if (value.compareTo(BigDecimal.ZERO) < 0) {
if (javaDecimalConstraint.getNegativeMin() != null) {
if (value.compareTo(javaDecimalConstraint.getNegativeMin()) == 0
&& Boolean.FALSE.equals(javaDecimalConstraint.getNegativeMinInclusive())) {
return false;
}

if (value.compareTo(javaDecimalConstraint.getNegativeMin()) < 0) {
return false;
}
if (javaDecimalConstraint.getMin() != null) {
if (value.compareTo(javaDecimalConstraint.getMin()) == 0
&& Boolean.FALSE.equals(javaDecimalConstraint.getMinInclusive())) {
return false;
}

if (javaDecimalConstraint.getNegativeMax() != null) {
if (value.compareTo(javaDecimalConstraint.getNegativeMax()) == 0
&& Boolean.FALSE.equals(javaDecimalConstraint.getNegativeMaxInclusive())) {
return false;
}

if (value.compareTo(javaDecimalConstraint.getNegativeMax()) > 0) {
return false;
}
}
return value.compareTo(javaDecimalConstraint.getMin()) >= 0;
}

if (value.compareTo(BigDecimal.ZERO) > 0) {
if (javaDecimalConstraint.getPositiveMin() != null) {
if (value.compareTo(javaDecimalConstraint.getPositiveMin()) == 0
&& Boolean.FALSE.equals(javaDecimalConstraint.getPositiveMinInclusive())) {
return false;
}

if (value.compareTo(javaDecimalConstraint.getPositiveMin()) < 0) {
return false;
}
if (javaDecimalConstraint.getMax() != null) {
if (value.compareTo(javaDecimalConstraint.getMax()) == 0
&& Boolean.FALSE.equals(javaDecimalConstraint.getMaxInclusive())) {
return false;
}

if (javaDecimalConstraint.getPositiveMax() != null) {
if (value.compareTo(javaDecimalConstraint.getPositiveMax()) == 0
&& Boolean.FALSE.equals(javaDecimalConstraint.getPositiveMaxInclusive())) {
return false;
}

if (value.compareTo(javaDecimalConstraint.getPositiveMax()) > 0) {
return false;
}
}
return value.compareTo(javaDecimalConstraint.getMax()) <= 0;
}

return true;
Expand Down
Loading
Loading