Repository navigation
Stricter Data Type checks - #674
arthurschreiber wants to merge 27 commits into
Conversation
1408a28 to
12b3629
Compare
| } | ||
|
|
||
| buffer.writeUInt8(1); | ||
| if (typeof value === 'number') { |
There was a problem hiding this comment.
Why not use the same program logic in validate(), i.e. test whether the value is already a number before using parseInt()?
And maybe use Math.round() if it's a number? Otherwise it's not backward compatible.
| buffer.writeUInt8(4); | ||
| buffer.writeUInt16LE(days); | ||
| if (!(value instanceof Date)) { | ||
| throw new TypeError(`parameter.value must be a Date, undefined or null. Received type ${typeof value}`); |
There was a problem hiding this comment.
This test should also be made in validate(), because Request.validateParameters() calls validate() and generates a better error message if the exception occurs there.
If the error occurs later in the constructor of RpcRequestPayload, the error message would not indicate the parameter name.
| buffer.writeUInt8(sign); | ||
| buffer.writeUInt64LE(decimalValue); | ||
| buffer.writeUInt32LE(0x00000000); | ||
| buffer.writeUInt32LE(0x00000000); |
There was a problem hiding this comment.
There is still an unnecessary parseFloat() in the corresponding validate() function below.
| } | ||
|
|
||
| buffer.writeUInt8(4); | ||
| if (typeof value === 'number') { |
There was a problem hiding this comment.
If it's a request parameter and the value is the number 0.000000006, the parseInt() call in the corresponding validate() function below will still convert it to 6, because in Request.validateParameters() the return value of validate() is used to replace the original parameter value.
|
I would split the program logic for each data type into the following 3 functions:
Usage:
Add an "autoconvert" option that can be used to skip the Document the behavior of the 3 functions |
|
Closing this in favor of #677. 😄 |
This pull request tightens up the type checks in the
writeParameterDatamethods across all data types that implement this method.I tried to keep backwards compatibility where possible, as I don't want this to be a breaking change. E.g. this still allows passing in strings to types like
TinyInt,SmallInt,Int,Money,Floatand so on. I'm not super happy about doing type conversion, as it can be lossy and lead to incorrect data being stored. But as changing this will be a breaking change for many users, I want to punt on that for now.I did not try to keep compatibility with object-to-value conversion (via
.valueOfmethods), as this was never an explicitly supported feature and I doubt anyone really relied on this.This change also tries to fix some cases where
tediouswas causing a loss of precision, especially with types likeFloatandReal, where passing anumbertype would always cause a conversion to astringand then back to anumberviaparseFloat. 😞This pull request also does not try to fix the schism between data type handling of plain
RequestandBulkLoad. Currently, the difference between those two in the way parameters are handled can lead to different results. This is really crazy, but changing this could also potentially be backwards incompatible.@v-suhame @chdh @atsage What do you think about these changes?