Skip to content

Commit

Permalink
widgets: Add validation for mini-widgets
Browse files Browse the repository at this point in the history
Include container and view validation.
  • Loading branch information
rafaellehmkuhl authored and patrickelectric committed Jul 2, 2024
1 parent d858319 commit 1d2e2f0
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/types/widgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,55 @@ export const validateWidget = (maybeWidget: Widget): maybeWidget is Widget => {
return true
}

export const validateMiniWidget = (maybeMiniWidget: MiniWidget): maybeMiniWidget is MiniWidget => {
if (maybeMiniWidget.hash === undefined) throw new Error('Mini widget validation failed: property hash is missing.')

const miniWidgetProps = ['component', 'name', 'options', 'managerVars']
const managetVarsProps = ['configMenuOpen', 'highlighted']
const checkFails: string[] = []

miniWidgetProps.forEach((p) => {
// @ts-ignore
if (maybeMiniWidget[p] !== undefined) return
checkFails.push(`Property ${p} is missing.`)
})

managetVarsProps.forEach((p) => {
// @ts-ignore
if (maybeMiniWidget['managerVars'] !== undefined && maybeMiniWidget['managerVars'][p] !== undefined) return
checkFails.push(`Property ${p} of the managerVars is missing.`)
})

if (checkFails.length !== 0) {
throw new Error(`Mini widget ${maybeMiniWidget.hash} validation failed: ${checkFails.join(' ')}`)
}

return true
}

export const validateContainer = (maybeContainer: MiniWidgetContainer): maybeContainer is MiniWidgetContainer => {
if (maybeContainer.name === undefined) throw new Error('View validation failed: property "name" is missing.')
const checkFails: string[] = []

if (Array.isArray(maybeContainer.widgets)) {
maybeContainer.widgets.forEach((w) => {
try {
validateMiniWidget(w)
} catch (error) {
checkFails.push((error as Error).message)
}
})
} else {
checkFails.push('Property "widgets" is missing or is not an array.')
}

if (checkFails.length !== 0) {
throw new Error(`Mini widget container ${maybeContainer.name} validation failed: ${checkFails.join(' ')}`)
}

return true
}

export const validateView = (maybeView: View): maybeView is View => {
if (maybeView.hash === undefined) throw new Error('View validation failed: property "hash" is missing.')

Expand All @@ -268,6 +317,18 @@ export const validateView = (maybeView: View): maybeView is View => {
checkFails.push('Property "widgets" is missing or is not an array.')
}

if (Array.isArray(maybeView.miniWidgetContainers)) {
maybeView.miniWidgetContainers.forEach((c) => {
try {
validateContainer(c)
} catch (error) {
checkFails.push((error as Error).message)
}
})
} else {
checkFails.push('Property "miniWidgetContainers" is missing or is not an array.')
}

if (checkFails.length !== 0) {
throw new Error(`View ${maybeView.hash} validation failed: ${checkFails.join(' ')}`)
}
Expand Down

0 comments on commit 1d2e2f0

Please sign in to comment.