Skip to content

Commit

Permalink
fix: getOrCreateProperty method now takes into account also ofType
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Jun 18, 2024
1 parent c84ce50 commit c95953f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
103 changes: 55 additions & 48 deletions engine/src/main/java/com/arcadedb/database/DocumentValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ public static void validate(final MutableDocument document) throws ValidationExc

public static void validateField(final MutableDocument document, final Property p) throws ValidationException {
if (p.isMandatory() && !document.has(p.getName()))
throwValidationException(p, "is mandatory, but not found on record: " + document);
throwValidationException(document.getType(), p, "is mandatory, but not found on record: " + document);

final Object fieldValue = document.get(p.getName());

if (fieldValue == null) {
if (p.isNotNull() && document.has(p.getName()))
// NULLITY
throwValidationException(p, "cannot be null, record: " + document);
throwValidationException(document.getType(), p, "cannot be null, record: " + document);
} else {
if (p.getRegexp() != null)
// REGEXP
if (!(fieldValue.toString()).matches(p.getRegexp()))
throwValidationException(p,
throwValidationException(document.getType(), p,
"does not match the regular expression '" + p.getRegexp() + "'. Field value is: " + fieldValue + ", record: "
+ document);

Expand All @@ -65,13 +65,14 @@ public static void validateField(final MutableDocument document, final Property
switch (propertyType) {
case LINK: {
if (fieldValue instanceof EmbeddedDocument)
throwValidationException(p, "has been declared as LINK but an EMBEDDED document is used. Value: " + fieldValue);
throwValidationException(document.getType(), p,
"has been declared as LINK but an EMBEDDED document is used. Value: " + fieldValue);

if (ofType != null) {
final RID rid = ((Identifiable) fieldValue).getIdentity();
final DocumentType embSchemaType = document.getDatabase().getSchema().getTypeByBucketId(rid.getBucketId());
if (!embSchemaType.instanceOf(ofType))
throwValidationException(p,
throwValidationException(document.getType(), p,
"has been declared as LINK of '" + ofType + "' but a link to type '" + embSchemaType + "' is used. Value: "
+ fieldValue);
}
Expand All @@ -80,12 +81,13 @@ public static void validateField(final MutableDocument document, final Property

case EMBEDDED: {
if (!(fieldValue instanceof EmbeddedDocument))
throwValidationException(p, "has been declared as EMBEDDED but an incompatible type is used. Value: " + fieldValue);
throwValidationException(document.getType(), p,
"has been declared as EMBEDDED but an incompatible type is used. Value: " + fieldValue);

if (ofType != null) {
final DocumentType embSchemaType = ((EmbeddedDocument) fieldValue).getType();
if (!embSchemaType.instanceOf(ofType))
throwValidationException(p,
throwValidationException(document.getType(), p,
"has been declared as EMBEDDED of '" + ofType + "' but a document of type '" + embSchemaType
+ "' is used. Value: " + fieldValue);
}
Expand All @@ -96,27 +98,30 @@ public static void validateField(final MutableDocument document, final Property

case LIST: {
if (!(fieldValue instanceof List))
throwValidationException(p, "has been declared as LIST but an incompatible type is used. Value: " + fieldValue);
throwValidationException(document.getType(), p,
"has been declared as LIST but an incompatible type is used. Value: " + fieldValue);

final Type embType = ofType != null ? Type.getTypeByName(ofType) : null;

for (final Object item : ((List<?>) fieldValue)) {
if (ofType != null) {
if (embType != null) {
if (Type.getTypeByValue(item) != embType)
throwValidationException(p,
throwValidationException(document.getType(), p,
"has been declared as LIST of '" + ofType + "' but a value of type '" + Type.getTypeByValue(item)
+ "' is used. Value: " + fieldValue);
} else if (item instanceof EmbeddedDocument) {
if (!((EmbeddedDocument) item).getType().instanceOf(ofType))
throwValidationException(p, "has been declared as LIST of '" + ofType + "' but an embedded document of type '"
+ ((EmbeddedDocument) item).getType().getName() + "' is used. Value: " + fieldValue);
throwValidationException(document.getType(), p,
"has been declared as LIST of '" + ofType + "' but an embedded document of type '"
+ ((EmbeddedDocument) item).getType().getName() + "' is used. Value: " + fieldValue);
} else if (item instanceof Identifiable) {
final RID rid = ((Identifiable) item).getIdentity();
final DocumentType embSchemaType = document.getDatabase().getSchema().getTypeByBucketId(rid.getBucketId());
if (!embSchemaType.instanceOf(ofType))
throwValidationException(p,
"has been declared as LIST of '" + ofType + "' but a link to type '" + embSchemaType.getName() + "' is used. Value: "
throwValidationException(document.getType(), p,
"has been declared as LIST of '" + ofType + "' but a link to type '" + embSchemaType.getName()
+ "' is used. Value: "
+ fieldValue);
}
}
Expand All @@ -129,27 +134,28 @@ public static void validateField(final MutableDocument document, final Property

case MAP: {
if (!(fieldValue instanceof Map))
throwValidationException(p, "has been declared as MAP but an incompatible type is used. Value: " + fieldValue);
throwValidationException(document.getType(), p,
"has been declared as MAP but an incompatible type is used. Value: " + fieldValue);

final Type embType = ofType != null ? Type.getTypeByName(ofType) : null;

for (final Object item : ((Map<?, ?>) fieldValue).values()) {
if (ofType != null) {
if (embType != null) {
if (Type.getTypeByValue(item) != embType)
throwValidationException(p,
throwValidationException(document.getType(), p,
"has been declared as a MAP of <String,'" + ofType + "'> but a value of type '" + Type.getTypeByValue(item)
+ "' is used. Value: " + fieldValue);
} else if (item instanceof EmbeddedDocument) {
if (!((EmbeddedDocument) item).getType().instanceOf(ofType))
throwValidationException(p,
throwValidationException(document.getType(), p,
"has been declared as a MAP of <String," + ofType + "> but an embedded document of type '"
+ ((EmbeddedDocument) item).getType().getName() + "' is used. Value: " + fieldValue);
} else if (item instanceof Identifiable) {
final RID rid = ((Identifiable) item).getIdentity();
final DocumentType embSchemaType = document.getDatabase().getSchema().getTypeByBucketId(rid.getBucketId());
if (!embSchemaType.instanceOf(ofType))
throwValidationException(p,
throwValidationException(document.getType(), p,
"has been declared as a MAP of <String," + ofType + "> but a link to type '" + embSchemaType.getName()
+ "' is used. Value: " + fieldValue);
}
Expand All @@ -171,56 +177,56 @@ public static void validateField(final MutableDocument document, final Property
case LONG: {
final long minAsLong = Long.parseLong(min);
if (((Number) fieldValue).longValue() < minAsLong)
throwValidationException(p, "value " + fieldValue + " is less than " + min);
throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min);
break;
}

case INTEGER: {
final int minAsInteger = Integer.parseInt(min);
if (((Number) fieldValue).intValue() < minAsInteger)
throwValidationException(p, "value " + fieldValue + " is less than " + min);
throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min);
break;
}

case SHORT: {
final int minAsInteger = Integer.parseInt(min);
if (((Number) fieldValue).shortValue() < minAsInteger)
throwValidationException(p, "value " + fieldValue + " is less than " + min);
throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min);
break;
}

case BYTE: {
final int minAsInteger = Integer.parseInt(min);
if (((Number) fieldValue).byteValue() < minAsInteger)
throwValidationException(p, "value " + fieldValue + " is less than " + min);
throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min);
break;
}

case FLOAT: {
final float minAsFloat = Float.parseFloat(min);
if (((Number) fieldValue).floatValue() < minAsFloat)
throwValidationException(p, "value " + fieldValue + " is less than " + min);
throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min);
break;
}

case DOUBLE: {
final double minAsDouble = Double.parseDouble(min);
if (((Number) fieldValue).floatValue() < minAsDouble)
throwValidationException(p, "value " + fieldValue + " is less than " + min);
throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min);
break;
}

case DECIMAL: {
final BigDecimal minAsDecimal = new BigDecimal(min);
if (((BigDecimal) fieldValue).compareTo(minAsDecimal) < 0)
throwValidationException(p, "value " + fieldValue + " is less than " + min);
throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min);
break;
}

case STRING: {
final int minAsInteger = Integer.parseInt(min);
if (fieldValue.toString().length() < minAsInteger)
throwValidationException(p, "contains fewer characters than " + min + " requested");
throwValidationException(document.getType(), p, "contains fewer characters than " + min + " requested");
break;
}

Expand All @@ -231,7 +237,7 @@ public static void validateField(final MutableDocument document, final Property
final Date fieldValueAsDate = (Date) Type.convert(database, fieldValue, Date.class);

if (fieldValueAsDate.compareTo(minAsDate) < 0)
throwValidationException(p,
throwValidationException(document.getType(), p,
"contains the date " + fieldValue + " which precedes the first acceptable date (" + min + ")");
break;
}
Expand All @@ -240,28 +246,28 @@ public static void validateField(final MutableDocument document, final Property
final int minAsInteger = Integer.parseInt(min);
if (fieldValue instanceof Binary) {
if (((Binary) fieldValue).size() < minAsInteger)
throwValidationException(p, "contains fewer bytes than " + min + " requested");
throwValidationException(document.getType(), p, "contains fewer bytes than " + min + " requested");
} else if (((byte[]) fieldValue).length < minAsInteger)
throwValidationException(p, "contains fewer bytes than " + min + " requested");
throwValidationException(document.getType(), p, "contains fewer bytes than " + min + " requested");
break;
}

case LIST: {
final int minAsInteger = Integer.parseInt(min);
if (((Collection) fieldValue).size() < minAsInteger)
throwValidationException(p, "contains fewer items than " + min + " requested");
throwValidationException(document.getType(), p, "contains fewer items than " + min + " requested");
break;
}

case MAP: {
final int minAsInteger = Integer.parseInt(min);
if (((Map) fieldValue).size() < minAsInteger)
throwValidationException(p, "contains fewer items than " + min + " requested");
throwValidationException(document.getType(), p, "contains fewer items than " + min + " requested");
break;
}

default:
throwValidationException(p, "value " + fieldValue + " is less than " + min);
throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min);
}
}

Expand All @@ -273,56 +279,56 @@ public static void validateField(final MutableDocument document, final Property
case LONG: {
final long maxAsLong = Long.parseLong(max);
if (((Number) fieldValue).longValue() > maxAsLong)
throwValidationException(p, "value " + fieldValue + " is greater than " + max);
throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max);
break;
}

case INTEGER: {
final int maxAsInteger = Integer.parseInt(max);
if (((Number) fieldValue).intValue() > maxAsInteger)
throwValidationException(p, "value " + fieldValue + " is greater than " + max);
throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max);
break;
}

case SHORT: {
final int maxAsInteger = Integer.parseInt(max);
if (((Number) fieldValue).shortValue() > maxAsInteger)
throwValidationException(p, "value " + fieldValue + " is greater than " + max);
throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max);
break;
}

case BYTE: {
final int maxAsInteger = Integer.parseInt(max);
if (((Number) fieldValue).byteValue() > maxAsInteger)
throwValidationException(p, "value " + fieldValue + " is greater than " + max);
throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max);
break;
}

case FLOAT: {
final float maxAsFloat = Float.parseFloat(max);
if (((Number) fieldValue).floatValue() > maxAsFloat)
throwValidationException(p, "value " + fieldValue + " is greater than " + max);
throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max);
break;
}

case DOUBLE: {
final double maxAsDouble = Double.parseDouble(max);
if (((Number) fieldValue).floatValue() > maxAsDouble)
throwValidationException(p, "value " + fieldValue + " is greater than " + max);
throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max);
break;
}

case DECIMAL: {
final BigDecimal maxAsDecimal = new BigDecimal(max);
if (((BigDecimal) fieldValue).compareTo(maxAsDecimal) > 0)
throwValidationException(p, "value " + fieldValue + " is greater than " + max);
throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max);
break;
}

case STRING: {
final int maxAsInteger = Integer.parseInt(max);
if (fieldValue.toString().length() > maxAsInteger)
throwValidationException(p, "contains more characters than " + max + " requested");
throwValidationException(document.getType(), p, "contains more characters than " + max + " requested");
break;
}

Expand All @@ -333,7 +339,7 @@ public static void validateField(final MutableDocument document, final Property
final Date fieldValueAsDate = (Date) Type.convert(database, fieldValue, Date.class);

if (fieldValueAsDate.compareTo(maxAsDate) > 0)
throwValidationException(p,
throwValidationException(document.getType(), p,
"contains the date " + fieldValue + " which is after the last acceptable date (" + max + ")");
break;
}
Expand All @@ -342,28 +348,28 @@ public static void validateField(final MutableDocument document, final Property
final int maxAsInteger = Integer.parseInt(max);
if (fieldValue instanceof Binary) {
if (((Binary) fieldValue).size() > maxAsInteger)
throwValidationException(p, "contains more bytes than " + max + " requested");
throwValidationException(document.getType(), p, "contains more bytes than " + max + " requested");
} else if (((byte[]) fieldValue).length > maxAsInteger)
throwValidationException(p, "contains more bytes than " + max + " requested");
throwValidationException(document.getType(), p, "contains more bytes than " + max + " requested");
break;
}

case LIST: {
final int maxAsInteger = Integer.parseInt(max);
if (((Collection) fieldValue).size() > maxAsInteger)
throwValidationException(p, "contains more items than " + max + " requested");
throwValidationException(document.getType(), p, "contains more items than " + max + " requested");
break;
}

case MAP: {
final int maxAsInteger = Integer.parseInt(max);
if (((Map) fieldValue).size() > maxAsInteger)
throwValidationException(p, "contains more items than " + max + " requested");
throwValidationException(document.getType(), p, "contains more items than " + max + " requested");
break;
}

default:
throwValidationException(p, "value " + fieldValue + " is greater than " + max);
throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max);
}
}
}
Expand All @@ -373,12 +379,13 @@ public static void validateField(final MutableDocument document, final Property
final Document originalDocument = ((LocalDatabase) document.getDatabase()).getOriginalDocument(document);
final Object originalFieldValue = originalDocument.get(p.getName());
if (!Objects.equals(fieldValue, originalFieldValue))
throwValidationException(p, "is immutable and cannot be altered. Field value is: " + fieldValue);
throwValidationException(document.getType(), p, "is immutable and cannot be altered. Field value is: " + fieldValue);
}
}
}

private static void throwValidationException(final Property p, final String message) throws ValidationException {
throw new ValidationException("The property '" + p.getName() + "' " + message);
private static void throwValidationException(final DocumentType type, final Property p, final String message)
throws ValidationException {
throw new ValidationException("The property '" + type.getName() + "." + p.getName() + "' " + message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public Property getOrCreateProperty(final String propertyName, final Type proper
public Property getOrCreateProperty(final String propertyName, final Type propertyType, final String ofType) {
final Property p = getPolymorphicPropertyIfExists(propertyName);
if (p != null) {
if (p.getType().equals(propertyType))
if (p.getType().equals(propertyType) && Objects.equals(p.getOfType(), ofType))
return p;

// DIFFERENT TYPE: DROP THE PROPERTY AND CREATE A NEW ONE
Expand Down

0 comments on commit c95953f

Please sign in to comment.