Skip to content

Commit

Permalink
Removed min and max on number
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanSusana committed Jul 31, 2024
1 parent 3c72437 commit 5b6c5ad
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 49 deletions.
11 changes: 0 additions & 11 deletions core/src/main/java/com/elepy/annotations/Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,4 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE_USE})
public @interface Number {
/**
* @deprecated use Java Bean Validation instead
*/
@Deprecated(forRemoval = true)
float minimum() default Integer.MIN_VALUE;

/**
* @deprecated use Java Bean Validation instead
*/
@Deprecated(forRemoval = true)
float maximum() default Integer.MAX_VALUE;
}
16 changes: 1 addition & 15 deletions core/src/main/java/com/elepy/models/options/NumberOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,19 @@
import java.lang.reflect.AnnotatedElement;

public class NumberOptions implements Options {
private final float minimum;
private final float maximum;
private final NumberType numberType;

public NumberOptions(float minimum, float maximum, NumberType numberType) {
this.minimum = minimum;
this.maximum = maximum;
public NumberOptions(NumberType numberType) {
this.numberType = numberType;
}

public static NumberOptions of(AnnotatedElement field) {
final Number annotation = Annotations.get(field, Number.class);
return new NumberOptions(
annotation == null ? Integer.MIN_VALUE : annotation.minimum(),
annotation == null ? Integer.MAX_VALUE : annotation.maximum(),
NumberType.guessType(ReflectionUtils.returnTypeOf(field)
));
}

public float getMinimum() {
return minimum;
}

public float getMaximum() {
return maximum;
}

public NumberType getNumberType() {
return numberType;
}
Expand Down
12 changes: 9 additions & 3 deletions core/src/test/java/com/elepy/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.elepy.models.TextType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;

import java.math.BigDecimal;
Expand Down Expand Up @@ -50,13 +52,17 @@ public class Resource {
@TextArea
private String minLen10MaxLen50;

@Number(minimum = 20)
@Number()
@Min(20)
private BigDecimal numberMin20;

@Number(maximum = 40)
@Number()
@Max(40)
private BigDecimal numberMax40;

@Number(minimum = 10, maximum = 50)
@Number()
@Min(10)
@Max(50)
private BigDecimal numberMin10Max50;

@Searchable
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/com/elepy/ResourceArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ResourceArray {
@Size(max = 2, min = 1)
private List<@Size(min = 10) String> arrayStringMax2Min1TextWithMinimumLengthOf10;

private List<@Number(minimum = 10) Integer> arrayNumberMax2Min1NumberWithMinimumOf10;
private List<@Number Integer> arrayNumberMax2Min1NumberWithMinimumOf10;


public List<String> getArrayFileReference() {
Expand Down
3 changes: 0 additions & 3 deletions core/src/test/java/com/elepy/describers/SchemaUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ void testCorrectNumber() {
final Schema<Resource> schemaFromClass = new SchemaFactory().createDeepSchema(Resource.class);

final Property property = schemaFromClass.getProperty("numberMin10Max50");
final NumberOptions of = property.getOptions();

assertThat(of.getMinimum()).isEqualTo(10);
assertThat(of.getMaximum()).isEqualTo(50);
assertThat(property.getType())
.isEqualTo(NUMBER);
}
Expand Down
4 changes: 2 additions & 2 deletions docs/annotations/field/number.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# @Number
This annotation signifies that a field is a number. It comes with three optional properties, minimum, maximum and value(NumberType). The minimum and maximum properties get asserted by the `ObjectEvaluator'
This annotation signifies that a field is a number.

_Example_
```java
@Number(minimum = 0, maximum = 1000, value = NumberType.DECIMAL)
@Number(value = NumberType.DECIMAL)
private BigDecimal price;
```
__*Can only be used on Fields that extend `java.lang.Number`__
8 changes: 5 additions & 3 deletions docs/guides/custom-functionality.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ public class Product {
private String name;

@Label("Product Price")
@Number(minimum = 0)
@Number
private BigDecimal price;

@Label("Amount of stock left")
@Number(minimum = 0)
@Number
@Min(0)
private int stockLeft;

@Label("Amount sold")
@Number(minimum = 0)
@Number
@Min(0)
private int amountSold;

// Getters and Setters
Expand Down
6 changes: 4 additions & 2 deletions docs/main/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ public class Product {
@Unique
private String name;

@Number(minimum = 0)
@Number
@Min(0)
private BigDecimal price;

@Number(minimum = 0)
@Number
@Min(0)
private int stockLeft;

//Getters and Setters. I like to use Lombok to automate this :D
Expand Down
11 changes: 8 additions & 3 deletions hibernate/src/test/java/com/elepy/hibernate/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.elepy.annotations.Number;
import com.elepy.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;

import javax.persistence.Entity;
Expand Down Expand Up @@ -42,13 +44,16 @@ public class Resource {
@Size(min = 10, max = 50)
private String minLen10MaxLen50;

@com.elepy.annotations.Number(minimum = 20)
@com.elepy.annotations.Number()
@Min(20)
private BigDecimal numberMin20;

@com.elepy.annotations.Number(maximum = 40)
@com.elepy.annotations.Number()
@Max(40)
private BigDecimal numberMax40;

@Number(minimum = 10, maximum = 50)
@Number()
@Min(10)@Max(50)
private BigDecimal numberMin10Max50;

@Searchable
Expand Down
11 changes: 8 additions & 3 deletions mongo/src/test/java/com/elepy/mongo/fast/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.elepy.mongo.annotations.MongoIndex;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;

import java.math.BigDecimal;
Expand Down Expand Up @@ -46,13 +48,16 @@ public class Resource {
@Size(min = 10, max = 50)
private String minLen10MaxLen50;

@Number(minimum = 20)
@Number
@Min(20)
private BigDecimal numberMin20;

@Number(maximum = 40)
@Number
@Max(40)
private BigDecimal numberMax40;

@Number(minimum = 10, maximum = 50)
@Number()
@Min(10)@Max(50)
private BigDecimal numberMin10Max50;

@Searchable
Expand Down
12 changes: 9 additions & 3 deletions tests/src/main/java/com/elepy/tests/basic/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.elepy.http.HttpMethod;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;

import javax.persistence.Entity;
Expand Down Expand Up @@ -52,13 +54,17 @@ public class Resource {
@Size(min = 10, max = 50)
private String minLen10MaxLen50;

@Number(minimum = 20)
@Number
@Min(20)
private BigDecimal numberMin20;

@Number(maximum = 40)
@Number
@Max(40)
private BigDecimal numberMax40;

@Number(minimum = 10, maximum = 50)
@Number()
@Min(10)
@Max(50)
private BigDecimal numberMin10Max50;

@Searchable
Expand Down

0 comments on commit 5b6c5ad

Please sign in to comment.