Search documentation of Ragex

# Ragex.Editor.Validator behaviour (Ragex v0.4.0)

View Source

Validation pipeline orchestration for code editing.

Coordinates validation across different language-specific validators. Automatically selects the appropriate validator based on file extension.

## Summary

### Callbacks

can_validate?(file_path)
validate(content, opts)

Callback for language-specific validators.

### Functions

can_validate?(opts)

Checks if validation is available for a given file or language.

get_validator(language)

Gets the validator module for a specific language.

list_validators()

Lists all available validators.

validate(content, opts \\ [])

Validates code content using the appropriate language validator.

## Callbacks

# can_validate?(file_path)

    @callback can_validate?(file_path :: String.t()) :: boolean()

# validate(content, opts)

    @callback validate(content :: String.t(), opts :: keyword()) ::
      {:ok, :valid} | {:error, [Ragex.Editor.Types.validation_error()]}

Callback for language-specific validators.

Validators must implement this behavior to participate in the validation pipeline.

## Functions

# can_validate?(opts)

    @spec can_validate?(keyword()) :: boolean()

Checks if validation is available for a given file or language.

## Examples

    iex> Validator.can_validate?(path: "test.ex")
    true

    iex> Validator.can_validate?(path: "test.unknown")
    false

# get_validator(language)

    @spec get_validator(atom()) :: {:ok, module()} | {:error, :no_validator}

Gets the validator module for a specific language.

# list_validators()

    @spec list_validators() :: %{required(atom()) => module()}

Lists all available validators.

Returns a map of language to validator module.

# validate(content, opts \\ [])

    @spec validate(
      String.t(),
      keyword()
    ) ::
      {:ok, :valid | :no_validator}
      | {:error, [Ragex.Editor.Types.validation_error()]}

Validates code content using the appropriate language validator.

## Parameters

- `content`: Code content to validate
- `opts`: Options
  - `:path` - File path (used for language detection)
  - `:language` - Explicit language override
  - `:validator` - Explicit validator module override

## Returns

- `{:ok, :valid}` if code is valid
- `{:error, errors}` if validation fails
- `{:ok, :no_validator}` if no validator available for language

## Examples

    iex> Validator.validate("defmodule Test do\nend", path: "test.ex")
    {:ok, :valid}

    iex> Validator.validate("defmodule Test", path: "test.ex")
    {:error, [%{message: "unexpected end of file", ...}]}