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
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
ash_file_utils/lib/example_resource.ex at main · TwistingTwists/as...
Contribute to TwistingTwists/ash_file_utils development by creating an account on GitHub.
GitHub
ash/lib/ash/resource/dsl.ex at 30c5b6e6db60a9a79c5c9de70e7c403e4a97...
A declarative, extensible framework for building Elixir applications. - ash-project/ash
1 Reply
abeeshake456
abeeshake456OP4mo ago
here is a patch Which i don't undestand fully
| Scrapfly
Free online tool to visualize git patches and diffs with syntax highlighting, file tree navigation, and side-by-side comparison. Perfect for code reviews and pull request analysis.

Did you find this page helpful?