Repository navigation
Fix comment-json@5 CommentArray detection in project config sanitization - #509
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors array type checks in functions/src/project.ts to use Array.isArray() and simplifies string type checking. A review comment suggests ensuring all elements in the array are strings using Array.prototype.every() to prevent potential runtime errors when handling mixed-type arrays.
| arr.length > 0 && | ||
| arr[0].constructor === String | ||
| ) { | ||
| if (Array.isArray(arr) && arr.length > 0 && typeof arr[0] === "string") { |
There was a problem hiding this comment.
The current check only verifies that the first element of the array is a string (typeof arr[0] === "string"). If the array contains mixed types (e.g., ["web", 123]), calling item.toLowerCase() inside the loop will throw a runtime TypeError. To prevent this and fully adhere to the comment ('Only sanitize if it's a non-empty array of strings'), we should ensure that all elements in the array are strings using Array.prototype.every().
| if (Array.isArray(arr) && arr.length > 0 && typeof arr[0] === "string") { | |
| if (Array.isArray(arr) && arr.length > 0 && arr.every(item => typeof item === "string")) { |
Summary
obj.constructor === ArraywithArray.isArray(obj)inProject.lowercaseMapKeysandProject.arraysToMaps, andarr[0].constructor === Stringwithtypeof arr[0] === "string".comment-json@5parses JSON arrays intoCommentArraysubclasses (arr.constructor !== Array), which previously causedsanitizeForStorageto skip convertingplatformsandrelatedarrays into Firestore boolean maps ({ [key]: true }), breaking downstream frontend static generation expecting object maps.Testing
npm --prefix=functions run build).Project.prototype.sanitizeForStorageconvertscjson.parse('{"platforms":["PHP","Admin"],"related":["a/b"]}', null, true)to{ platforms: { php: true, admin: true }, related: { 'a/b': true } }.