-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[BUGFIX release] Validate JSON API docs returned by normalizeResponse #3608
Conversation
c93a4f1
to
926b966
Compare
*/ | ||
export function hasJsonApiDocumentValidationErrors(doc) { | ||
let errors = []; | ||
if (Ember.typeOf(doc) !== '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.
doc && typeof object === 'object
99feb94
to
e983e26
Compare
Thanks for feedback @stefanpenner . I have addressed it, and also reformatted the error messages In the future, if we get more sophisticated about JSON API validation, there may be multiple validation errors returned for a document |
!('meta' in doc)) { | ||
errors.push('One or more of the following keys must be present: "data", "errors", "meta".'); | ||
} else { | ||
if (('data' in doc) && ('errors' in doc)) { |
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.
do we also need to check that the properties have object values, or are they allow to be of any type
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.
arrays or objects are allowed as values. I'm not sure where we want this to end, because we could then validate the objects (or arrays) themselves, including relationships, etc...
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.
meta
has to be an objectdata
can be an object, null or an arrayerrors
has to be an array
I'm fine with just checking that the keys exists for now though.
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.
thanks, I'll go ahead and implement these validations
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.
Ok, now I have validations in place for meta
, data
, errors
, links
, jsonapi
and included
. This should provide complete validation of the top-level of a JSON API document
5505ee9
to
7cf819f
Compare
This seems to now be complete AFAIK. |
} | ||
if ('data' in doc) { | ||
if (!(doc.data === null || Ember.isArray(doc.data) || typeof doc.data === 'object')) { | ||
errors.push('data must be null, an object, or an array'); |
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.
Whats the difference between this error and the one on line 31?
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.
Line 29-33 seems identical with 34-38.
Looks good. Do we want to prefix this commit with [BUGFIX release] and include in in 1.13.x? |
yes please (just pinged mike) |
7cf819f
to
0c6032b
Compare
@bmac done |
@@ -16,7 +80,8 @@ const get = Ember.get; | |||
*/ | |||
export function normalizeResponseHelper(serializer, store, modelClass, payload, id, requestType) { | |||
let normalizedResponse = serializer.normalizeResponse(store, modelClass, payload, id, requestType); | |||
|
|||
let validationErrors = validateDocumentStructure(normalizedResponse); |
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 call should happen in the assert block, or it wont get stripped for prod builds
left one blocking comment. |
0c6032b
to
353889a
Compare
@stefanpenner should be good now. I need the data in the assert description, so wrapped the whole thing in a |
no longer caching node_modules #3616 |
Stef clarified this for me. The way defeatureify works internally disallows nested "defeaturees" (i.e., an assert in a runInDebug) |
353889a
to
58dbdb7
Compare
Let's remove one of the duplicate checks (mentioned above) and merge this |
…urned by serializer#normalizeResponse
58dbdb7
to
782c5b1
Compare
Looks good. Thanks @mike-north |
[BUGFIX release] Validate JSON API docs returned by normalizeResponse
Fixes #3603
JSON API documents returned by
normalizeResponse
now are validated in the following waysEnsure that...
data
, if present, is either null, an array or an objecterrors
, if present, is an arraylinks
, if present, is an objectmeta
, if present, is an objectinclude
, if present, is an arrayjsonapi
, if present, is an object