allowArray flag for remoteObject accept#369
Conversation
|
@bajtos PTAL, this is so hacky 😭 |
Yeah, I agree. How about adding an Thoughts? |
a94f021 to
073b003
Compare
|
@slnode test please |
|
@bajtos PTAL again, i think this is better this time |
bajtos
left a comment
There was a problem hiding this comment.
Agreed, this second version looks much better!
| var httpFormat = o.http; | ||
| var name = o.name || o.arg; | ||
| var val; | ||
| var coercionOptions = {}; |
There was a problem hiding this comment.
I would personally prefer conversionOptions over coercionOptions, because we already use "conversion" in TypeRegistry-related code, see e.g.
strong-remoting/lib/type-registry.js
Lines 22 to 31 in 97507d2
|
|
||
| var typeConverter = ctx.typeRegistry.getConverter(o.type); | ||
| if (o.allowArray) | ||
| coercionOptions.allowArray = o.allowArray; |
There was a problem hiding this comment.
I am proposing to extract these two lines into a shared method, so that there is only one place to change if we need to add more options in the future. E.g. a static method SharedMethod.getConversionOptionsForArg(o).
| err = errorArrayItemsNotAnObject(); | ||
| return; | ||
| } | ||
| }); |
There was a problem hiding this comment.
I think this may be a bit cleaner:
var hasInvalidItems = value.some(function(item) {
return typeof item !== 'object' || Array.isArray(item) || item === null;
});
return hasInvalidItems ? errorArrayItemsNotAnObject() : null;How about Dates as the array items, I think we should reject them too. Can we perhaps reuse validate inside the forEach/some callback?
var self = this;
var hasInvalidItems = value.some(function(item) {
return !self.validate(item/* no options (?) */);
});
return hasInvalidItems ? errorArrayItemsNotAnObject() : null;| } | ||
|
|
||
| function errorArrayItemsNotAnObject() { | ||
| var err = new Error(g.f('Array items are not an object.')); |
There was a problem hiding this comment.
How about Some of array items are not an object ?
|
|
||
| describe('json body - object - allowArray: true', function() { | ||
| verifyTestCases({ arg: 'data', type: 'object', allowArray: true }, [ | ||
| // should accept regular objects, plus array of objects |
There was a problem hiding this comment.
could you please add few cases for "regular objects"?
4d03f76 to
89a388c
Compare
|
oh nice, changed now auto collapse again, @bajtos PTAL |
| * @param optionsObj (optional parameter that gets operated on if you provide it) | ||
| * @returns optionsObj(options to pass to typeConverter methods, eg: validate, fromTypedValue) | ||
| */ | ||
| SharedMethod.getConversionOptionsForArg = function(arg, options) { |
There was a problem hiding this comment.
Modifying input parameters is a bad practice that's better to be avoided.
SharedMethod.getConversionOptionsForArg = function(arg) {
var options = {};
// ...
return options;
};| var name = desc.name || desc.arg; | ||
| var uarg = SharedMethod.convertArg(desc, args[name]); | ||
|
|
||
| var coercionOptions = {}; |
There was a problem hiding this comment.
Use SharedMethod.getConversionOptionsForArg, rename coercionOptions to conversionOptions.
| * @return {*} Coerced argument. | ||
| */ | ||
| function validateInputArgument(uarg, desc, ctx) { | ||
| function validateInputArgument(uarg, desc, ctx, options) { |
There was a problem hiding this comment.
rename options to conversionOptions?
|
|
||
| validate: function(ctx, value) { | ||
| validate: function(ctx, value, options) { | ||
| var _self = this; |
There was a problem hiding this comment.
Why the leading underscore? AFAIK, we use self everywhere else in LoopBack.
ebebf61 to
e53a511
Compare
bajtos
left a comment
There was a problem hiding this comment.
The jsdoc comment is not correct, the patch LGTM otherwise. No more review is needed.
| * build conversion options from remote's' args | ||
| * | ||
| * @param arg (remote args such as `accepts`) | ||
| * @param optionsObj (optional parameter that gets operated on if you provide it) |
| /** | ||
| * build conversion options from remote's' args | ||
| * | ||
| * @param arg (remote args such as `accepts`) |
There was a problem hiding this comment.
@param {Object} arg Definition of accepts/returns argument.
| * | ||
| * @param arg (remote args such as `accepts`) | ||
| * @param optionsObj (optional parameter that gets operated on if you provide it) | ||
| * @returns optionsObj(options to pass to typeConverter methods, eg: validate, fromTypedValue) |
There was a problem hiding this comment.
@returns {Object} Options object to pass to type-converter methods,
e.g `validate` or `fromTypedValue`.
e53a511 to
08f6994
Compare
|
ci failing due to |
|
@slnode test please |
options are now available in functions - fromTypedValue - fromSloppyValue - validate
In loopback 2.x endpoints supported batch create which passes an Array of Objects into the endpoint that expects Objects, this is nolonger allowed in 3.x's stricter coercion In order to maintain backwards compatiability, this is a short term fix to allow endpoints to accept Array of Objects with the allowArray
08f6994 to
e9b7977
Compare
|
@slnode test please |
fixes #360
In loopback 2.x endpoints supported batch create which passes
an Array of Objects into the endpoint that expects Objects,
this is nolonger allowed in 3.x's stricter coercion
In order to maintain backwards compatiability, this is a short term
fix to allow endpoints to accept Array of Objects with the allowArray