Skip to content

Commit

Permalink
Add support for minItems and maxItems (fabric8io#5836)
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo42 committed Oct 12, 2024
1 parent 6da44b8 commit eacef8b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ class PropertyMetadata {
private String pattern;
private Long minLength;
private Long maxLength;
private Long minItems;
private Long maxItems;
private boolean nullable;
private String format;
private List<V> validationRules = new ArrayList<>();
Expand All @@ -244,26 +246,48 @@ public PropertyMetadata(JsonSchema value, BeanProperty beanProperty) {
}

if (value.isStringSchema()) {
System.out.println("String schema");
StringSchema stringSchema = value.asStringSchema();
// only set if ValidationSchemaFactoryWrapper is used
this.pattern = stringSchema.getPattern();

this.maxLength = ofNullable(stringSchema.getMaxLength())
pattern = ofNullable(stringSchema.getPattern())
.or(() -> ofNullable(beanProperty.getAnnotation(Pattern.class)).map(Pattern::value))
.orElse(null);

maxLength = ofNullable(stringSchema.getMaxLength())
.map(Integer::longValue)
.or(() -> ofNullable(beanProperty.getAnnotation(Size.class))
.map(Size::max)
.filter(v -> v < Long.MAX_VALUE))
.orElse(null);
this.minLength = ofNullable(stringSchema.getMinLength())
minLength = ofNullable(stringSchema.getMinLength())
.map(Integer::longValue)
.or(() -> ofNullable(beanProperty.getAnnotation(Size.class))
.map(Size::min)
.filter(v -> v > 0))
.orElse(null);

} else {
// TODO: process the other schema types for validation values
} else if(value.isNumberSchema() || value.isIntegerSchema()){
// minimum and maximum are only allowed on number and integer types
ofNullable(beanProperty.getAnnotation(Max.class)).ifPresent(a -> {
max = a.value();
if (!a.inclusive()) {
exclusiveMaximum = true;
}
});
ofNullable(beanProperty.getAnnotation(Min.class)).ifPresent(a -> {
min = a.value();
if (!a.inclusive()) {
exclusiveMinimum = true;
}
});
} else if (value.isArraySchema()) {
maxItems = ofNullable(beanProperty.getAnnotation(Size.class))
.map(Size::max)
.filter(v -> v < Long.MAX_VALUE)
.orElse(null);
minItems = ofNullable(beanProperty.getAnnotation(Size.class))
.map(Size::min)
.filter(v -> v > 0)
.orElse(null);
}

collectValidationRules(beanProperty, validationRules);
Expand All @@ -272,23 +296,9 @@ public PropertyMetadata(JsonSchema value, BeanProperty beanProperty) {
// see ValidationSchemaFactoryWrapper
nullable = beanProperty.getAnnotation(Nullable.class) != null;

ofNullable(beanProperty.getAnnotation(Max.class)).ifPresent(a -> {
max = a.value();
if (!a.inclusive()) {
exclusiveMaximum = true;
}
});
ofNullable(beanProperty.getAnnotation(Min.class)).ifPresent(a -> {
min = a.value();
if (!a.inclusive()) {
exclusiveMinimum = true;
}
});

// TODO: should the following be deprecated?
required = beanProperty.getAnnotation(Required.class) != null;
defaultValue = ofNullable(beanProperty.getAnnotation(Default.class)).map(Default::value).orElse(defaultValue);
pattern = ofNullable(beanProperty.getAnnotation(Pattern.class)).map(Pattern::value).orElse(pattern);
}

public void updateSchema(T schema) {
Expand All @@ -312,6 +322,9 @@ public void updateSchema(T schema) {
schema.setMinLength(minLength);
schema.setMaxLength(maxLength);

schema.setMinItems(minItems);
schema.setMaxItems(maxItems);

schema.setPattern(pattern);
schema.setFormat(format);
if (preserveUnknownFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public interface KubernetesJSONSchemaProps {

void setMaxLength(Long max);

void setMinItems(Long min);

void setMaxItems(Long max);

void setPattern(String pattern);

void setFormat(String format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.fabric8.generator.annotation.Size;
import lombok.Data;

import java.util.List;

@Data
public class ValidationSpec {

Expand All @@ -33,6 +35,7 @@ public class ValidationSpec {
private ValidationOnDouble onDouble;
private ValidationOnDoublePrim onDoublePrim;
private ValidationOnString onString;
private ValidationOnList onList;

@Data
static class ValidationOnInteger {
Expand Down Expand Up @@ -191,4 +194,14 @@ static class ValidationOnString {
private String minLength1maxLength3;
}

@Data
static class ValidationOnList {
@Size(min = 1)
private List<String> minItems1;
@Size(max = 1)
private List<String> maxItems1;
@Size(min = 1, max = 3)
private List<String> minItems1maxItems3;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ spec:
minimum: 1.0
type: "integer"
type: "object"
onList:
properties:
maxItems1:
items:
type: "string"
maxItems: 1
type: "array"
minItems1:
items:
type: "string"
minItems: 1
type: "array"
minItems1maxItems3:
items:
type: "string"
maxItems: 3
minItems: 1
type: "array"
type: "object"
onLong:
properties:
maximum1:
Expand Down

0 comments on commit eacef8b

Please sign in to comment.