Skip to content

Commit

Permalink
Add support for minLength and maxLength (fabric8io#5836)
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo42 committed Nov 28, 2024
1 parent 75fd0d5 commit 7a787df
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.fabric8.generator.annotation.Nullable;
import io.fabric8.generator.annotation.Pattern;
import io.fabric8.generator.annotation.Required;
import io.fabric8.generator.annotation.Size;
import io.fabric8.generator.annotation.ValidationRule;
import io.fabric8.generator.annotation.ValidationRules;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
Expand Down Expand Up @@ -234,6 +235,8 @@ class PropertyMetadata {
private Double max;
private Boolean exclusiveMaximum;
private String pattern;
private Long minLength;
private Long maxLength;
private boolean nullable;
private String format;
private List<V> validationRules = new ArrayList<>();
Expand All @@ -254,11 +257,24 @@ 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()).map(Integer::doubleValue).orElse(null);
//this.minLength = ofNullable(stringSchema.getMinLength()).map(Integer::doubleValue).orElse(null);

this.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())
.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
}
Expand Down Expand Up @@ -313,6 +329,10 @@ public void updateSchema(T schema) {
schema.setExclusiveMaximum(exclusiveMaximum);
schema.setMinimum(min);
schema.setExclusiveMinimum(exclusiveMinimum);

schema.setMinLength(minLength);
schema.setMaxLength(maxLength);

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

void setExclusiveMinimum(Boolean b);

void setMinLength(Long min);

void setMaxLength(Long max);

void setPattern(String pattern);

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

@Data
Expand Down Expand Up @@ -181,6 +182,13 @@ static class ValidationOnDoublePrim {
static class ValidationOnString {
@Pattern("(a|b)+")
private String pattern;

@Size(min = 1)
private String minLength1;
@Size(max = 1)
private String maxLength1;
@Size(min = 1, max = 3)
private String minLength1maxLength3;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ spec:
type: "object"
onString:
properties:
maxLength1:
maxLength: 1
type: "string"
minLength1:
minLength: 1
type: "string"
minLength1maxLength3:
maxLength: 3
minLength: 1
type: "string"
pattern:
pattern: "(a|b)+"
type: "string"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.fabric8.generator.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Decorates the resulting property with size limits.
* <p>
* The annotation can be used on strings, list/arrays and maps and will result in an appropriate JSON Schema constraint:
* <ul>
* <li>{@code minLength} and/or {@code maxLength} for a String</li>
* <li>{@code minItems} and/or {@code maxItems} for a list/array</li>
* <li>{@code minProperties} and/or {@code maxProperties} for a map</li>
* </ul>
* </p>
*
* @see <a href=
* "https://kubernetes.io/docs/reference/kubernetes-api/extend-resources/custom-resource-definition-v1/#JSONSchemaProps">
* Kubernetes Docs - API Reference - CRD v1 - JSONSchemaProps
* </a>
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE_USE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Size {
long min() default 0;

long max() default Long.MAX_VALUE;
}

0 comments on commit 7a787df

Please sign in to comment.