Personal Catalogue: React & Firebase — Chapter 2, Exercise 2 ===================================================================== TASK Extend `validateItem()` to also require a `director` field on Dvd and Bluray documents, then run it against one valid and one invalid document of each new type to confirm the new rule actually fires. SOLUTION const REQUIRED_BY_TYPE = { Book: ['author'], Cd: ['artist'], Dvd: ['director'], Bluray: ['director'], }; const VALID_ITEM_TYPES = Object.keys(REQUIRED_BY_TYPE); export function validateItem(data) { if (!data.title) { throw new Error('Every item needs a title.'); } if (!VALID_ITEM_TYPES.includes(data.itemType)) { throw new Error(`itemType must be one of ${VALID_ITEM_TYPES.join(', ')}, got "${data.itemType}".`); } for (const field of REQUIRED_BY_TYPE[data.itemType]) { if (!data[field]) { throw new Error(`A ${data.itemType} needs a ${field}.`); } } if (data.itemType === 'Bluray' && data.resolution && !['1080p', '4K'].includes(data.resolution)) { throw new Error(`resolution must be "1080p" or "4K", got "${data.resolution}".`); } } Only `REQUIRED_BY_TYPE` needed to change — the loop that actually enforces the requirement already reads from that object generically, so adding `'director'` to the Dvd and Bluray arrays is the entire fix. Run: validateItem({ itemType: 'Dvd', title: 'Spirited Away', director: 'Hayao Miyazaki' }); // passes, no error validateItem({ itemType: 'Dvd', title: 'Spirited Away' }); // throws: "A Dvd needs a director." validateItem({ itemType: 'Bluray', title: 'Blade Runner 2049', director: 'Denis Villeneuve', resolution: '4K' }); // passes, no error validateItem({ itemType: 'Bluray', title: 'Blade Runner 2049', resolution: '4K' }); // throws: "A Bluray needs a director." Output confirms the new rule fires for both new types and stays silent when the field is actually present, exactly the same behavior the original Book/Cd checks already had. WHY THIS WORKS AS AN ANSWER ---------------------------- It extends the validator through its own existing generic mechanism (the `REQUIRED_BY_TYPE` lookup) rather than hand-writing a new special case for Dvd and Bluray, and it demonstrates the new rule actually firing with real before/after calls rather than just editing the code and assuming it works.