# `ExDataSketch.Binary.Validator`
[🔗](https://github.com/thanos/ex_data_sketch/blob/main/lib/ex_data_sketch/binary/validator.ex#L1)

Structured validation primitives for EXSK frames.

This module exposes the individual checks that `ExDataSketch.Binary.Header.decode/1`
performs as discrete, composable functions. Tests, fuzzers, and tools
(e.g., a hypothetical "exsk inspect" CLI) can use these to surface
precisely which check failed without parsing the full frame.

All validators follow a uniform contract:

- Return `:ok` on success.
- Return `{:error, %ExDataSketch.Errors.DeserializationError{}}` on failure.
- NEVER crash the BEAM.

## See also

- `ExDataSketch.Binary.Header` — the main decode pipeline.
- `ExDataSketch.Binary.CRC` — checksum algorithm.
- `plans/corruption_detection.md` — full error taxonomy.

# `check_crc`

```elixir
@spec check_crc(binary()) :: :ok | {:error, Exception.t()}
```

Verifies the trailing CRC32C of a frame against the recomputed checksum.

Accepts the *full* frame including the trailing 4-byte CRC32C. Returns
`:ok` if the recomputed CRC over `bin[0 .. -5]` equals the declared
trailer; an error otherwise.

Useful in fuzz tests that want to assert a single bit-flip is detected.

## Examples

    iex> meta = ExDataSketch.Hash.Metadata.new(:xxhash3, 0, 1, 1, :rust)
    iex> frame = ExDataSketch.Binary.Header.encode(meta, <<1, 2, 3>>)
    iex> ExDataSketch.Binary.Validator.check_crc(frame)
    :ok

# `check_magic`

```elixir
@spec check_magic(binary()) :: :ok | {:error, Exception.t()}
```

Returns `:ok` when the leading 4 bytes are the EXSK magic.

## Examples

    iex> ExDataSketch.Binary.Validator.check_magic("EXSK" <> <<0, 0>>)
    :ok

    iex> match?({:error, _}, ExDataSketch.Binary.Validator.check_magic("BAAD"))
    true

# `check_minimum_v2_size`

```elixir
@spec check_minimum_v2_size(binary()) :: :ok | {:error, Exception.t()}
```

Returns `:ok` when the binary is at least the v2 minimum frame size.

## Examples

    iex> ExDataSketch.Binary.Validator.check_minimum_v2_size(:binary.copy(<<0>>, 32))
    :ok

    iex> match?({:error, _}, ExDataSketch.Binary.Validator.check_minimum_v2_size(<<1, 2, 3>>))
    true

# `check_version`

```elixir
@spec check_version(binary(), non_neg_integer()) :: :ok | {:error, Exception.t()}
```

Returns `:ok` when the frame is the expected version.

## Examples

    iex> ExDataSketch.Binary.Validator.check_version("EXSK" <> <<2>>, 2)
    :ok

    iex> match?({:error, _}, ExDataSketch.Binary.Validator.check_version("EXSK" <> <<1>>, 2))
    true

---

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