Ootempl.Validator (ootempl v0.3.0)

Provides validation functions for .docx files.

This module validates .docx file structure, ensuring files are valid ZIP archives, contain required files, and have well-formed XML content.

Required .docx Files

All valid .docx files must contain:

  • word/document.xml - Primary document content
  • [Content_Types].xml - MIME type definitions
  • _rels/.rels - Package-level relationships

Validation Workflow

Use validate_docx/1 to run all validation checks:

case Ootempl.Validator.validate_docx("template.docx") do
  :ok -> # File is valid
  {:error, exception} -> # File is invalid, exception has details
end

Or use individual validation functions for specific checks:

  • validate_archive/1 - Check if file is a valid ZIP
  • validate_structure/1 - Verify required files exist
  • validate_xml/1 - Check if XML string is well-formed

Summary

Functions

Runs all validation checks on a .docx file.

Functions

validate_docx(path)

Runs all validation checks on a .docx file.

Performs the following validations in order:

  1. File exists and is a regular file
  2. File is a valid ZIP archive
  3. All required files are present
  4. The main document XML is well-formed

Returns :ok if all checks pass, or {:error, exception} with a specific error type indicating the first validation failure.

Examples

iex> Ootempl.Validator.validate_docx("template.docx")
:ok

iex> Ootempl.Validator.validate_docx("nonexistent.docx")
{:error, %Ootempl.ValidationError{reason: :file_not_found}}

iex> Ootempl.Validator.validate_docx("corrupt.docx")
{:error, %Ootempl.InvalidArchiveError{}}