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

fix: handle missing required columns with a nicer error #725

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
19 changes: 16 additions & 3 deletions src/commands/cmdt/generate/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ export default class Insert extends SfCommand<CreateConfigs> {
const recordConfigs: CreateConfig[] = validateUniqueNames(
parsedRecords.map((record) => ({
typename: flags['type-name'],
recordname: (record[flags['name-column']] as string)
recordname: ensureNameColumnValue(flags['name-column'])(record)
.replace(/[^a-zA-Z0-9]/g, '_') // replace all non-alphanumeric characters with _
.replace(/^(\d)/, 'X$1') // prepend an X if the first character is a number
.replace(/_{2,}/g, '_') // replace multiple underscores with single underscore
.replace(/_$/, ''), // remove trailing underscore (if any)
label: record[flags['name-column']] as string,
label: ensureNameColumnValue(flags['name-column'])(record),
inputdir: flags['input-directory'],
outputdir: flags['output-directory'],
protected: false,
Expand All @@ -107,14 +107,27 @@ export default class Insert extends SfCommand<CreateConfigs> {

// find the cmdt in the inputdir.
// loop through files and create records that match fields
await Promise.all(recordConfigs.map((r) => createRecord(r)));
await Promise.all(recordConfigs.map(createRecord));

this.log(messages.getMessage('successResponse', [flags.csv, flags['output-directory']]));

return recordConfigs;
}
}

/** pass in the column name and record. Makes sure tht the column has a non-empty value */
const ensureNameColumnValue =
(nameColumn: string) =>
(record: Record): string => {
const nameColumnValue = record[nameColumn] as unknown;
if (typeof nameColumnValue !== 'string' || !nameColumnValue.trim().length) {
throw new SfError(
`The column specified for the "name-column" flag (${nameColumn}) must be present in every row of the CSV file.`
);
}
return nameColumnValue;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be a function that returns a function? Seems like it could just be a function that takes the record and column name and it would be simpler to read.


/** validate name fields are unique, otherwise they'll be trying to write to the same file */
const validateUniqueNames = (recordConfigs: CreateConfig[]): CreateConfig[] => {
const recordNameSet = new Set<string>();
Expand Down
Loading