Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: Tool class validation, baked-in #30

Open
slashequip opened this issue Oct 17, 2024 · 3 comments
Open

Idea: Tool class validation, baked-in #30

slashequip opened this issue Oct 17, 2024 · 3 comments

Comments

@slashequip
Copy link

In a lot of my tools I have to run validation before running my actual logic. This might be checking something exists in the DB first for example, or checking a date has been provided in the correct format.

Would love to see a rules method that I can add to my extends Tool classes that gets picked up and run automagically, exactly like Laravel's Form Requests, returns an array of rules that runs against the data.

I think providing a default message back to the models "Invalid arguments provided" would suffice as an error message, which I guess could be configurable like Laravel's messages() method on Form Requests.

🫡

@slashequip
Copy link
Author

I'm basically doing this inside every __invoke method on a tool class;

$validation = Validator::make(
    compact('title', 'author', 'start_date'),
    [
        'title' => ['required'],
        'author' => ['required'],
        'date' => ['required', 'date_format:Y-m-d'],
    ]
);

if ($validation->fails()) {
    return 'Invalid attributes provided.';
}

@sixlive
Copy link
Contributor

sixlive commented Oct 17, 2024

I'm going to stew on this. I think it probably makes sense to either automatically or opt-in to automatic validation against the parameter schema

@slashequip
Copy link
Author

Nice. Yeah my only thought on that is how do you introduce more complex rules like new Exists() for example.

The JSON schema limits us a little. For dates there is a format: 'date' support on the params that I've used before, but yeah it comes down to needing to be super sure that we've got the exact data.

IMO it does move to more "complex tool case" which you do separate in the docs to the class format.

Just some extra thoughts there :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants