diff --git a/src/TemplateMap.js b/src/TemplateMap.js index e71bc7f46..5ded43f1f 100644 --- a/src/TemplateMap.js +++ b/src/TemplateMap.js @@ -409,18 +409,6 @@ class TemplateMap { } else { let counter = 0; for (let page of map._pages) { - // Data Schema callback #879 - if (typeof page.data[this.config.keys.dataSchema] === "function") { - try { - await page.data[this.config.keys.dataSchema](page.data); - } catch (e) { - throw new EleventyDataSchemaError( - `Error in the data schema for: ${map.inputPath} (via \`eleventyDataSchema\`)`, - e, - ); - } - } - // Copy outputPath to map entry if (!map.outputPath) { map.outputPath = page.outputPath; @@ -478,6 +466,7 @@ class TemplateMap { }; }); + await this.runDataSchemas(orderedMap); await this.populateContentDataInMap(orderedMap); this.populateCollectionsWithContent(); @@ -534,6 +523,28 @@ class TemplateMap { return orderedMap; } + async runDataSchemas(orderedMap) { + for (let map of orderedMap) { + if (!map._pages) { + continue; + } + + for (let pageEntry of map._pages) { + // Data Schema callback #879 + if (typeof pageEntry.data[this.config.keys.dataSchema] === "function") { + try { + await pageEntry.data[this.config.keys.dataSchema](pageEntry.data); + } catch (e) { + throw new EleventyDataSchemaError( + `Error in the data schema for: ${map.inputPath} (via \`eleventyDataSchema\`)`, + e, + ); + } + } + } + } + } + async populateContentDataInMap(orderedMap) { let usedTemplateContentTooEarlyMap = []; for (let map of orderedMap) { diff --git a/test/EleventyTest.js b/test/EleventyTest.js index bddda9c3e..fb37d126b 100644 --- a/test/EleventyTest.js +++ b/test/EleventyTest.js @@ -1201,3 +1201,24 @@ test("Eleventy data schema (fails, using zod) #879", async (t) => { t.is(e.originalError.toString(), 'Validation error: Expected boolean, received number at "draft", or Expected undefined, received number at "draft"'); }); + +test("Eleventy data schema has access to custom collections created via API #879", async (t) => { + t.plan(2); + let elev = new Eleventy("./test/stubs-virtual/", undefined, { + config: eleventyConfig => { + eleventyConfig.addCollection("userCollection", function (collection) { + return collection.getAll(); + }); + + eleventyConfig.addTemplate("index1.html", "", { + eleventyDataSchema: function(data) { + t.is(data.collections.userCollection.length, 1); + } + }); + } + }); + elev.disableLogger(); + + let results = await elev.toJSON(); + t.is(results.length, 1); +});