Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with date validation with format() #39

Open
jd-m3 opened this issue Apr 21, 2021 · 2 comments
Open

Issue with date validation with format() #39

jd-m3 opened this issue Apr 21, 2021 · 2 comments

Comments

@jd-m3
Copy link

jd-m3 commented Apr 21, 2021

Support plan

  • is this issue currently blocking your project? (yes/no): Yes
  • is this issue affecting a production system? (yes/no): No

Context

  • node version: v14.16.1
  • module version with issue: 2.1.0
  • last module version without issue: N/A
  • environment (e.g. node, browser, native): Browser
  • used with (e.g. hapi application, another framework, standalone, ...): Next.js / TypeScript / joi
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

I was trying to validate a date on a particular form that I'm working on, but the problem is there's an edge case where it says 1 is a valid date. I also tried some numbers, the biggest that I got validated is 275760 until it got a date.base error. Take note that I used format() just to handle this edge case but it doesn't work as expected.

import Joi from "joi";
import JoiDate from "@joi/date";

const JoiExtended = Joi.extend(JoiDate);

export const FieldValidationSchema = Joi.Object().keys({
  fieldDate: JoiExtended.date().raw().format("MM/DD/YYYY")
})

// Usage:

const validate = FieldValidationSchema.validate({fieldDate: 1})

What was the result you got? It says date with value 1 is valid

What result did you expect? It should be invalid, following the rules of format()

@chrisvoo
Copy link

chrisvoo commented May 7, 2021

Maybe the problem refers to this line: you're passing an integer and maybe that $_getFlag function just returns false/null, no idea. Since the following condition is met (if (!format) return; }), the validation is never performed.
Moment is actually able to mark it as an invalid date also if you pass a number:

const mom = require('moment');
mom(1, 'MM/DD/YYYY', true).isValid()
// false

Just pass '1' instead of the number, something like yourFieldDate + '' as a workaround, for dynamic fields

Cruikshanks added a commit to DEFRA/sroc-charging-module-api that referenced this issue Jun 30, 2021
When we made the change to ES6 import we admittedly made a best guess as to the way to extend Joi with the joi-date extension. There is simply nothing in the Joi docs on how to do it using the ES6 syntax.

Thankfully we tracked down this issue hapijs/joi-date#39 (comment) which included how they had used `@joi/date` with Joi to extend it.

So, this updates our code to match and resolve the error we were getting (essentially Joi was not recognising our date formats).
Cruikshanks added a commit to DEFRA/sroc-charging-module-api that referenced this issue Jul 1, 2021
When we made the change to ES6 import we admittedly made a best guess as to the way to extend Joi with the joi-date extension. There is simply nothing in the Joi docs on how to do it using the ES6 syntax.

Thankfully we tracked down this issue hapijs/joi-date#39 (comment) which included how they had used `@joi/date` with Joi to extend it.

So, this updates our code to match and resolve the error we were getting (essentially Joi was not recognising our date formats).
@Abhijith327
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants