Skip to content

Commit

Permalink
add bean validation for ScaledBigDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
SirCotare committed Sep 9, 2024
1 parent f50e118 commit ecaf0f8
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package it.aboutbits.springboot.toolbox.validation.validator;

import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.Max;

public class MaxScaledBigDecimalValidator implements ConstraintValidator<Max, ScaledBigDecimal> {

private long upperBound;

@Override
public void initialize(Max constraintAnnotation) {
this.upperBound = constraintAnnotation.value();
}

@Override
public boolean isValid(ScaledBigDecimal scaledBigDecimal, ConstraintValidatorContext context) {
if (scaledBigDecimal == null) {
return true;
}

return scaledBigDecimal.compareTo(new ScaledBigDecimal(upperBound)) <= 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package it.aboutbits.springboot.toolbox.validation.validator;

import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.Min;

public class MinScaledBigDecimalValidator implements ConstraintValidator<Min, ScaledBigDecimal> {

private Long lowerBound;

@Override
public void initialize(Min constraintAnnotation) {
this.lowerBound = constraintAnnotation.value();
}

@Override
public boolean isValid(ScaledBigDecimal scaledBigDecimal, ConstraintValidatorContext context) {
if (scaledBigDecimal == null) {
return true;
}

return scaledBigDecimal.compareTo(new ScaledBigDecimal(lowerBound)) >= 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.aboutbits.springboot.toolbox.validation.validator;

import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.NegativeOrZero;

public class NegativeOrZeroScaledBigDecimalValidator implements ConstraintValidator<NegativeOrZero, ScaledBigDecimal> {
@Override
public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}

return value.compareTo(ScaledBigDecimal.ZERO) < 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.aboutbits.springboot.toolbox.validation.validator;

import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.Negative;

public class NegativeScaledBigDecimalValidator implements ConstraintValidator<Negative, ScaledBigDecimal> {
@Override
public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}

return value.compareTo(ScaledBigDecimal.ZERO) < 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.aboutbits.springboot.toolbox.validation.validator;

import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.PositiveOrZero;

public class PositiveOrZeroScaledBigDecimalValidator implements ConstraintValidator<PositiveOrZero, ScaledBigDecimal> {
@Override
public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}

return value.compareTo(ScaledBigDecimal.ZERO) > -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.aboutbits.springboot.toolbox.validation.validator;

import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.Positive;

public class PositiveScaledBigDecimalValidator implements ConstraintValidator<Positive, ScaledBigDecimal> {
@Override
public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}

return value.compareTo(ScaledBigDecimal.ZERO) > 0;
}
}
11 changes: 11 additions & 0 deletions src/main/resources/META-INF/validation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="https://jakarta.ee/xml/ns/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/validation/configuration
https://jakarta.ee/xml/ns/validation/configuration/validation-configuration-3.0.xsd"
version="3.0">
<constraint-mapping>
META-INF/validation/scaled-big-decimal.xml
</constraint-mapping>
</validation-config>
46 changes: 46 additions & 0 deletions src/main/resources/META-INF/validation/scaled-big-decimal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<constraint-mappings
xmlns="https://jakarta.ee/xml/ns/validation/mapping"
version="3.0">
<constraint-definition annotation="jakarta.validation.constraints.Max">
<validated-by include-existing-validators="true">
<value>
it.aboutbits.springboot.toolbox.validation.validator.MaxScaledBigDecimalValidator
</value>
</validated-by>
</constraint-definition>
<constraint-definition annotation="jakarta.validation.constraints.Min">
<validated-by include-existing-validators="true">
<value>
it.aboutbits.springboot.toolbox.validation.validator.MinScaledBigDecimalValidator
</value>
</validated-by>
</constraint-definition>
<constraint-definition annotation="jakarta.validation.constraints.Negative">
<validated-by include-existing-validators="true">
<value>
it.aboutbits.springboot.toolbox.validation.validator.NegativeScaledBigDecimalValidator
</value>
</validated-by>
</constraint-definition>
<constraint-definition annotation="jakarta.validation.constraints.NegativeOrZero">
<validated-by include-existing-validators="true">
<value>
it.aboutbits.springboot.toolbox.validation.validator.NegativeOrZeroScaledBigDecimalValidator
</value>
</validated-by>
</constraint-definition>
<constraint-definition annotation="jakarta.validation.constraints.Positive">
<validated-by include-existing-validators="true">
<value>
it.aboutbits.springboot.toolbox.validation.validator.PositiveScaledBigDecimalValidator
</value>
</validated-by>
</constraint-definition>
<constraint-definition annotation="jakarta.validation.constraints.PositiveOrZero">
<validated-by include-existing-validators="true">
<value>
it.aboutbits.springboot.toolbox.validation.validator.PositiveOrZeroScaledBigDecimalValidator
</value>
</validated-by>
</constraint-definition>
</constraint-mappings>

0 comments on commit ecaf0f8

Please sign in to comment.