Warm tip: This article is reproduced from serverfault.com, please click

Validate dates then transfer it to iso string react

发布于 2020-12-07 04:31:02

My problem is that I need to validate my date using any validation library (joi, yup, superstruct..etc) and after that, I need to cast the date object to iso string

I use Flatpikr , react-hook-form , joi OR yup

This my approach at first glance

  1. Flatpickr >> Date >> Validate >> toISOString() if validation succeed

I tried to achieve the first approach via yup but no hope

I tried to make a pre and post transform in yup

I opened a question in their repository explaining my steps

https://github.com/jquense/yup/issues/1153

My approach in joi didn't succeed also

i used this code

joi
  .date()
  .iso()
  .required()
  .min(new Date())
  .messages({
    'any.required': `Required.`,
    'date.format': `Required`,
    'date.base': `Should be a type of number`,
    'date.min': `The date should be in future`,
  });

and i have used

{ convert : false }

in joi options to prevent joi from converting the values

when setting convert to false, all of my validation schemas succeed even if my variables was required or empty

Questioner
Ahmed Ahmed Sayed
Viewed
0
Ahmed Ahmed Sayed 2020-12-11 04:04:26

Found an solution by using

.raw()

with joi

refrences : https://joi.dev/api/?v=17.3.0#anyrawenabled