Skip to content
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
38 changes: 30 additions & 8 deletions src/xlsform/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,39 @@ export class XLSValidator {
const code = (choice.name ?? '').toString().trim();
if (!code) return null; // reported as a missing code
const where = `choice "${code}" (list "${String(choice.list_name ?? '').trim()}")`;
const labels = this.choiceLabels(choice);
if (labels.size === 0) return `${where} has no label`;
const missing = [...labels]
.filter(([, text]) => text === '')
.map(([k]) => k);
if (missing.length === 0) return null;
if (missing.length === labels.size) return `${where} has no label`;
return `${where} has no label in: ${missing.join(', ')}`;
}

/**
* A choice's label text per language, trimmed (`''` = empty). Covers the
* loader's shape (`label` as a string, or `{lang: text}` with `_languages`)
* and raw rows that keep `label::<lang>` columns (Kobo's layout once a form
* declares a language). A plain `label` is keyed `''`.
*/
private static choiceLabels(choice: ChoiceRow): Map<string, string> {
const out = new Map<string, string>();
const text = (v: unknown) =>
typeof v === 'string' || typeof v === 'number' ? String(v).trim() : '';
const label = choice.label;
if (label !== null && typeof label === 'object') {
const langs = choice._languages ?? Object.keys(label);
const missing = langs.filter(
(lang) => String(label[lang] ?? '').trim() === '',
);
if (missing.length === 0) return null;
if (missing.length === langs.length) return `${where} has no label`;
return `${where} has no label in: ${missing.join(', ')}`;
for (const lang of choice._languages ?? Object.keys(label)) {
out.set(lang, text(label[lang]));
}
} else if (label !== undefined) {
out.set('', text(label));
}
for (const [key, value] of Object.entries(choice)) {
if (key.startsWith('label::'))
out.set(key.slice('label::'.length), text(value));
}
return String(label ?? '').trim() === '' ? `${where} has no label` : null;
return out;
}

/** Type, choice-list and appearance findings for one survey row. */
Expand Down
34 changes: 34 additions & 0 deletions tests/ts/unit/subsetValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,37 @@ describe("validateSubset — target 'ddi' (#52)", () => {
).toContainEqual(expect.objectContaining({ severity: 'error' }));
});
});

describe('validateSubset — label::<lang> choice columns (#75)', () => {
const warn = (choices: Record<string, unknown>[]) =>
XLSValidator.validateSubset(
[{ type: 'select_one g', name: 'q', 'label::English': 'Q' }],
choices,
{ target: 'ddi' },
).map((v) => v.message);

test('a label::<lang> column counts as a label', () => {
expect(
warn([{ list_name: 'g', name: 'm', 'label::English': 'Male' }]),
).toEqual([]);
});

test('names the language columns that are empty', () => {
expect(
warn([
{
list_name: 'g',
name: 'm',
'label::English': 'Male',
'label::Deutsch': '',
},
]),
).toEqual(['choice "m" (list "g") has no label in: Deutsch']);
});

test('still warns when every label column is empty', () => {
expect(
warn([{ list_name: 'g', name: 'm', 'label::English': ' ' }]),
).toEqual(['choice "m" (list "g") has no label']);
});
});
Loading