Scrutinex (Scrutinex v0.2.0)

Copy Markdown View Source

Validates tabular data (lists of maps) against a schema definition.

Define a schema using Scrutinex.Schema, then validate data with validate/2 or validate!/2.

Quick Start

defmodule MySchema do
  use Scrutinex.Schema

  column "name",  :string,  checks: [length: [min: 1, max: 100]]
  column "age",   :integer, coerce: true, checks: [number: [greater_than: 0]]
  column "email", :string,  checks: [format: ~r/@/]
end

result = Scrutinex.validate(data, MySchema)

if result.valid? do
  process(result.data)
else
  handle_errors(result.errors)
end

See Scrutinex.Schema for the full DSL reference, supported types, and check options.

Summary

Functions

Validates a list of row maps against the given schema module.

Validates a list of raw header strings against the given schema module.

Functions

validate(data, schema_module, opts \\ [])

@spec validate([map()], module(), keyword()) :: Scrutinex.Result.t()

Validates a list of row maps against the given schema module.

Returns a Scrutinex.Result struct with :valid?, :data (with coerced values where applicable), and :errors.

Options

  • :max_errors - stop validation after accumulating this many errors. Useful for quick validity checks or limiting output on large datasets.

Examples

result = Scrutinex.validate(data, MyApp.UserSchema)
result.valid?   # => true or false
result.errors   # => [%Scrutinex.Error{}, ...]

# Stop after 10 errors
result = Scrutinex.validate(data, MyApp.UserSchema, max_errors: 10)

validate!(data, schema_module, opts \\ [])

@spec validate!([map()], module(), keyword()) :: [map()]

Like validate/2, but raises Scrutinex.ValidationError on failure.

On success, returns the list of coerced row maps directly (not wrapped in a Result struct). On failure, the raised ValidationError contains the full Result in its :result field.

Examples

# Success: returns coerced data
rows = Scrutinex.validate!(data, MyApp.UserSchema)
#=> [%{"name" => "Alice", "age" => 30}, ...]

# Failure: raises with result attached
try do
  Scrutinex.validate!(bad_data, MyApp.UserSchema)
rescue
  e in Scrutinex.ValidationError ->
    e.result.errors  #=> [%Scrutinex.Error{}, ...]
end

validate_headers(headers, schema_module)

@spec validate_headers([String.t()], module()) ::
  :ok | {:error, [Scrutinex.Error.t()]}

Validates a list of raw header strings against the given schema module.

Checks for duplicate headers, missing required columns, and (in strict mode) unexpected columns. All errors have row: nil since they are schema-level.

Returns :ok when the headers satisfy all structural requirements, or {:error, errors} with a list of Scrutinex.Error structs.

validate_headers!(headers, schema_module)

@spec validate_headers!([String.t()], module()) :: :ok

Like validate_headers/2, but raises Scrutinex.ValidationError on failure.

On success, returns :ok. On failure, the raised ValidationError contains the errors in its :result field with valid?: false.