AshIntrospection.Rpc.FieldProcessing.Validation (AshIntrospection v0.3.0)

View Source

Field validation helpers for the FieldSelector module.

Provides validation functions for checking field selections are valid and properly structured.

This is a shared module used by both AshTypescript and AshKotlinMultiplatform.

Summary

Functions

Checks for duplicate field names in a field selection list.

Checks whether field_specs declares field_name.

Validates that nested fields are non-empty for fields that require selection.

Types

config()

@type config() :: %{optional(:input_field_formatter) => atom()}

Functions

check_for_duplicates(fields, path, config \\ %{})

@spec check_for_duplicates(list(), list(), config()) :: :ok

Checks for duplicate field names in a field selection list.

Normalizes field names using the input formatter before checking for duplicates. Throws {:duplicate_field, field_name, path} if duplicates are found.

This runs before any field-existence check, so it sees every name a client sent, valid or not. A name that matches no existing atom stays a string here and is reported as such; the caller rejects it as an unknown field moments later.

Parameters

  • fields - List of field selections
  • path - Current path for error reporting
  • config - Configuration map with :input_field_formatter

field_exists?(field_specs, field_name)

@spec field_exists?(
  keyword(),
  term()
) :: boolean()

Checks whether field_specs declares field_name.

Accepts a string name, which Keyword.has_key?/2 rejects outright. A client name that resolved to no existing atom stays a string, and no field spec can be keyed by one, so the answer is always false — the caller then reports it as an unknown field.

validate_field_exists!(field_name, field_specs, path, error_type \\ "field_constrained_type")

@spec validate_field_exists!(atom() | String.t(), keyword(), list(), String.t()) ::
  :ok

Validates that a field exists in the given field specs.

Throws {:unknown_field, field_name, error_type, path} if not found.

Parameters

  • field_name - The field name to check
  • field_specs - Keyword list of field specifications
  • path - Current path for error reporting
  • error_type - Error type string for error messages

validate_non_empty(nested_fields, field_name, path, error_type \\ :relationship)

@spec validate_non_empty(term(), term(), list(), atom()) :: :ok

Validates that nested fields are non-empty for fields that require selection.

Throws appropriate errors if validation fails.

Parameters

  • nested_fields - The nested field selections
  • field_name - The field name for error messages
  • path - Current path for error reporting
  • error_type - The type of error to throw (default: :relationship)