-
Notifications
You must be signed in to change notification settings - Fork 12
fix(view): ensure all optional properties are validated #1438
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
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /*! | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import { Folder, View } from '../../lib/index.ts' | ||
|
|
||
| /** | ||
| * Creates a mock View and its associated Folder for testing purposes. | ||
| */ | ||
| export function mockView() { | ||
| const folder = new Folder({ | ||
| source: 'https://example.org/dav/files/admin/', | ||
| root: '/files/admin', | ||
| owner: 'admin', | ||
| }) | ||
|
|
||
| const view = new View({ | ||
| id: 'test', | ||
| name: 'Test', | ||
| caption: 'Test caption', | ||
| emptyTitle: 'Test empty title', | ||
| emptyCaption: 'Test empty caption', | ||
| getContents: () => Promise.resolve({ folder, contents: [] }), | ||
| hidden: true, | ||
| icon: '<svg></svg>', | ||
| order: 1, | ||
| params: {}, | ||
| columns: [], | ||
| emptyView: () => {}, | ||
| parent: 'parent', | ||
| sticky: false, | ||
| expanded: false, | ||
| defaultSortKey: 'key', | ||
| loadChildViews: async () => {}, | ||
| }) | ||
|
|
||
| return { folder, view } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,64 +208,70 @@ export class View implements IView { | |
| * @throws {Error} if the view is not valid | ||
| */ | ||
| export function validateView(view: IView) { | ||
| if (!view.id || typeof view.id !== 'string') { | ||
| throw new Error('View id is required and must be a string') | ||
| } | ||
|
|
||
| if (!view.name || typeof view.name !== 'string') { | ||
| throw new Error('View name is required and must be a string') | ||
| if (!view.icon || typeof view.icon !== 'string' || !isSvg(view.icon)) { | ||
| throw new Error('View icon is required and must be a valid svg string') | ||
| } | ||
|
|
||
| if ('caption' in view && typeof view.caption !== 'string') { | ||
| throw new Error('View caption must be a string') | ||
| if (!view.id || typeof view.id !== 'string') { | ||
| throw new Error('View id is required and must be a string') | ||
| } | ||
|
|
||
| if (!view.getContents || typeof view.getContents !== 'function') { | ||
| throw new Error('View getContents is required and must be a function') | ||
| } | ||
|
|
||
| if ('hidden' in view && typeof view.hidden !== 'boolean') { | ||
| throw new Error('View hidden must be a boolean') | ||
| if (!view.name || typeof view.name !== 'string') { | ||
| throw new Error('View name is required and must be a string') | ||
| } | ||
|
|
||
| if (!view.icon || typeof view.icon !== 'string' || !isSvg(view.icon)) { | ||
| throw new Error('View icon is required and must be a valid svg string') | ||
| } | ||
| // optional properties type checks | ||
|
|
||
| if ('order' in view && typeof view.order !== 'number') { | ||
| throw new Error('View order must be a number') | ||
| } | ||
| checkOptionalProperty(view, 'caption', 'string') | ||
| checkOptionalProperty(view, 'columns', 'array') | ||
| checkOptionalProperty(view, 'defaultSortKey', 'string') | ||
|
susnux marked this conversation as resolved.
|
||
| checkOptionalProperty(view, 'emptyCaption', 'string') | ||
| checkOptionalProperty(view, 'emptyTitle', 'string') | ||
| checkOptionalProperty(view, 'emptyView', 'function') | ||
| checkOptionalProperty(view, 'expanded', 'boolean') | ||
| checkOptionalProperty(view, 'hidden', 'boolean') | ||
| checkOptionalProperty(view, 'loadChildViews', 'function') | ||
| checkOptionalProperty(view, 'order', 'number') | ||
| checkOptionalProperty(view, 'params', 'object') | ||
| checkOptionalProperty(view, 'parent', 'string') | ||
| checkOptionalProperty(view, 'sticky', 'boolean') | ||
|
|
||
| // Optional properties | ||
| if (view.columns) { | ||
| view.columns.forEach((column) => { | ||
| if (!(column instanceof Column)) { | ||
| throw new Error('View columns must be an array of Column. Invalid column found') | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if (view.emptyView && typeof view.emptyView !== 'function') { | ||
| throw new Error('View emptyView must be a function') | ||
| } | ||
|
|
||
| if (view.parent && typeof view.parent !== 'string') { | ||
| throw new Error('View parent must be a string') | ||
| } | ||
|
|
||
| if ('sticky' in view && typeof view.sticky !== 'boolean') { | ||
| throw new Error('View sticky must be a boolean') | ||
| } | ||
|
|
||
| if ('expanded' in view && typeof view.expanded !== 'boolean') { | ||
| throw new Error('View expanded must be a boolean') | ||
| } | ||
|
|
||
| if (view.defaultSortKey && typeof view.defaultSortKey !== 'string') { | ||
| throw new Error('View defaultSortKey must be a string') | ||
| } | ||
|
|
||
| if (view.loadChildViews && typeof view.loadChildViews !== 'function') { | ||
| throw new Error('View loadChildViews must be a function') | ||
| /** | ||
| * Check an optional property type | ||
| * | ||
| * @param obj - the object to check | ||
| * @param property - the property name | ||
| * @param type - the expected type | ||
| * @throws {Error} if the property is defined and not of the expected type | ||
| */ | ||
| function checkOptionalProperty( | ||
| obj: Partial<IView>, | ||
| property: keyof IView, | ||
| type: 'array' | 'function' | 'string' | 'boolean' | 'number' | 'object', | ||
| ): void { | ||
| if (typeof obj[property] !== 'undefined') { | ||
| if (type === 'array') { | ||
| if (!Array.isArray(obj[property])) { | ||
| throw new Error(`View ${property} must be an array`) | ||
| } | ||
| // eslint-disable-next-line valid-typeof | ||
| } else if (typeof obj[property] !== type) { | ||
| throw new Error(`View ${property} must be a ${type}`) | ||
| } else if (type === 'object' && (obj[property] === null || Array.isArray(obj[property]))) { | ||
| throw new Error(`View ${property} must be an object`) | ||
| } | ||
|
susnux marked this conversation as resolved.
Comment on lines
+273
to
+275
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was added on the rebase |
||
| } | ||
| } | ||
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.
This is failing without this changes