[FEAT] correct the output generation of the typescript ==> missing decimal min and decimal max ==> need to test pattern and email

This commit is contained in:
Edouard DUPIN 2025-01-29 00:34:49 +01:00
parent 249e6ad2c8
commit e071d3dbf7
5 changed files with 100 additions and 116 deletions

View File

@ -195,7 +195,7 @@ public class AnnotationTools {
return (DataJson) annotation[0]; return (DataJson) annotation[0];
} }
public static Checker[] getCheckers(final Field element) { public static Checker[] getConstraintsCheckers(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Checker.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Checker.class);
if (annotation.length == 0) { if (annotation.length == 0) {
return null; return null;
@ -219,20 +219,20 @@ public class AnnotationTools {
return ((DecimalMax) annotation[0]); return ((DecimalMax) annotation[0]);
} }
public static Long getConstraintsMax(final Field element) { public static Max getConstraintsMax(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Max.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Max.class);
if (annotation.length == 0) { if (annotation.length == 0) {
return null; return null;
} }
return ((Max) annotation[0]).value(); return ((Max) annotation[0]);
} }
public static Long getConstraintsMin(final Field element) { public static Min getConstraintsMin(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Min.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Min.class);
if (annotation.length == 0) { if (annotation.length == 0) {
return null; return null;
} }
return ((Min) annotation[0]).value(); return ((Min) annotation[0]);
} }
public static int getLimitSize(final Field element) { public static int getLimitSize(final Field element) {
@ -252,20 +252,20 @@ public class AnnotationTools {
return (Size) annotation[0]; return (Size) annotation[0];
} }
public static String getConstraintsPattern(final Field element) { public static Pattern getConstraintsPattern(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Pattern.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Pattern.class);
if (annotation.length == 0) { if (annotation.length == 0) {
return null; return null;
} }
return ((Pattern) annotation[0]).regexp(); return (Pattern) annotation[0];
} }
public static boolean getConstraintsEmail(final Field element) { public static Email getConstraintsEmail(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Email.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Email.class);
if (annotation.length == 0) { if (annotation.length == 0) {
return false; return null;
} }
return true; return (Email) annotation[0];
} }
public static boolean isAnnotationGroup(final Field field, final Class<?> annotationType) { public static boolean isAnnotationGroup(final Field field, final Class<?> annotationType) {

View File

@ -34,6 +34,8 @@ import org.slf4j.LoggerFactory;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.DecimalMax; import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin; import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
public class CheckJPA<T> implements CheckFunctionInterface { public class CheckJPA<T> implements CheckFunctionInterface {
@ -182,8 +184,9 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
final Long maxValue = AnnotationTools.getConstraintsMax(field); final Max maxValue = AnnotationTools.getConstraintsMax(field);
if (maxValue != null) { if (maxValue != null) {
final Long maxValueTmp = maxValue.value();
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -196,14 +199,15 @@ public class CheckJPA<T> implements CheckFunctionInterface {
return; return;
} }
final Long elemTyped = (Long) elem; final Long elemTyped = (Long) elem;
if (elemTyped > maxValue) { if (elemTyped > maxValueTmp) {
throw new InputException(baseName + fieldName, throw new InputException(baseName + fieldName,
"Value too height max: " + maxValue); "Value too height max: " + maxValueTmp);
} }
}); });
} }
final Long minValue = AnnotationTools.getConstraintsMin(field); final Min minValue = AnnotationTools.getConstraintsMin(field);
if (minValue != null) { if (minValue != null) {
final Long minValueTmp = minValue.value();
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -216,9 +220,9 @@ public class CheckJPA<T> implements CheckFunctionInterface {
return; return;
} }
final Long elemTyped = (Long) elem; final Long elemTyped = (Long) elem;
if (elemTyped < minValue) { if (elemTyped < minValueTmp) {
throw new InputException(baseName + fieldName, throw new InputException(baseName + fieldName,
"Value too Low min: " + minValue); "Value too Low min: " + minValueTmp);
} }
}); });
} }
@ -278,9 +282,9 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
final Long maxValueRoot = AnnotationTools.getConstraintsMax(field); final Max maxValueRoot = AnnotationTools.getConstraintsMax(field);
if (maxValueRoot != null) { if (maxValueRoot != null) {
final int maxValue = maxValueRoot.intValue(); final int maxValue = (int) maxValueRoot.value();
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -299,9 +303,9 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
final Long minValueRoot = AnnotationTools.getConstraintsMin(field); final Min minValueRoot = AnnotationTools.getConstraintsMin(field);
if (minValueRoot != null) { if (minValueRoot != null) {
final int minValue = minValueRoot.intValue(); final int minValue = (int) minValueRoot.value();
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -377,9 +381,9 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
final Long maxValueRoot = AnnotationTools.getConstraintsMax(field); final Max maxValueRoot = AnnotationTools.getConstraintsMax(field);
if (maxValueRoot != null) { if (maxValueRoot != null) {
final float maxValue = maxValueRoot.floatValue(); final float maxValue = maxValueRoot.value();
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -399,9 +403,9 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
final Long minValueRoot = AnnotationTools.getConstraintsMin(field); final Min minValueRoot = AnnotationTools.getConstraintsMin(field);
if (minValueRoot != null) { if (minValueRoot != null) {
final float minValue = minValueRoot.floatValue(); final float minValue = minValueRoot.value();
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -475,9 +479,9 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
final Long maxValueRoot = AnnotationTools.getConstraintsMax(field); final Max maxValueRoot = AnnotationTools.getConstraintsMax(field);
if (maxValueRoot != null) { if (maxValueRoot != null) {
final double maxValue = maxValueRoot.doubleValue(); final double maxValue = maxValueRoot.value();
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -496,9 +500,9 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
final Long minValueRoot = AnnotationTools.getConstraintsMin(field); final Min minValueRoot = AnnotationTools.getConstraintsMin(field);
if (minValueRoot != null) { if (minValueRoot != null) {
final double minValue = minValueRoot.doubleValue(); final double minValue = minValueRoot.value();
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -524,26 +528,6 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} else if (type == LocalTime.class) { } else if (type == LocalTime.class) {
} else if (type == String.class) { } else if (type == String.class) {
final int maxSizeString = AnnotationTools.getLimitSize(field);
if (maxSizeString > 0) {
add(fieldName,
(
final DBAccess ioDb,
final String baseName,
final T data,
final List<String> modifiedValue,
final QueryOptions options) -> {
final Object elem = field.get(data);
if (elem == null) {
return;
}
final String elemTyped = (String) elem;
if (elemTyped.length() > maxSizeString) {
throw new InputException(baseName + fieldName,
"Too long size must be <= " + maxSizeString);
}
});
}
final Size limitSize = AnnotationTools.getConstraintsSize(field); final Size limitSize = AnnotationTools.getConstraintsSize(field);
if (limitSize != null) { if (limitSize != null) {
add(fieldName, add(fieldName,
@ -568,9 +552,10 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
final String patternString = AnnotationTools.getConstraintsPattern(field); final jakarta.validation.constraints.Pattern patternString = AnnotationTools
if (patternString != null) { .getConstraintsPattern(field);
final Pattern pattern = Pattern.compile(patternString); if (patternString != null && patternString.regexp() != null) {
final Pattern pattern = Pattern.compile(patternString.regexp());
add(fieldName, add(fieldName,
( (
final DBAccess ioDb, final DBAccess ioDb,
@ -585,12 +570,12 @@ public class CheckJPA<T> implements CheckFunctionInterface {
final String elemTyped = (String) elem; final String elemTyped = (String) elem;
if (!pattern.matcher(elemTyped).find()) { if (!pattern.matcher(elemTyped).find()) {
throw new InputException(baseName + fieldName, throw new InputException(baseName + fieldName,
"does not match the required pattern (constraints) must be '" "does not match the required pattern (constraints) must be '" + pattern
+ patternString + "'"); + "'");
} }
}); });
} }
if (AnnotationTools.getConstraintsEmail(field)) { if (AnnotationTools.getConstraintsEmail(field) != null) {
final String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; final String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
final Pattern pattern = Pattern.compile(emailPattern); final Pattern pattern = Pattern.compile(emailPattern);
add(fieldName, add(fieldName,
@ -615,7 +600,7 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} else if (type.isEnum()) { } else if (type.isEnum()) {
// nothing to do. // nothing to do.
} }
final Checker[] checkers = AnnotationTools.getCheckers(field); final Checker[] checkers = AnnotationTools.getConstraintsCheckers(field);
if (checkers != null) { if (checkers != null) {
for (final Checker checker : checkers) { for (final Checker checker : checkers) {
if (checker == null || checker.value() == CheckFunctionVoid.class) { if (checker == null || checker.value() == CheckFunctionVoid.class) {

View File

@ -151,38 +151,6 @@ public class DotClassElement {
return ".optional()"; return ".optional()";
} }
public String maxSizeZod(final FieldProperty field) {
final StringBuilder builder = new StringBuilder();
final Class<?> clazz = field.model().getOriginClasses();
if (clazz == String.class) {
if (field.sizeMin() > 0) {
builder.append(".min(");
builder.append(field.sizeMin());
builder.append(")");
}
if (field.sizeMax() > 0) {
builder.append(".max(");
builder.append(field.sizeMax());
builder.append(")");
}
}
if (clazz == short.class || clazz == Short.class || clazz == int.class || clazz == Integer.class
|| clazz == long.class || clazz == Long.class || clazz == float.class || clazz == Float.class
|| clazz == double.class || clazz == Double.class) {
if (field.min() != null && field.min() > 0) {
builder.append(".min(");
builder.append(field.min());
builder.append(")");
}
if (field.max() != null && field.max() > 0) {
builder.append(".max(");
builder.append(field.max());
builder.append(")");
}
}
return builder.toString();
}
public String readOnlyZod(final FieldProperty field) { public String readOnlyZod(final FieldProperty field) {
if (field.readOnly()) { if (field.readOnly()) {
return ".readonly()"; return ".readonly()";

View File

@ -18,6 +18,12 @@ import org.slf4j.LoggerFactory;
import jakarta.persistence.ManyToMany; import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany; import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
public class ClassObjectModel extends ClassModel { public class ClassObjectModel extends ClassModel {
@ -61,24 +67,31 @@ public class ClassObjectModel extends ClassModel {
ClassModel model, ClassModel model,
ClassModel linkClass, // link class when use remote ID (ex: list<UUID>) ClassModel linkClass, // link class when use remote ID (ex: list<UUID>)
String comment, String comment,
int sizeMin, // String SizeMin Size stringSize, // String Size
int sizeMax, // String SizeMax Min min, // number min value
Long min, // number min value Max max, // number max value
Long max, // number max value DecimalMin decimalMin,
DecimalMax decimalMax,
Pattern pattern,
Email email,
Boolean readOnly, Boolean readOnly,
Boolean notNull, Boolean notNull,
Boolean columnNotNull, Boolean columnNotNull,
Boolean nullable) { Boolean nullable) {
public FieldProperty(final String name, final ClassModel model, final ClassModel linkClass, public FieldProperty(final String name, final ClassModel model, final ClassModel linkClass,
final String comment, final int sizeMin, final int sizeMax, final Long min, final Long max, final String comment, final Size stringSize, final Min min, final Max max, final DecimalMin decimalMin,
final Boolean readOnly, final Boolean notNull, final Boolean columnNotNull, final Boolean nullable) { final DecimalMax decimalMax, final Pattern pattern, final Email email, final Boolean readOnly,
final Boolean notNull, final Boolean columnNotNull, final Boolean nullable) {
this.name = name; this.name = name;
this.model = model; this.model = model;
this.linkClass = linkClass; this.linkClass = linkClass;
this.comment = comment; this.comment = comment;
this.sizeMin = sizeMin; this.stringSize = stringSize;
this.sizeMax = sizeMax; this.decimalMin = decimalMin;
this.decimalMax = decimalMax;
this.pattern = pattern;
this.email = email;
this.min = min; this.min = min;
this.max = max; this.max = max;
this.readOnly = readOnly; this.readOnly = readOnly;
@ -88,17 +101,6 @@ public class ClassObjectModel extends ClassModel {
} }
private static int getStringMinSize(final Field field) throws DataAccessException {
final Size size = AnnotationTools.getConstraintsSize(field);
return size != null ? size.min() : 0;
}
private static int getStringMaxSize(final Field field) throws DataAccessException {
final Size size = AnnotationTools.getConstraintsSize(field);
final int colomnLimitSize = AnnotationTools.getLimitSize(field);
return size == null ? colomnLimitSize : colomnLimitSize < size.max() ? colomnLimitSize : size.max();
}
private static Class<?> getSubModelIfExist2(final Field field) { private static Class<?> getSubModelIfExist2(final Field field) {
final ManyToOne manyToOne = AnnotationTools.getManyToOne(field); final ManyToOne manyToOne = AnnotationTools.getManyToOne(field);
if (manyToOne != null) { if (manyToOne != null) {
@ -137,10 +139,13 @@ public class ClassObjectModel extends ClassModel {
ClassModel.getModel(field.getGenericType(), previous), // ClassModel.getModel(field.getGenericType(), previous), //
getSubModelIfExist(field, previous), // getSubModelIfExist(field, previous), //
AnnotationTools.getSchemaDescription(field), // AnnotationTools.getSchemaDescription(field), //
getStringMinSize(field), // AnnotationTools.getConstraintsSize(field), //
getStringMaxSize(field), //
AnnotationTools.getConstraintsMin(field), // AnnotationTools.getConstraintsMin(field), //
AnnotationTools.getConstraintsMax(field), // AnnotationTools.getConstraintsMax(field), //
AnnotationTools.getConstraintsDecimalMin(field), //
AnnotationTools.getConstraintsDecimalMax(field), //
AnnotationTools.getConstraintsPattern(field), //
AnnotationTools.getConstraintsEmail(field), //
AnnotationTools.getSchemaReadOnly(field), // AnnotationTools.getSchemaReadOnly(field), //
AnnotationTools.getConstraintsNotNull(field), // AnnotationTools.getConstraintsNotNull(field), //
AnnotationTools.getColumnNotNull(field), // AnnotationTools.getColumnNotNull(field), //

View File

@ -259,30 +259,56 @@ public class TsClassElement {
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
final Class<?> clazz = field.model().getOriginClasses(); final Class<?> clazz = field.model().getOriginClasses();
if (clazz == String.class) { if (clazz == String.class) {
if (field.sizeMin() > 0) { if (field.stringSize() != null) {
builder.append(".min("); builder.append(".min(");
builder.append(field.sizeMin()); builder.append(field.stringSize().min());
builder.append(")"); builder.append(")");
} }
if (field.sizeMax() > 0) { if (field.stringSize() != null) {
builder.append(".max("); builder.append(".max(");
builder.append(field.sizeMax()); builder.append(field.stringSize().max());
builder.append(")"); builder.append(")");
} }
/*Must be tested before
if (field.pattern() != null) {
builder.append(".regex((");
builder.append(field.pattern().regexp());
builder.append(")");
}*/
/*Must be tested before
if (field.email() != null) {
builder.append(".regex((");
builder.append(field.email().regexp());
builder.append(")");
}*/
} }
if (clazz == short.class || clazz == Short.class || clazz == int.class || clazz == Integer.class if (clazz == short.class || clazz == Short.class || clazz == int.class || clazz == Integer.class
|| clazz == long.class || clazz == Long.class || clazz == float.class || clazz == Float.class || clazz == long.class || clazz == Long.class || clazz == float.class || clazz == Float.class
|| clazz == double.class || clazz == Double.class) { || clazz == double.class || clazz == Double.class) {
if (field.min() != null && field.min() > 0) { if (field.min() != null) {
builder.append(".min("); builder.append(".min(");
builder.append(field.min()); builder.append(field.min().value());
builder.append(")"); builder.append(")");
} }
if (field.max() != null && field.max() > 0) { if (field.max() != null) {
builder.append(".max("); builder.append(".max(");
builder.append(field.max()); builder.append(field.max().value());
builder.append(")"); builder.append(")");
} }
if (field.decimalMax() != null) {
builder.append(".max(");
builder.append(field.decimalMax().value());
builder.append(", { inclusive: ");
builder.append(field.decimalMax().inclusive() ? "true" : "false");
builder.append("})");
}
if (field.decimalMin() != null) {
builder.append(".min(");
builder.append(field.decimalMin().value());
builder.append(", { inclusive: ");
builder.append(field.decimalMin().inclusive() ? "true" : "false");
builder.append("})");
}
} }
return builder.toString(); return builder.toString();
} }