Transcript
-
So up until recently, if you wanted to perform a unique or exist check with Vine.js, you would write something like this right here
-
where you have your base set of rules and then you would chain off of it, your unique or exists check with a callback function that would then perform the actual uniqueness check
-
inside of your database. On December 4th with Lucid version 21.5.0, an alternative overload was added in for both the unique and exists check that instead gives you an additional option
-
more similar to AdonisJS 5's validator. And the awesome part about this is that it's a non-breaking change, meaning that this callback approach still works perfectly fine in this new version.
-
This new approach was added in as an overload. So if we type in our email rule once more and we use our base email rule there and then tack on the unique,
-
we see that we now have two overload options. We have this Vine DB search options as well as the Vine DB search callback, the callback being what we have commented out right below.
-
So let's dig into the Vine DB search options to see what it accepts. So we can command click on unique there and then command click on Vine DB search options to dig into that type.
-
You'll see that it accepts a table column. We can specify a manual connection if we wish and then whether or not it should be case sensitive. And in addition to that,
-
we also have the ability to add additional filters using a database callback here as well. So let's dig back out of those types, back into our validator and let's see what that would look like.
-
So we'll add our options in, we'll have our table, which will be our users table still, our column, which will be the email column as we're using the unique rule here
-
to verify that this email is not already taken in our database. And we can also specify that we want this to be case and sensitive with its search. So we'll set that to true.
-
However, our normalized email rule will already by default take care of that for us. It's always good to go ahead and verify that here as well. And then on top of that,
-
we also have the additional filter options that we can add onto this as well, which accepts in the database, the email value that's trying to be validated, as well as the field context information.
-
And for this, the DB is our query already. So we can just tack on additional where's where we see fit. So if we wanted this unique rule to only check verified emails,
-
we could do something like where is verified is true. And now our unique rule would only apply to user emails where that user is verified. In our case, we don't want that additional filter
-
as I don't have that column inside of my database. We can jump back into our browser. Let's register somebody in here. So Bobby Hill, bobby@hill.com for the email,
-
and then just something there for the password. All right, we are logged in and registered. We can now log back out, jump back over to our register, and let's try registering that same person yet again.
-
So bobbyhill, bobby@hill.com, and then whatever there for the password. If we hit enter, there we go. Our uniqueness check is still working A-okay, despite us using that new callback approach.
-
And again, that new option set is available for both the unique and exist validation rules with VineJS
New Unique & Exist Validation Overloads in AdonisJS 6
We'll cover the new overload option recently added to Lucid's VineJS unique and exists rules allowing for a simplified usage for common use-cases.

- Created by
- @tomgobich
- Published
Join the Discussion 0 comments
Be the first to comment!