org.jarbframework.constraint.validation.NotNullConstraintValidationStep Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jarb-constraints Show documentation
Show all versions of jarb-constraints Show documentation
Module for handling database constraints
The newest version!
package org.jarbframework.constraint.validation;
import org.jarbframework.constraint.metadata.DatabaseGenerated;
import org.jarbframework.constraint.metadata.database.ColumnMetadata;
import org.jarbframework.utils.bean.Annotations;
import org.jarbframework.utils.bean.PropertyReference;
public class NotNullConstraintValidationStep implements DatabaseConstraintValidationStep {
private static final String NOT_NULL_VIOLATION_TEMPLATE = "{javax.validation.constraints.NotNull.message}";
@Override
public void validate(Object value, PropertyReference reference, ColumnMetadata metadata, DatabaseValidationContext context) {
if (value == null && isValueExpected(reference, metadata)) {
context.buildViolationWithTemplate(reference, NOT_NULL_VIOLATION_TEMPLATE).addToContext();
}
}
private boolean isValueExpected(PropertyReference reference, ColumnMetadata metadata) {
return metadata.isRequired() && ! isGeneratable(reference, metadata);
}
private boolean isGeneratable(PropertyReference reference, ColumnMetadata metadata) {
return metadata.isGeneratable() || Annotations.hasAnnotation(reference, DatabaseGenerated.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy