Skip to content

Commit

Permalink
validator-core - AttributeContraints - add minLength constraints (refs
Browse files Browse the repository at this point in the history
  • Loading branch information
mborne committed Jun 30, 2021
1 parent bd618fb commit 71874dd
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ public class CoreErrorCodes {
* ErrorCode for error in Features and Attributes
*/
public static final ErrorCode ATTRIBUTE_INVALID_FORMAT = ErrorCode.valueOf("ATTRIBUTE_INVALID_FORMAT");
public static final ErrorCode ATTRIBUTE_SIZE_TOO_SHORT = ErrorCode.valueOf("ATTRIBUTE_SIZE_TOO_SHORT");
public static final ErrorCode ATTRIBUTE_SIZE_EXCEEDED = ErrorCode.valueOf("ATTRIBUTE_SIZE_EXCEEDED");

public static final ErrorCode ATTRIBUTE_INVALID_REGEXP = ErrorCode.valueOf("ATTRIBUTE_INVALID_REGEXP");
public static final ErrorCode ATTRIBUTE_UNEXPECTED_VALUE = ErrorCode.valueOf("ATTRIBUTE_UNEXPECTED_VALUE");
public static final ErrorCode ATTRIBUTE_UNEXPECTED_NULL = ErrorCode.valueOf("ATTRIBUTE_UNEXPECTED_NULL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public class AttributeConstraints {
*/
private String pattern;

// TODO add private Integer minLength
/**
* Minimum length of the value
*
* @since 4.1
*/
private Integer minLength;

/**
* Maximum length of the value
Expand Down Expand Up @@ -113,6 +118,14 @@ public void setMaxLength(Integer maxLength) {
this.maxLength = maxLength;
}

public Integer getMinLength() {
return minLength;
}

public void setMinLength(Integer minLength) {
this.minLength = minLength;
}

/**
* Tell if enum restriction is defined
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.ign.validator.model.AttributeType;
import fr.ign.validator.validation.attribute.FilenameExistsValidator;
import fr.ign.validator.validation.attribute.MaxLengthValidator;
import fr.ign.validator.validation.attribute.MinLengthValidator;

/**
*
Expand All @@ -24,6 +25,7 @@ public class FilenameType extends AttributeType<File> {

public FilenameType() {
super(File.class);
addValidator(new MinLengthValidator<File>());
addValidator(new MaxLengthValidator<File>());
addValidator(new FilenameExistsValidator());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.ign.validator.model.AttributeType;
import fr.ign.validator.validation.attribute.PathExistsValidator;
import fr.ign.validator.validation.attribute.MaxLengthValidator;
import fr.ign.validator.validation.attribute.MinLengthValidator;

/**
*
Expand All @@ -22,6 +23,7 @@ public class PathType extends AttributeType<File> {

public PathType() {
super(File.class);
addValidator(new MinLengthValidator<File>());
addValidator(new MaxLengthValidator<File>());
addValidator(new PathExistsValidator());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package fr.ign.validator.model.type;

import java.io.File;

import com.fasterxml.jackson.annotation.JsonTypeName;

import fr.ign.validator.model.AttributeType;
import fr.ign.validator.validation.attribute.StringEnumValuesValidator;
import fr.ign.validator.validation.attribute.StringPatternValidator;
import fr.ign.validator.validation.attribute.MaxLengthValidator;
import fr.ign.validator.validation.attribute.MinLengthValidator;

/**
* Represents a character string
Expand All @@ -20,6 +23,7 @@ public class StringType extends AttributeType<String> {

public StringType() {
super(String.class);
addValidator(new MinLengthValidator<String>());
addValidator(new MaxLengthValidator<String>());
addValidator(new StringPatternValidator());
addValidator(new StringEnumValuesValidator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import fr.ign.validator.model.AttributeType;
import fr.ign.validator.validation.attribute.MaxLengthValidator;
import fr.ign.validator.validation.attribute.MinLengthValidator;

/**
* Location of a file
Expand All @@ -20,6 +21,7 @@ public class UrlType extends AttributeType<URL> {

public UrlType() {
super(URL.class);
addValidator(new MinLengthValidator<URL>());
addValidator(new MaxLengthValidator<URL>());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fr.ign.validator.validation.attribute;

import fr.ign.validator.Context;
import fr.ign.validator.data.Attribute;
import fr.ign.validator.error.CoreErrorCodes;
import fr.ign.validator.model.AttributeConstraints;
import fr.ign.validator.validation.Validator;

/**
*
* Validates the size of a StringType Attribute
*
* @author MBorne
*
*/
public class MinLengthValidator<T> implements Validator<Attribute<T>> {

@Override
public void validate(Context context, Attribute<T> attribute) {
T value = attribute.getBindedValue();

if (value == null) {
return;
}

AttributeConstraints constraints = attribute.getType().getConstraints();
Integer minLength = constraints.getMinLength();
if (minLength == null || minLength < 0) {
return;
}

int length = value.toString().length();
if (length < minLength) {
context.report(
context.createError(CoreErrorCodes.ATTRIBUTE_SIZE_TOO_SHORT)
.setMessageParam("VALUE_LENGTH", String.valueOf(length))
.setMessageParam("EXPECTED_LENGTH", minLength.toString())
);
}
}

}
6 changes: 6 additions & 0 deletions validator-core/src/main/resources/error-code.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@
"message": "La valeur ({VALUE}) ne correspond pas à l'expression régulière ({REGEXP}).",
"documentation": "Cette erreur se produit lorsqu'un champ contient une chaine de caractère qui ne correspond pas à la règle d'écriture du modèle."
},
{
"name": "ATTRIBUTE_SIZE_TOO_SHORT",
"level": "ERROR",
"message": "La taille de l'attribut ({VALUE_LENGTH}) est en dessous la taille minimale autorisée ({EXPECTED_LENGTH}).",
"documentation": "Cette erreur se produit lorsqu'un champ contient une valeur dont la longueur est inférieure à la taille minimale autorisée par le modèle"
},
{
"name": "ATTRIBUTE_SIZE_EXCEEDED",
"level": "ERROR",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ private void assertExceptedFeatureTypeAdresse(FeatureType featureType) {
Assert.assertFalse(
attribute.getConstraints().isUnique()
);
Assert.assertEquals(
Integer.valueOf(5),
attribute.getConstraints().getMinLength()
);
Assert.assertEquals(
Integer.valueOf(254),
attribute.getConstraints().getMaxLength()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package fr.ign.validator.validation.attribute;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import fr.ign.validator.Context;
import fr.ign.validator.data.Attribute;
import fr.ign.validator.error.ValidatorError;
import fr.ign.validator.model.type.FilenameType;
import fr.ign.validator.model.type.PathType;
import fr.ign.validator.model.type.StringType;
import fr.ign.validator.model.type.UrlType;
import fr.ign.validator.report.InMemoryReportBuilder;

public class MinLengthValidatorTest {

private Context context;

private InMemoryReportBuilder report;

@Before
public void setUp() throws Exception {
context = new Context();
report = new InMemoryReportBuilder();
context.setReportBuilder(report);
}

@Test
public void testStringTypeNoLimit() {
MinLengthValidator<String> validator = new MinLengthValidator<>();
StringType type = new StringType();
Attribute<String> attribute = new Attribute<String>(type, "abc");
validator.validate(context, attribute);
Assert.assertEquals(0, report.countErrors());
}

@Test
public void testStringTypeMoreThanLimit() {
MinLengthValidator<String> validator = new MinLengthValidator<>();
StringType type = new StringType();
type.getConstraints().setMinLength(2);
Attribute<String> attribute = new Attribute<String>(type, "abc");
validator.validate(context, attribute);
Assert.assertEquals(0, report.countErrors());
}

@Test
public void testStringTypeEqualLimit() {
MinLengthValidator<String> validator = new MinLengthValidator<>();
StringType type = new StringType();
type.getConstraints().setMinLength(5);
Attribute<String> attribute = new Attribute<String>(type, "abcde");
validator.validate(context, attribute);
Assert.assertEquals(0, report.countErrors());
}

@Test
public void testStringTypeLessThanLimit() {
MinLengthValidator<String> validator = new MinLengthValidator<>();
StringType type = new StringType();
type.getConstraints().setMinLength(5);
Attribute<String> attribute = new Attribute<String>(type, "abcd");
validator.validate(context, attribute);
Assert.assertEquals(1, report.countErrors());

ValidatorError error = report.getErrors().get(0);
Assert.assertEquals(
"La taille de l'attribut (4) est en dessous la taille minimale autorisée (5).",
error.getMessage()
);
}

@Test
public void testUrlTypeNull() {
MinLengthValidator<URL> validator = new MinLengthValidator<>();
UrlType type = new UrlType();
type.getConstraints().setMinLength(10);
Attribute<URL> attribute = new Attribute<URL>(type, null);
validator.validate(context, attribute);
Assert.assertEquals(0, report.countErrors());
}

@Test
public void testUrlTypeMoreThanLimit() throws MalformedURLException {
MinLengthValidator<URL> validator = new MinLengthValidator<>();
UrlType type = new UrlType();
type.getConstraints().setMinLength(50);
Attribute<URL> attribute = new Attribute<URL>(type, new URL("https://example.org/something"));
validator.validate(context, attribute);
Assert.assertEquals(1, report.countErrors());

ValidatorError error = report.getErrors().get(0);
Assert.assertEquals(
"La taille de l'attribut (29) est en dessous la taille minimale autorisée (50).",
error.getMessage()
);
}

@Test
public void testPathTypeNull() {
MinLengthValidator<File> validator = new MinLengthValidator<>();
PathType type = new PathType();
type.getConstraints().setMinLength(5);
Attribute<File> attribute = new Attribute<File>(type, null);
validator.validate(context, attribute);
Assert.assertEquals(0, report.countErrors());
}

@Test
public void testPathTypeLessThanLimit() {
MinLengthValidator<File> validator = new MinLengthValidator<>();
PathType type = new PathType();
type.getConstraints().setMinLength(10);
Attribute<File> attribute = new Attribute<File>(type, new File("abcd.pdf"));
validator.validate(context, attribute);
Assert.assertEquals(1, report.countErrors());

ValidatorError error = report.getErrors().get(0);
Assert.assertEquals(
"La taille de l'attribut (8) est en dessous la taille minimale autorisée (10).",
error.getMessage()
);
}

@Test
public void testFilenameTypeNull() {
MinLengthValidator<File> validator = new MinLengthValidator<>();
FilenameType type = new FilenameType();
type.getConstraints().setMinLength(5);
Attribute<File> attribute = new Attribute<File>(type, null);
validator.validate(context, attribute);
Assert.assertEquals(0, report.countErrors());
}

@Test
public void testFilenameTypeLessThanLimit() {
MinLengthValidator<File> validator = new MinLengthValidator<>();
FilenameType type = new FilenameType();
type.getConstraints().setMinLength(10);
Attribute<File> attribute = new Attribute<File>(type, new File("abcd.pdf"));
validator.validate(context, attribute);
Assert.assertEquals(1, report.countErrors());

ValidatorError error = report.getErrors().get(0);
Assert.assertEquals(
"La taille de l'attribut (8) est en dessous la taille minimale autorisée (10).",
error.getMessage()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"type": "String",
"constraints": {
"required": false,
"minLength": 5,
"maxLength": 254
}
},
Expand Down

0 comments on commit 71874dd

Please sign in to comment.