How To Create A Custom VineJS Validation Rule
In this lesson, we'll learn how to make a custom validation rule with VineJS that requires a value to be unique for the provided table and column. We'll learn how we can register this rule for both strings and number types.
- Author
- Tom Gobich
- Published
- Apr 12
- Duration
- 9m 7s
Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.
Adocasts
Burlington, KY
Transcript
How To Create A Custom VineJS Validation Rule
-
(upbeat music)
-
So we can create our own rules with VineJS as well.
-
And the documentation does a fantastic job
-
of walking through this.
-
And the example that they give is great as well.
-
So we're gonna walk through that in this lesson
-
in the context of AdonisJS.
-
So we're gonna be provided a couple of things
-
whenever we create a rule.
-
We're gonna be provided the value,
-
which will be of type unknown.
-
So we're gonna need to discern what the type is
-
before we actually attempt to use it.
-
Options accepted by the rule.
-
These are options that we can define as is.
-
So this could be something like a table or a column
-
to check the database to see whether or not
-
a value is unique.
-
That's something defined by us.
-
And if we define the rule to have nothing as options,
-
then the second parameter will be undefined.
-
Then comes the field context.
-
This is contextual information about the actual field data.
-
So things like the value, data, metadata,
-
whether the field is valid, defined.
-
You can mutate the value, report errors,
-
field parent, name, and whether or not it's an array member.
-
So that's what's confined within the field context.
-
So to start, we're going to need a place
-
to house our new validation rule.
-
For this, we'll want to use a preload file.
-
So we can stop our server.
-
Let's clear that out.
-
Let's do node ace make reload.
-
We'll put these inside of a directory called rules,
-
and we're gonna follow along with the documentations example.
-
So we'll create a unique rule to simplify the unique checks
-
throughout our validation example.
-
Remember, right now within our application,
-
if we dive inside of our auth validator,
-
our unique check looks something like this,
-
where we're taking a callback function
-
that's provided into the database,
-
manually compiling out that query,
-
and then returning back whether or not we found results.
-
So we're gonna wrap this up in a pretty little bow,
-
where all that we need to do is provide in the table
-
and the column that we want to check,
-
and the check will happen automatically.
-
So let's hide that back away.
-
Let's run that to create our preload.
-
We'll have it register it within our AdonisRC.ts file,
-
and we're good to go there.
-
Let's go ahead and boot our server back up while we're here.
-
All right, let's hide that back away
-
and dive into our text editor,
-
and let's head into our new preload file.
-
So that's within our start directory.
-
We created that within a folder called rules,
-
and there's our file for unique.
-
Within here, we're gonna want an async function.
-
We'll call this function our rules name.
-
Within the documentation, they call this unique.
-
However, I don't want to overwrite
-
the current unique check that we have.
-
We'll leave this in place.
-
So let's name our simplified rule something different,
-
maybe something like is unique.
-
Our parameters to this method will then be the value,
-
which as the documentation noted, is of type unknown.
-
So we need to discern what type the actual value holds
-
before we can use it.
-
Then options.
-
By default, if we don't want our method
-
to accept any options, this will be undefined.
-
We do want ours to accept options,
-
but just to get us going,
-
let's stub that to undefined for right now.
-
And then our field information,
-
which is of type field context,
-
which we can import from vine.js/vine-types.
-
Since we're specifically going to use that just as a type,
-
we'll import that as a type there as well.
-
So for our options,
-
let's go ahead and create a type for that.
-
So we'll do type options equals object.
-
We'll have one option be our table.
-
So the table that we should check against for the column,
-
this will be a type string,
-
and then the column that we should check against
-
inside of that table.
-
Let's jump down to our options
-
and set that as the type value there.
-
The first thing we're going to want to do
-
is check the type of our value.
-
For the documentation,
-
they show this as just being a string
-
and they kick out anything that is not a string,
-
but I could see this being used for an ID as well.
-
So I think we'll allow numbers within ours as well.
-
So let's do if type of value does not equal string
-
and type of value does not equal number,
-
then we'll just want to return back out of this method
-
so that this particular method
-
doesn't handle either of those types.
-
We're going to add our is unique method
-
as an option onto vine string and vine number.
-
So if the value doesn't match one of those two types,
-
the vine string or vine number validation
-
will handle that accordingly.
-
So within our actual unique check,
-
we don't need to report the error at this point
-
because that vine string or vine number check
-
will take care of that for us.
-
We can just ignore the value here.
-
Then if our value is a string or a number,
-
we can move onward.
-
So we'll do const result equals await,
-
and then we're going to want to import the database module
-
because we don't know what particular model
-
we're going to be working with here.
-
So we can use the database module to accept in the table
-
that we should check against and hone that in
-
on the particular column to check and see
-
whether or not we have a value.
-
So import db from at AdonisJS,
-
Lucid services, db, we can scroll back down
-
and make use of that db query builder.
-
So db.from, provide in our options.table,
-
select just the options.column that we want to hone in on
-
where that options.column equals the value
-
that's provided in.
-
And then if any of them exist inside of the database,
-
the is unique check will be false.
-
So we can just stop at the first found record
-
and get that back as our result.
-
So if we have a result,
-
then we'll want to report that the value is not unique.
-
So we'll do field, and then we can use that report method
-
that we saw inside of the documentation to report an error.
-
The first argument here is going to be that error message.
-
The second argument is going to be the rule that failed.
-
And then the third argument is going to be
-
the field information for our validation.
-
So for this value, we can do something like this
-
and then reach for interpolation and specify in the field.
-
And this would plop something in like this.
-
And then if we're validating an email,
-
this would be this email is already taken
-
or something like that.
-
The rule here is is unique.
-
And then the field information we can provide in
-
as the third argument.
-
If this rule is valid, however,
-
we don't want to report an error.
-
And instead we can just escape out of this function
-
so we don't need to do anything else.
-
Okay, so we have the function handler
-
for our rule fully defined.
-
Now we need to register it with VineJS.
-
So first thing we're going to want to import find
-
from VineJS/find.
-
Scroll back down and let's do const is unique rule
-
equals find.
-
And there's a method on here called create rule
-
that will actually create a find validator rule
-
for the function that we provide in.
-
So we'll just want to provide in our is unique function.
-
There, now we've actually created a find rule
-
with our is unique method.
-
And the full rule is our is unique rule variable.
-
We could export this so that we could use it
-
as needed elsewhere.
-
But as stated earlier, we could also add this directly
-
onto the types that we want this rule
-
to be available for as well,
-
which in our case is both our string and our numbered types.
-
So let's import find string,
-
which is the rule set for the find string validator
-
and then find number,
-
which is the same thing for the number validator.
-
Scroll back down and let's do a macro for both of those.
-
So we'll do find string first,
-
create a macro called is unique.
-
We'll create a block level function that accepts in this,
-
which is of type find string.
-
And this is an instance of the actual validation rule
-
that's being built specifically for the string type.
-
And then the second argument will want to be
-
the actual options that we accept
-
into our new is unique validation rule.
-
So that's going to be the options type
-
that we defined at the top of this file.
-
It's defined as a block level function
-
and then return this dot.
-
And we can use to push our validation rule
-
into the applied validations
-
for this specific find string instance.
-
So for this use method,
-
we'll want to provide in our is unique rule,
-
not the actual is unique function.
-
So we'll do is unique rule.
-
We'll call that as a function
-
and provide the options into it.
-
Okay, now we're getting a red squiggly
-
on our is unique string because is unique
-
is not assignable to a parameter uptight key find string
-
because it has not been defined on the actual type set.
-
So we want to do that next.
-
So let's declare module at find JS slash bind.
-
Then we'll want to reach for the bind string interface.
-
So bind string there and add in our is unique rule
-
and designate that this accepts in options of type options
-
and returns back the validation rule builder of type this.
-
And now we should have that readily available to us.
-
Furthermore, if we now dive back into our auth validator,
-
before where we call this unique,
-
let's attempt to do now dot is unique
-
and there is our brand new rule.
-
And if we attempt to call it,
-
we'll see that it accepts in our options.
-
So we can provide in our object with a type table.
-
For this, we want to check the users table
-
and provide in the column.
-
Then we'll want to check the email column there.
-
So now we should be able to get rid
-
of this big function call
-
and just use our brand new is unique rule.
-
And additionally, we've left that unique method there
-
just in case we need it in the future
-
for more expandability.
-
Okay, let's jump back into our unique rule
-
and apply this for our number types as well.
-
It's gonna look very similar to this.
Join The Discussion! (4 Comments)
Please sign in or sign up for free to join in on the dicussion.
redeemefy
Good content so far. Thanks for putting it together. Keep up the hard work. This course indeed is going to help me start my own business. Praying to God to give me the strength to move forward with it.
Please sign in or sign up for free to reply
tomgobich
Thank you, redeemefy!! I'm happy to hear it's helping, and best of luck with starting your business! You got this, just take it one line at a time!! :D
Also - apologies your Adocasts Plus badge was missing! Looks like it was a concurrency issue with the Stripe Webhook, I'll have to get that fixed up.
Please sign in or sign up for free to reply
redeemefy
Nice listening with those Stripe web-hooks. I use those myself at work when Stripe was our provider.
Please sign in or sign up for free to reply
tomgobich
Adocasts has been my first time working with Stripe, we do more direct B2B sales where I work, so it's been a fun learning experience lol. :D
Please sign in or sign up for free to reply