SefazNfe.Schema (sefaz_nfe v0.1.0)

Copy Markdown View Source

Optional XSD validation against the official NF-e schemas.

Catching a malformed document locally is worth a round trip: SEFAZ answers cStat 225 with no indication of which element is wrong, while the validator names it. It is optional because schemas change with every Nota Técnica, and a library that hard-failed on last quarter's XSD would block the very documents a reforma update requires.

Configuration

Off unless a schema directory is configured:

config :sefaz_nfe, :schemas, "/etc/sefaz/schemas"

Point it at an unpacked Pacote de Liberação from the portal — the directory holding nfe_v4.00.xsd and its imports. The package vendors none: the zip is megabytes, is owned by the RFB, and is versioned by NT rather than by this library (AD-004).

Cost

The schema is compiled on every call. Caching the compiled state looks obvious and is wrong: an :xmerl_xsd state references ETS tables that validation releases, so a cached state validates the first document and then reports every later one as "element not in schema". Compiling costs milliseconds, and validation is opt-in anyway.

Summary

Functions

Whether validation is configured.

Validates xml against schema — a file name inside the configured directory, such as nfe_v4.00.xsd.

Validates against an explicit schema directory, ignoring configuration.

Types

reason()

@type reason() :: {:schema, term()}

Functions

enabled?()

@spec enabled?() :: boolean()

Whether validation is configured.

validate(xml, schema)

@spec validate(String.t(), String.t()) :: :ok | {:error, reason()}

Validates xml against schema — a file name inside the configured directory, such as nfe_v4.00.xsd.

Returns :ok when validation is off, so a caller can wire this in unconditionally and let configuration decide.

validate(xml, schema, directory)

@spec validate(String.t(), String.t(), String.t()) :: :ok | {:error, reason()}

Validates against an explicit schema directory, ignoring configuration.