Issue with date validation with format()
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()
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
thank you