-
Notifications
You must be signed in to change notification settings - Fork 4
Encourage to avoid using (required)
field option for numeric and boolean types
#266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
doing so requires comparing it against a default value, which in case of numeric types is `0` - a perfectly valid field value
cannot be such because of its type
@dmdashenkov, PTAL. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@serhii-lekariev, please see my comments.
* | ||
* @return whether a field that is being validated can be {@code required} | ||
*/ | ||
protected abstract boolean requiredAllowed(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method name is complicated. It uses 2 past form verbs. Please think of a better name.
/** | ||
* Checks whether a field can be {@code (required) = true}. | ||
* | ||
* Validators can define that for their respective types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<p>
is missing.
return result; | ||
} | ||
|
||
private void canBeRequired() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name does not reflect the action. I'd expect a method starting with can
to return a boolean
. Please rename.
*/ | ||
@SuppressWarnings("ConstantConditions") | ||
// `isFirstTimeSet` controls for `null`s. | ||
// `isFirstTimeSet` controls for `null`s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please undo this change.
* <p>If the field is repeated, it must have at least one value set, and all its values | ||
* must be valid. | ||
* | ||
* <p>If the field is of a numeric type, it's always considered set. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about bool
s?
|
||
@Override | ||
protected boolean requiredAllowed() { | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the overloads of this method return a constant. Please consider passing the value in a super(...)
ctor call.
* @implNote Attempting to define whether a numeric field is set or not is semantically | ||
* incorrect, because the way to define whether a value is set or not is to compare it against | ||
* its default value, which for numeric fields is {@code 0}, which is sometimes a desired and | ||
* valid value for a field. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one sentence is too long. Please split it up and simplify. Also, I'm not sure if <p>
paragraphs are supported in @implNote
.
# Conflicts: # base/src/main/java/io/spine/validate/FieldValidator.java
from abstract methods to a field initialized in the constructor
Codecov Report
@@ Coverage Diff @@
## master #266 +/- ##
============================================
+ Coverage 74.74% 74.76% +0.02%
Complexity 689 689
============================================
Files 302 302
Lines 8236 8240 +4
Branches 551 551
============================================
+ Hits 6156 6161 +5
Misses 1944 1944
+ Partials 136 135 -1 |
`validateOwnRules` method, since it's already done in the `FieldValidator#validate()`
@dmdashenkov, PTAL. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@serhii-lekariev, please see some new comments.
private void checkCanBeRequired() { | ||
boolean fieldIsRequired = isRequiredField(); | ||
if (!canBeRequired && fieldIsRequired) { | ||
String messageFormat = "Fields of type %s can't be declared as `(required)`."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String messageFormat = "Fields of type %s can't be declared as `(required)`."; | |
String messageFormat = "Fields of type %s shouldn't be declared as `(required)`."; |
FloatFieldValidatorTest() { | ||
super(HALF, -HALF, new FloatFieldValidator(FieldValue.of(HALF, fieldContext))); | ||
super(HALF, -HALF, new FloatFieldValidator(FieldValue.of(HALF, fieldContext)), | ||
requiredFieldValidator()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please allocate each next param on a new line.
.descriptor() | ||
.getType() | ||
.name(); | ||
log().warn(messageFormat, typeName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Logging._warn()
and similar methods instead.
String typeName = this.field() | ||
.descriptor() | ||
.getType() | ||
.name(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ugly. Please use FieldDeclaration
methods. If there is no such method, please add one.
@dmdashenkov, PTAL again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@serhii-lekariev, LGTM after a single comment is addressed.
if (isRequiredField()) { | ||
log().warn("'required' option not allowed for boolean field"); | ||
} | ||
// NOP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please document the reason.
This PR closes #197.
Before, attempting to use
(required) = true
on all numeric types and the boolean type resulted in false-positive violations ofValidatingException
- the numeric fields were compared against0
and boolean fields were compared againstfalse
, and if they were equal to those default values, the fields were considered unset.This is semantically incorrect, because both
0
andfalse
, while being a default, are valid values nonetheless.Now, when trying to validate
required
fields of the following numeric types:int32
int64
float
double
or a
required
field of typebool
, a warning is produced, telling that doing so is incorrect.