@@ -42,7 +42,7 @@ export interface IView {
4242 * This method _must_ also return the current directory
4343 * information alongside with its content.
4444 *
45- * Usually a abort signal is provided to be able to
45+ * A abort signal is provided to be able to
4646 * cancel the request if the user change directory
4747 * {@see https://developer.mozilla.org/en-US/docs/Web/API/AbortController }.
4848 */
@@ -201,64 +201,68 @@ export class View implements IView {
201201 * @throws {Error } if the view is not valid
202202 */
203203export function validateView ( view : IView ) {
204- if ( ! view . id || typeof view . id !== 'string' ) {
205- throw new Error ( 'View id is required and must be a string' )
206- }
207-
208- if ( ! view . name || typeof view . name !== 'string' ) {
209- throw new Error ( 'View name is required and must be a string' )
204+ if ( ! view . icon || typeof view . icon !== 'string' || ! isSvg ( view . icon ) ) {
205+ throw new Error ( 'View icon is required and must be a valid svg string' )
210206 }
211207
212- if ( 'caption' in view && typeof view . caption !== 'string' ) {
213- throw new Error ( 'View caption must be a string' )
208+ if ( ! view . id || typeof view . id !== 'string' ) {
209+ throw new Error ( 'View id is required and must be a string' )
214210 }
215211
216212 if ( ! view . getContents || typeof view . getContents !== 'function' ) {
217213 throw new Error ( 'View getContents is required and must be a function' )
218214 }
219215
220- if ( 'hidden' in view && typeof view . hidden !== 'boolean ' ) {
221- throw new Error ( 'View hidden must be a boolean ' )
216+ if ( ! view . name || typeof view . name !== 'string ' ) {
217+ throw new Error ( 'View name is required and must be a string ' )
222218 }
223219
224- if ( ! view . icon || typeof view . icon !== 'string' || ! isSvg ( view . icon ) ) {
225- throw new Error ( 'View icon is required and must be a valid svg string' )
226- }
220+ // optional properties type checks
227221
228- if ( 'order' in view && typeof view . order !== 'number' ) {
229- throw new Error ( 'View order must be a number' )
230- }
222+ checkOptionalProperty ( view , 'caption' , 'string' )
223+ checkOptionalProperty ( view , 'columns' , 'array' )
224+ checkOptionalProperty ( view , 'defaultSortKey' , 'string' )
225+ checkOptionalProperty ( view , 'emptyCaption' , 'string' )
226+ checkOptionalProperty ( view , 'emptyTitle' , 'string' )
227+ checkOptionalProperty ( view , 'emptyView' , 'function' )
228+ checkOptionalProperty ( view , 'expanded' , 'boolean' )
229+ checkOptionalProperty ( view , 'hidden' , 'boolean' )
230+ checkOptionalProperty ( view , 'loadChildViews' , 'function' )
231+ checkOptionalProperty ( view , 'order' , 'number' )
232+ checkOptionalProperty ( view , 'params' , 'object' )
233+ checkOptionalProperty ( view , 'parent' , 'string' )
234+ checkOptionalProperty ( view , 'sticky' , 'boolean' )
231235
232- // Optional properties
233236 if ( view . columns ) {
234237 view . columns . forEach ( ( column ) => {
235238 if ( ! ( column instanceof Column ) ) {
236239 throw new Error ( 'View columns must be an array of Column. Invalid column found' )
237240 }
238241 } )
239242 }
243+ }
240244
241- if ( view . emptyView && typeof view . emptyView !== 'function' ) {
242- throw new Error ( 'View emptyView must be a function' )
243- }
244-
245- if ( view . parent && typeof view . parent !== 'string' ) {
246- throw new Error ( 'View parent must be a string' )
247- }
248-
249- if ( 'sticky' in view && typeof view . sticky !== 'boolean' ) {
250- throw new Error ( 'View sticky must be a boolean' )
251- }
252-
253- if ( 'expanded' in view && typeof view . expanded !== 'boolean' ) {
254- throw new Error ( 'View expanded must be a boolean' )
255- }
256-
257- if ( view . defaultSortKey && typeof view . defaultSortKey !== 'string' ) {
258- throw new Error ( 'View defaultSortKey must be a string' )
259- }
260-
261- if ( view . loadChildViews && typeof view . loadChildViews !== 'function' ) {
262- throw new Error ( 'View loadChildViews must be a function' )
245+ /**
246+ * Check an optional property type
247+ *
248+ * @param obj - the object to check
249+ * @param property - the property name
250+ * @param type - the expected type
251+ * @throws { Error } if the property is defined and not of the expected type
252+ */
253+ function checkOptionalProperty (
254+ obj : Partial < IView > ,
255+ property : keyof IView ,
256+ type : 'array' | 'function' | 'string' | 'boolean' | 'number' | 'object' ,
257+ ) : void {
258+ if ( typeof obj [ property ] !== 'undefined' ) {
259+ if ( type === 'array' ) {
260+ if ( ! Array . isArray ( obj [ property ] ) ) {
261+ throw new Error ( `View ${ property } must be an array` )
262+ }
263+ // eslint-disable-next-line valid-typeof
264+ } else if ( typeof obj [ property ] !== type ) {
265+ throw new Error ( `View ${ property } must be a ${ type } ` )
266+ }
263267 }
264268}
0 commit comments