@@ -208,64 +208,70 @@ export class View implements IView {
208208 * @throws {Error } if the view is not valid
209209 */
210210export function validateView ( view : IView ) {
211- if ( ! view . id || typeof view . id !== 'string' ) {
212- throw new Error ( 'View id is required and must be a string' )
213- }
214-
215- if ( ! view . name || typeof view . name !== 'string' ) {
216- throw new Error ( 'View name is required and must be a string' )
211+ if ( ! view . icon || typeof view . icon !== 'string' || ! isSvg ( view . icon ) ) {
212+ throw new Error ( 'View icon is required and must be a valid svg string' )
217213 }
218214
219- if ( 'caption' in view && typeof view . caption !== 'string' ) {
220- throw new Error ( 'View caption must be a string' )
215+ if ( ! view . id || typeof view . id !== 'string' ) {
216+ throw new Error ( 'View id is required and must be a string' )
221217 }
222218
223219 if ( ! view . getContents || typeof view . getContents !== 'function' ) {
224220 throw new Error ( 'View getContents is required and must be a function' )
225221 }
226222
227- if ( 'hidden' in view && typeof view . hidden !== 'boolean ' ) {
228- throw new Error ( 'View hidden must be a boolean ' )
223+ if ( ! view . name || typeof view . name !== 'string ' ) {
224+ throw new Error ( 'View name is required and must be a string ' )
229225 }
230226
231- if ( ! view . icon || typeof view . icon !== 'string' || ! isSvg ( view . icon ) ) {
232- throw new Error ( 'View icon is required and must be a valid svg string' )
233- }
227+ // optional properties type checks
234228
235- if ( 'order' in view && typeof view . order !== 'number' ) {
236- throw new Error ( 'View order must be a number' )
237- }
229+ checkOptionalProperty ( view , 'caption' , 'string' )
230+ checkOptionalProperty ( view , 'columns' , 'array' )
231+ checkOptionalProperty ( view , 'defaultSortKey' , 'string' )
232+ checkOptionalProperty ( view , 'emptyCaption' , 'string' )
233+ checkOptionalProperty ( view , 'emptyTitle' , 'string' )
234+ checkOptionalProperty ( view , 'emptyView' , 'function' )
235+ checkOptionalProperty ( view , 'expanded' , 'boolean' )
236+ checkOptionalProperty ( view , 'hidden' , 'boolean' )
237+ checkOptionalProperty ( view , 'loadChildViews' , 'function' )
238+ checkOptionalProperty ( view , 'order' , 'number' )
239+ checkOptionalProperty ( view , 'params' , 'object' )
240+ checkOptionalProperty ( view , 'parent' , 'string' )
241+ checkOptionalProperty ( view , 'sticky' , 'boolean' )
238242
239- // Optional properties
240243 if ( view . columns ) {
241244 view . columns . forEach ( ( column ) => {
242245 if ( ! ( column instanceof Column ) ) {
243246 throw new Error ( 'View columns must be an array of Column. Invalid column found' )
244247 }
245248 } )
246249 }
250+ }
247251
248- if ( view . emptyView && typeof view . emptyView !== 'function' ) {
249- throw new Error ( 'View emptyView must be a function' )
250- }
251-
252- if ( view . parent && typeof view . parent !== 'string' ) {
253- throw new Error ( 'View parent must be a string' )
254- }
255-
256- if ( 'sticky' in view && typeof view . sticky !== 'boolean' ) {
257- throw new Error ( 'View sticky must be a boolean' )
258- }
259-
260- if ( 'expanded' in view && typeof view . expanded !== 'boolean' ) {
261- throw new Error ( 'View expanded must be a boolean' )
262- }
263-
264- if ( view . defaultSortKey && typeof view . defaultSortKey !== 'string' ) {
265- throw new Error ( 'View defaultSortKey must be a string' )
266- }
267-
268- if ( view . loadChildViews && typeof view . loadChildViews !== 'function' ) {
269- throw new Error ( 'View loadChildViews must be a function' )
252+ /**
253+ * Check an optional property type
254+ *
255+ * @param obj - the object to check
256+ * @param property - the property name
257+ * @param type - the expected type
258+ * @throws {Error } if the property is defined and not of the expected type
259+ */
260+ function checkOptionalProperty (
261+ obj : Partial < IView > ,
262+ property : keyof IView ,
263+ type : 'array' | 'function' | 'string' | 'boolean' | 'number' | 'object' ,
264+ ) : void {
265+ if ( typeof obj [ property ] !== 'undefined' ) {
266+ if ( type === 'array' ) {
267+ if ( ! Array . isArray ( obj [ property ] ) ) {
268+ throw new Error ( `View ${ property } must be an array` )
269+ }
270+ // eslint-disable-next-line valid-typeof
271+ } else if ( typeof obj [ property ] !== type ) {
272+ throw new Error ( `View ${ property } must be a ${ type } ` )
273+ } else if ( type === 'object' && ( obj [ property ] === null || Array . isArray ( obj [ property ] ) ) ) {
274+ throw new Error ( `View ${ property } must be an object` )
275+ }
270276 }
271277}
0 commit comments