Skip to content

[feat] Add database constraints for validation annotations#1085

Open
Klaas058 wants to merge 5 commits intospatie:mainfrom
Klaas058:feature/annotation-validation-database-constraints
Open

[feat] Add database constraints for validation annotations#1085
Klaas058 wants to merge 5 commits intospatie:mainfrom
Klaas058:feature/annotation-validation-database-constraints

Conversation

@Klaas058
Copy link
Copy Markdown
Contributor

Currently when using annotations with database rules like Exists and Unique, it is not possible to include database constraints.

This will throw a php error:

// Error: Constant expression contains invalid operations
#[Exists(Customer::class, column: 'id', where: fn($query) => $query->where('active', true))]
public ?int $customer_id

This feature would allow using database constraints like so:

// Valid php 🎉
#[Exists(Customer::class, column: 'id', where: new WhereConstraint('active', true))]
public ?int $customer_id

After testing this has been working great for us, happy with any feedback

One thing I'm not a 100% sure about:

  • Types in the DatabaseConstraint implementations are mixed since the Laravel methods are untyped themselves. Is this the best way?

Copy link
Copy Markdown
Member

@rubenvanassche rubenvanassche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great, few changes required and then this one is good to go!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants