Action-specific parameter sanitization

Is there a way to sanitize a random map of params that is used for a specific action? Here's my exact use case: - I have a map coming from query string params - That map may contain extra or invalid keys - I want to filter out any keys that aren't a valid param/argument for that action Right now if I don't sanitize the map and pass it along to an action, it fails with a NoSuchInput error:
Ash.Query.for_read(Account, :search, %{type: :asset, invalid: :key})
#Ash.Query<
...
action: :search,
arguments: %{type: :asset, search: ""}
errors: [
%Ash.Error.Invalid.NoSuchInput{
input: :invalid,
inputs: [:search, :type],
...
}
]
Ash.Query.for_read(Account, :search, %{type: :asset, invalid: :key})
#Ash.Query<
...
action: :search,
arguments: %{type: :asset, search: ""}
errors: [
%Ash.Error.Invalid.NoSuchInput{
input: :invalid,
inputs: [:search, :type],
...
}
]
I see though that Ash obviously knows about the expected parameters (with the arguments keys on the query, and the inputs key on the NoSuchInputError). I have found Ash.Filter.parse_filter/2 but it only sanitizes based on public attributes of a resource, not specific to an action.
Solution:
When calling the action you can do skip_unknown_inputs: :*
Jump to solution
13 Replies
marcofiset
marcofisetOP4mo ago
I know I could do that sanitization manually (which is what I do at the moment actually), but I feel like this is something Ash would offer straight out of the box
Solution
ZachDaniel
ZachDaniel4mo ago
When calling the action you can do skip_unknown_inputs: :*
ZachDaniel
ZachDaniel4mo ago
Or when building the query
marcofiset
marcofisetOP4mo ago
trying that out rn yes, that seems to work! 😄 This is the fastest I've got support for anything ever, thanks @Zach Daniel ! Now, that option does not seem to be available when using Code Interface functions though. If I try like Accounting.search_accounts(%{invalid: :param}, skip_unknown_inputs: :*), it fails with the same NoSuchInputError. Same thing if I try to get a query for that action instead: Accounting.query_to_search_accounts(...) But that's not so bad, I'll convert those calls to Ash.Query.for_read and it'll be okay
kernel
kernel4mo ago
nah you have to define it on the action iirc
marcofiset
marcofisetOP4mo ago
what do you mean @kernel ?
kernel
kernel4mo ago
read :my_little_pony do
skip_unknown_inputs :*
....
end
read :my_little_pony do
skip_unknown_inputs :*
....
end
marcofiset
marcofisetOP4mo ago
oh! I'll try that as well
kernel
kernel4mo ago
that's the only place I've ever seen it 🤷🏿‍♂️
marcofiset
marcofisetOP4mo ago
it works! I like that even more thanks 💜
kernel
kernel4mo ago
it says it takes a list of params also, but I've never been able to get the list working, only :* / [:*]
marcofiset
marcofisetOP4mo ago
it would be impractical to exhaustively list all possible invalid inputs though 😆
kernel
kernel4mo ago
yup

Did you find this page helpful?