- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 357
Add unique validation rule. #1011
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
base: v5
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the work! You need to check the tests, though.
On a conceptual level, I'm not convinced about this feature-I've had some people complain about any references to a database table. 🤔 I think there's an argument there, that this is an implementation detail we shouldn't mention in the API. The more I think about it, the more I think it's better to just say "Must not be taken".
| $table = $ruleArguments[0] ?? 'table'; | ||
| $column = isset($ruleArguments[1]) | ||
| ? ($ruleArguments[1] === 'NULL' ? 'id' : $ruleArguments[1]) | ||
| : 'id'; // If the column is not specified, assume it is 'id'. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think doing this may cause some issues for people with some other column. Can we change this to not mention a column instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it stands, it does not require the column to be provided.
Rule::unique('vouchers') It is the equivalent Rule::unique('vouchers', 'id')
The ID column is assumed, because Laravel assumes that it is also ID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just go with "Must not be taken" (which is Rails' default unique validation message). Database table and column names are implementation details that API consumers should neither know or care about.
…to add_unique_validation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go with "Must not be taken".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I still think we should not be mentioning the table name. We had something like that before, but there were complaints that we were exposing an implementation detail to API consumers. And that makes sense: if the endpoint is POST /suppliers, but you store in a table called third_parties (for instance, if you do Single Table Inheritance), returning an error message, Must be unique in the "third_parties" table would be confusing.
Sorry it's taken me so long to get back to you on this!
This PR adds support for the
uniquerule.Currently validations that use the
uniquerule were ignored.With this PR:
Without (same rules):

Note: I tried to make
$this->route(‘voucher’)resolvable, so that the description would be:This is useful for update cases where we need to ignore an ID.
However, I couldn't get it to work and I don't even know if it's possible, let me know.