-
Notifications
You must be signed in to change notification settings - Fork 9
fix: enforce catalog integrity for order item creation #59
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
Open
dharapandya85
wants to merge
1
commit into
fuzziecoder:main
Choose a base branch
from
dharapandya85:fix/enforce-catalog-integrity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🧩 Analysis chain
🌐 Web query:
JavaScriptinoperator behavior with null undefined primitives TypeError💡 Result:
In JavaScript, the
inoperator requires its right-hand operand to be an Object. Per the ECMAScript spec, evaluatingprop in rhsdoes:rhsto a valuerValrValis not an Object, throwTypeErrorHasProperty(rVal, ToPropertyKey(prop))(checks own + prototype chain) [1]So these all throw, because the RHS is not an Object (including
null,undefined, and all primitives like strings/numbers/booleans/bigints/symbols):This is the same error described by MDN (“right-hand side of 'in' should be an object”) and it explicitly calls out
null/undefinedand primitives as causes [2].How to avoid the TypeError
1) Guard against nullish values (most common):
2) If you intentionally want “wrap primitives” behavior:
(Be aware this changes semantics vs throwing—for
null/undefinedit becomes{}, for strings it becomes aStringobject wrapper.)3) If you just want a safe property read (not
in):Use optional chaining for access:
[3]
Sources: ECMAScript spec (RelationalExpression
in) [1], MDN error reference forinwith non-objects [2], MDN optional chaining [3].🌐 Web query:
JavaScriptinoperator prototype chain inheritance check💡 Result:
In JavaScript, the
inoperator returnstrueif a property exists either as an own property or anywhere on the object’s prototype chain:Key points:
prop in objchecks existence, not value—so it can betrueeven ifobj[prop] === undefined. [1]Object.hasOwn(obj, prop)(orhasOwnProperty.call) for that. [1]someProto.isPrototypeOf(obj)to test whethersomeProtoappears inobj’s prototype chain, orobj instanceof Ctor(which checks againstCtor.prototype). [2]Sources:
[1] MDN —
inoperator (prototype chain behavior,Object.hasOwnrecommendation) (developer.mozilla.org)[2] MDN —
Object.prototype.isPrototypeOf()(prototype chain relationship; note aboutinstanceof) (developer.mozilla.org)Citations:
🏁 Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 95
🏁 Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 97
🏁 Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 1364
🏁 Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 2620
🏁 Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 997
Add type guard before using
inoperator on item; can throw TypeError on malformed payloads.At line 246, the
inoperator throws aTypeErrorifitemisnull,undefined, or a primitive value. This can occur with malformed client input and will cause an unhandled runtime error instead of a proper validation error. Additionally,inchecks the prototype chain; useObject.prototype.hasOwnProperty.call()to validate only own properties.Suggested fix
const parsedItems = items.map((item) => { + if (!item || typeof item !== 'object' || Array.isArray(item)) { + throw new Error('Each order item must be an object with productId and a positive integer quantity'); + } - if ('name' in item || 'unitPrice' in item || 'total' in item) { - throw new Error('Do not provide name, unitPrice, or total. These are derived from catalog. ') - } + if ( + Object.prototype.hasOwnProperty.call(item, 'name') || + Object.prototype.hasOwnProperty.call(item, 'unitPrice') || + Object.prototype.hasOwnProperty.call(item, 'total') + ) { + throw new Error('Do not provide name, unitPrice, or total. These are derived from catalog.'); + }📝 Committable suggestion
🤖 Prompt for AI Agents