# `SefazNfe.Schema`
[🔗](https://github.com/devaction-labs/sefaz_nfe/blob/v0.5.0/lib/sefaz_nfe/schema.ex#L1)

Optional XSD validation against the official NF-e, CT-e and MDF-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).

## CT-e 4.00 and MDF-e 3.00 (bundled)

The CT-e and MDF-e schemas ship with the package, under `priv/schemas`, so a
host does not have to unpack anything for them:

  * `cte_4.00`: *Pacote de Liberação* `PL_CTe_400_NT2026.005 RTC SN_1.00`
    (CT-e portal, `dfe-portal.svrs.rs.gov.br/Cte/Documentos`): `cte_v4.00.xsd`,
    `cteOS_v4.00.xsd`, `cteSimp_v4.00.xsd`, `eventoCTe_v4.00.xsd` and the rest;
  * `mdfe_3.00`: `PL_MDFe_300b_NT012025_1.04` (`dfe-portal.svrs.rs.gov.br/Mdfe/Documentos`):
    `mdfe_v3.00.xsd`, `eventoMDFe_v3.00.xsd` and the rest.

`validate/2` picks them by file name (`bundled?/1`), so the same call that
validates an NF-e validates a CT-e:

    Schema.validate(cte_xml, "cte_v4.00.xsd")
    Schema.validate(event_xml, "eventoCTe_v4.00.xsd")
    Schema.validate(mdfe_xml, "mdfe_v3.00.xsd")

Validation stays off until `:schemas` is configured. To validate only the
bundled CT-e and MDF-e schemas, with no NF-e directory, configure `:bundled`:

    config :sefaz_nfe, :schemas, :bundled

In that mode a schema the package does not ship (the NF-e's) is not checked
and returns `:ok`. `SefazNfe.CTe.authorize/1` and `SefazNfe.MDFe.authorize/1`
validate the document they are about to send whenever validation is enabled.
`validate_bundled/2` validates regardless of configuration.

The bundled schemas are checked with `xmllint` (libxml2), which must be on the
`PATH`: `:xmerl_xsd` cannot compile the CT-e package (it leaves the RTC
simple types derived from `TStringRTC` unresolved) and refuses a signed
document (`Reference URI="#CTe..."` is not an `anyURI` to it). `xmllint`
accepts both, so the signed document, `infCTeSupl` included, is what is
validated. Without `xmllint` the result is `{:error, {:schema, :xmllint_not_found}}`.
An invalid document is `{:error, {:schema, [line, ...]}}`, one line of
`xmllint` output per violation.

## 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.

# `reason`

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

# `bundled`

```elixir
@spec bundled() :: [String.t()]
```

The bundled schema file names, sorted.

# `bundled?`

```elixir
@spec bundled?(String.t()) :: boolean()
```

Whether `schema` is one of the CT-e or MDF-e schemas the package ships.

# `enabled?`

```elixir
@spec enabled?() :: boolean()
```

Whether validation is configured.

# `validate`

```elixir
@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`

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

Validates against an explicit schema directory, ignoring configuration.

# `validate_bundled`

```elixir
@spec validate_bundled(String.t(), String.t()) :: :ok | {:error, reason()}
```

Validates against a CT-e or MDF-e schema shipped with the package
(`bundled?/1`), ignoring configuration. Needs `xmllint` on the `PATH`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
