@rogerpence/rp-utils
    Preparing search index...

    Function validateMarkdownObjects

    • Validates markdown frontmatter against a Zod schema and returns typed results.

      Takes untyped parsed markdown files and validates their frontmatter against the provided Zod schema. Successfully validated files are returned with properly typed frontmatter, while validation errors are collected for review.

      Type Parameters

      • T extends Record<string, any>

        The validated type of the frontmatter (inferred from schema)

      Parameters

      • objects: MarkdownFileResult[]

        Array of parsed markdown files with untyped frontmatter

      • schema: ZodType<T>

        Zod schema defining the expected frontmatter structure

      Returns MarkdownObjectsValidState<T>

      Object containing statistics, errors, and validated files with typed frontmatter

      Validate and use typed frontmatter

      const { successful } = await getMarkdownObjects('./posts');
      const validation = validateMarkdownObjects(successful, TechnicalNoteFrontmatterSchema);

      if (validation.filesValid === validation.filesFound) {
      console.log('All files valid!');
      // validatedObjects have typed frontmatter
      validation.validatedObjects.forEach(obj => {
      console.log(obj.markdownObject.frontMatter.title); // Type-safe!
      });
      } else {
      console.log(`${validation.filesValid}/${validation.filesFound} valid`);
      console.error(validation.validationErrors.join('\n'));
      }

      Write validation errors to file

      const validation = validateMarkdownObjects(files, schema);
      if (validation.validationErrors.length > 0) {
      await writeTextFile(
      validation.validationErrors.join('\n'),
      'validation-errors.txt'
      );
      }