Bylaw.Credo.Check.Ecto.OwnContextForSchema (bylaw_credo v0.1.0-alpha.1)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of higher and works with any version of Elixir.

Explanation

Each Ecto schema using a configured schema wrapper should live under its own dedicated context module.

Examples

Configure the schema wrapper modules that identify application schemas:

{Bylaw.Credo.Check.Ecto.OwnContextForSchema,
 [
   schema_modules: [MyApp.Schema]
 ]}

Avoid: ToolCall is nested under the Runs context:

  defmodule MyApp.Runs.ToolCall do
    use MyApp.Schema
  end

Prefer: ToolCall has its own context:

  defmodule MyApp.ToolCalls.ToolCall do
    use MyApp.Schema
  end

Notes

Keeping one schema per context ensures that context modules stay small and focused. When a schema is nested under another schema's context (e.g. MyApp.Runs.ToolCall), the context tends to accumulate unrelated responsibilities.

This check uses static AST analysis, so it favors clear source-level patterns over runtime behavior.

Options

Configure options in .credo.exs with the check tuple:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Ecto.OwnContextForSchema,
         [
           schema_modules: [MyApp.Schema],
           excluded_modules: ["MyApp.Legacy.LegacySchema"]
         ]}
      ]
    }
  ]
}
  • :schema_modules - Schema wrapper modules that identify application schemas to check.
  • :excluded_modules - List of fully qualified module names (as strings) to exclude from this check.

Usage

Add this check to Credo's checks: list in .credo.exs:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Ecto.OwnContextForSchema,
         [
           schema_modules: [MyApp.Schema]
         ]}
      ]
    }
  ]
}

Check-Specific Parameters

Use the following parameters to configure this check:

:schema_modules

Schema wrapper modules that identify application schemas to check.

This parameter defaults to [].

:excluded_modules

List of fully qualified module names (as strings) to exclude from this check.

This parameter defaults to [].

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.