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.

Published
Apr 09
Duration
9m 29s

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

Join The Discussion! (4 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 2 months ago

    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

    Please sign in or sign up for free to reply

    1. Commented 2 months ago

      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

      Please sign in or sign up for free to reply

      1. Commented 2 months ago

        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)),

        1

        Please sign in or sign up for free to reply

        1. Commented 2 months ago

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

          1

          Please sign in or sign up for free to reply

Playing Next Lesson In
seconds