diff --git a/src/commands/cmdt/generate/records.ts b/src/commands/cmdt/generate/records.ts index ab3c9a75..78b6409e 100644 --- a/src/commands/cmdt/generate/records.ts +++ b/src/commands/cmdt/generate/records.ts @@ -81,12 +81,12 @@ export default class Insert extends SfCommand { 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, @@ -107,7 +107,7 @@ export default class Insert extends SfCommand { // 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']])); @@ -115,6 +115,19 @@ export default class Insert extends SfCommand { } } +/** 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; + }; + /** 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();