Skip to content
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

Provide more specific message for invalid state code in header #16

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions src/versions/2.0/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const HEADER_COLUMNS = [
"version", // string - maybe one of the known versions?
"hospital_location", // string
"hospital_address", // string
"license_number | state", // string, check for valid postal code in header
"license_number | [state]", // string, check for valid postal code in header
ATTESTATION, // "true"
] as const

Expand Down Expand Up @@ -62,8 +62,8 @@ const ERRORS = {
HEADER_COLUMN_MISSING: (column: string) =>
`Header column should be "${column}", but it is not present`,
HEADER_COLUMN_BLANK: (column: string) => `"${column}" is blank`,
HEADER_STATE_CODE: (column: string, stateCode: string) =>
`Header column "${column}" includes an invalid state code "${stateCode}"`,
HEADER_STATE_CODE: (stateCode: string) =>
`${stateCode} is not an allowed value for state abbreviation. You must fill in the state or territory abbreviation even if there is no license number to encode. See the table found here for the list of valid values for state and territory abbreviations https://github.com/CMSgov/hospital-price-transparency/blob/master/documentation/CSV/state_codes.md`,
DUPLICATE_HEADER_COLUMN: (column: string) =>
`Column ${column} duplicated in header`,
COLUMN_MISSING: (column: string, format: string) =>
Expand Down Expand Up @@ -112,12 +112,33 @@ export function validateHeaderColumns(columns: string[]): {
const rowIndex = 0
const remainingColumns = [...HEADER_COLUMNS]
const discoveredColumns: string[] = []
const duplicateErrors: CsvValidationError[] = []
const errors: CsvValidationError[] = []
columns.forEach((column, index) => {
const matchingColumnIndex = remainingColumns.findIndex((requiredColumn) => {
if (requiredColumn === "license_number | state") {
// see if it works
return validateLicenseStateColumn(column)
if (requiredColumn === "license_number | [state]") {
// make a best guess as to when a header is meant to be the license_number header
// if it has two parts, and the first part matches, then the second part ought to be valid
const splitColumn = column.split("|").map((v) => v.trim())
if (splitColumn.length !== 2) {
return false
}
if (sepColumnsEqual(splitColumn[0], "license_number")) {
if (STATE_CODES.includes(splitColumn[1].toUpperCase() as StateCode)) {
return true
} else {
errors.push(
csvErr(
rowIndex,
index,
requiredColumn,
ERRORS.HEADER_STATE_CODE(splitColumn[1])
)
)
return false
}
} else {
return false
}
} else {
return sepColumnsEqual(column, requiredColumn)
}
Expand All @@ -131,7 +152,7 @@ export function validateHeaderColumns(columns: string[]): {
return discovered != null && sepColumnsEqual(discovered, column)
})
if (existingColumn) {
duplicateErrors.push(
errors.push(
csvErr(
rowIndex,
index,
Expand All @@ -144,7 +165,7 @@ export function validateHeaderColumns(columns: string[]): {
})
return {
errors: [
...duplicateErrors,
...errors,
...remainingColumns.map((requiredColumn) => {
return csvErr(
rowIndex,
Expand Down Expand Up @@ -329,20 +350,6 @@ export function validateRow(
DRUG_UNITS
)
)
// if (!DRUG_UNITS.includes(row["drug_type_of_measurement"] as DrugUnit)) {
// errors.push(
// csvErr(
// index,
// columns.indexOf("drug_type_of_measurement"),
// "drug_type_of_measurement",
// ERRORS.ALLOWED_VALUES(
// "drug_type_of_measurement",
// row["drug_type_of_measurement"],
// DRUG_UNITS as unknown as string[]
// )
// )
// )
// }
}

const chargeFields = [
Expand Down Expand Up @@ -411,20 +418,6 @@ export function validateWideFields(
return errors
}

function validateLicenseStateColumn(column: string): boolean {
const splitColumn = column.split("|").map((v) => v.trim())
if (splitColumn.length !== 2) {
return false
}
const stateCode = column.split("|").slice(-1)[0].trim()
if (!STATE_CODES.includes(stateCode.toUpperCase() as StateCode)) {
return false
} else if (!sepColumnsEqual(column, `license_number | ${stateCode}`)) {
return false
}
return true
}

/** @private */
export function validateTallFields(
row: { [key: string]: string },
Expand Down
12 changes: 11 additions & 1 deletion test/2.0/csv.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "../../src/versions/2.0/csv.js"

const VALID_HEADER_COLUMNS = HEADER_COLUMNS.map((c) =>
c === "license_number | state" ? "license_number | MD" : c
c === "license_number | [state]" ? "license_number | MD" : c
)

test("validateHeaderColumns", (t) => {
Expand Down Expand Up @@ -46,6 +46,16 @@ test("validateHeaderColumns", (t) => {
"Column hospital_location duplicated in header"
)
t.deepEqual(duplicateResult.columns, VALID_HEADER_COLUMNS)
const invalidStateColumns = HEADER_COLUMNS.map((c) =>
c === "license_number | [state]" ? "license_number | ZZ" : c
)
const invalidStateErrors = validateHeaderColumns(invalidStateColumns)
t.is(invalidStateErrors.errors.length, 2)
t.assert(
invalidStateErrors.errors[0].message.includes(
"ZZ is not an allowed value for state abbreviation"
)
)
})

test("validateHeaderRow", (t) => {
Expand Down
Loading