Ash FrameworkAF
Ash Framework7mo ago
2 replies
Abhishek Tripathi

ash extensions

I want to write an ash extension that works with ANY action. it is a custom validator + resolver

Aim: Validates that file exits and resolves the relative path to absolute path.

Ideally this DSL would do

actions do
    # Example action that validates a document upload
    action :process_document, :map do

>>>>>>>>>>>>>>>>>> 

      validate_file :document_path,
        max_size: "5.megabytes",
        allowed_types: [:pdf],
        base_path: "priv/uploads"

>>>>>>>>>>>>>>>>> 

      argument :document_path, :string, allow_nil?: false

      run fn input, _context ->
        # By the time we get here, document_path has been validated and
        # converted to an absolute path
        path = input.arguments.document_path

        {:ok, %{
          status: :processed,
          path: path,
          size: File.stat!(path).size
        }}
      end
    end




i am inspired by
https://hexdocs.pm/ash/dsl-ash-resource.html#actions-create-validate

so this validate block is inside the action block.

i looked at the original ash code But that does not mention validate.

So there are two questions:
1. how does ash include validate inside it's own action block?
2. how can I do the same?

My attempt is here:
https://github.com/TwistingTwists/ash_file_utils/blob/main/lib/example_resource.ex

the error I have is:
actions is exported both by Ash and my AshFileUtils.
GitHub
Contribute to TwistingTwists/ash_file_utils development by creating an account on GitHub.
ash_file_utils/lib/example_resource.ex at main · TwistingTwists/as...
GitHub
A declarative, extensible framework for building Elixir applications. - ash-project/ash
ash/lib/ash/resource/dsl.ex at 30c5b6e6db60a9a79c5c9de70e7c403e4a97...
Was this page helpful?