-
Notifications
You must be signed in to change notification settings - Fork 92
allowArray flag for remoteObject accept
#369
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
|
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, this second version looks much better!
lib/http-context.js
Outdated
| var httpFormat = o.http; | ||
| var name = o.name || o.arg; | ||
| var val; | ||
| var coercionOptions = {}; |
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.
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
| assert(typeof typeName === 'string' && typeName, | |
| 'typeName must be a non-empty string'); | |
| assert(typeof converter === 'object' && converter, | |
| 'converter must be an object'); | |
| assert(typeof converter.fromTypedValue === 'function', | |
| 'converter.fromTypedValue must be a function'); | |
| assert(typeof converter.fromSloppyValue === 'function', | |
| 'converter.fromSloppyValue must be a function'); | |
| assert(typeof converter.validate === 'function', | |
| 'converter.validate must be a function'); |
lib/http-context.js
Outdated
|
|
||
| var typeConverter = ctx.typeRegistry.getConverter(o.type); | ||
| if (o.allowArray) | ||
| coercionOptions.allowArray = o.allowArray; |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
lib/types/object.js
Outdated
| } | ||
|
|
||
| function errorArrayItemsNotAnObject() { | ||
| var err = new Error(g.f('Array items are not an object.')); |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please add few cases for "regular objects"?
4d03f76 to
89a388c
Compare
|
oh nice, changed now auto collapse again, @bajtos PTAL |
bajtos
left a comment
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.
Few more comments, PTAL
lib/shared-method.js
Outdated
| * @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modifying input parameters is a bad practice that's better to be avoided.
SharedMethod.getConversionOptionsForArg = function(arg) {
var options = {};
// ...
return options;
};
lib/shared-method.js
Outdated
| var name = desc.name || desc.arg; | ||
| var uarg = SharedMethod.convertArg(desc, args[name]); | ||
|
|
||
| var coercionOptions = {}; |
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.
Use SharedMethod.getConversionOptionsForArg, rename coercionOptions to conversionOptions.
lib/shared-method.js
Outdated
| * @return {*} Coerced argument. | ||
| */ | ||
| function validateInputArgument(uarg, desc, ctx) { | ||
| function validateInputArgument(uarg, desc, ctx, options) { |
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.
rename options to conversionOptions?
lib/types/object.js
Outdated
|
|
||
| validate: function(ctx, value) { | ||
| validate: function(ctx, value, options) { | ||
| var _self = this; |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The jsdoc comment is not correct, the patch LGTM otherwise. No more review is needed.
lib/shared-method.js
Outdated
| * 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) |
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 remove
lib/shared-method.js
Outdated
| /** | ||
| * build conversion options from remote's' args | ||
| * | ||
| * @param arg (remote args such as `accepts`) |
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.
@param {Object} arg Definition of accepts/returns argument.
lib/shared-method.js
Outdated
| * | ||
| * @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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 |
2 similar comments
|
@slnode test please |
|
@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