Playing Next Lesson In
seconds

Let's Learn AdonisJS 6 #6.1

Validating Form Data with VineJS

In This Lesson

We'll learn how to validate form data using AdonisJS homegrown validation library, VineJS. With VineJS we can easily ensure our request body consists of properties we need, in the types we need them, with valid values.

Created by
@tomgobich
Published

Join the Discussion 4 comments

Create a free account to join in on the discussion
  1. @guy-aloni

    Great tutorial, thanks!
    How can I validate dates? I get an error Uncaught Exception: Invalid value for "Chapter.startDate". It must be an instance of "luxon.DateTime", looks like date should be cast inside the validator?

    1
    1. Responding to guy-aloni
      @tomgobich

      Hi Guy! Yeah, the VineJS date validation will return back a native JavaScript Date object. You can transform the value returned by the validator into a Luxon DateTime using the transform method.

      import { DateTime } from 'luxon'
      
      const validator = vine.compile(
        vine.object({
          startDate: vine.date().transform((date) => DateTime.fromJSDate(date))
        })
      )
      Copied!
      1
      1. Responding to tomgobich
        @guy-aloni

        Thanks Tom!
        In my case, just if someone has the same problem, I had to add also an ISO format:
        startDate: vine.date({ formats: { utc: true } }).transform((date) => DateTime.fromJSDate(date)),

        2
        1. Responding to guy-aloni
          @tomgobich

          Anytime!! Happy to hear you got it all working!

          1