Skip to content

Commit

Permalink
🔨 enum validation abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Oct 30, 2023
1 parent 65df8e9 commit e48b3e3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
34 changes: 14 additions & 20 deletions db/model/Gdoc/rawToEnriched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ import {
TableTemplate,
TableSize,
EnrichedBlockTableCell,
tableSizes,
tableTemplates,
} from "@ourworldindata/utils"
import { checkIsInternalLink } from "@ourworldindata/components"
import {
Expand Down Expand Up @@ -797,6 +799,13 @@ const parseRecirc = (raw: RawBlockRecirc): EnrichedBlockRecirc => {
}
}

function validateRawEnum<T extends string>(
validValues: ReadonlyArray<T>,
value: unknown
): value is T {
return typeof value === "string" && validValues.includes(value as T)
}

export const parseTable = (raw: RawBlockTable): EnrichedBlockTable => {
const createError = (
error: ParseError,
Expand All @@ -813,33 +822,18 @@ export const parseTable = (raw: RawBlockTable): EnrichedBlockTable => {

const parseErrors: ParseError[] = []

const validTemplates: TableTemplate[] = [
"header-row",
"header-column",
"header-column-row",
]
function validateTableTemplate(
template: unknown
): template is TableTemplate {
return validTemplates.includes(template as TableTemplate)
}

const template = raw.value?.template
if (!validateTableTemplate(template))
const template = raw.value?.template || "header-row"
if (!validateRawEnum(tableTemplates, template))
return createError({
message: `Invalid table template "${template}". Must be one of ${validTemplates.join(
message: `Invalid table template "${template}". Must be one of ${tableTemplates.join(
", "
)}`,
})

const validSizes: TableSize[] = ["narrow", "wide"]
function validateTableSize(size: unknown): size is TableSize {
return validSizes.includes(size as TableSize)
}
const size = raw.value?.size || "narrow"
if (!validateTableSize(size))
if (!validateRawEnum(tableSizes, size))
return createError({
message: `Invalid table size "${size}". Must be one of ${validSizes.join(
message: `Invalid table size "${size}". Must be one of ${tableSizes.join(
", "
)}`,
})
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ export {
type RawBlockText,
type RawBlockTopicPageIntro,
type RawBlockUrl,
tableTemplates,
type TableTemplate,
tableSizes,
type TableSize,
type RawBlockTable,
type RawBlockTableRow,
Expand Down
12 changes: 10 additions & 2 deletions packages/@ourworldindata/utils/src/owidTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1138,9 +1138,17 @@ export type EnrichedBlockEntrySummary = {
items: EnrichedBlockEntrySummaryItem[]
} & EnrichedBlockWithParseErrors

export type TableTemplate = "header-column" | "header-row" | "header-column-row"
export const tableTemplates = [
"header-column",
"header-row",
"header-column-row",
] as const

export type TableTemplate = (typeof tableTemplates)[number]

export const tableSizes = ["narrow", "wide"] as const

export type TableSize = "narrow" | "wide"
export type TableSize = (typeof tableSizes)[number]

export type RawBlockTable = {
type: "table"
Expand Down

0 comments on commit e48b3e3

Please sign in to comment.