-
Notifications
You must be signed in to change notification settings - Fork 970
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
fix: allow boolean for some validators in validation recipe #4528
fix: allow boolean for some validators in validation recipe #4528
Conversation
Hey @aguscha333 Thanks for this PR, I will take a look on it soon! |
…ons-in-correct-validators
@simoncrypta thanks! I'll keep an eye on it. We're also dealing with a OOM (or something along those lines) error in the Lint, Build & Test workflow that's suddenly sprung up on the Windows runner. |
…ons-in-correct-validators
…ons-in-correct-validators
This is Typescript so I'd put very little confidence in my review of this PR, but: looks good to me? 😬 Maybe the TS engine or linter or whatever does it isn't checking for type errors in tests? We've got plenty that use that boolean form: expect(() => validate(val, 'email', { email: true })).toThrow(
ValidationErrors.EmailValidationError
) And there aren't any errors...maybe type problems are only warnings and don't stop the test suite? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @aguscha333 thanks so much for this PR!
From a cursory glance, it doesn't quite look like the correct typing to me.
I think the fix would be in this line type WithOptionalMessage<T = Record<string, unknown>> = T & {
here, where TS is actually complaining that boolean
can't be assigned to unknown
You're totally right that this is a problem though. @cannikin let's catch up tomorrow to see if I can suggest a different fix? I get the TS, but I don't get validators, so our powers combined could be formidable ;)
Just going to hold this PR for now, I will update with recommendations soon
@dac09 thanks for taking a look. type WithOptionalMessage<T = Record<string, unknown>> = T & {
/**
* A message to be shown if the validation fails.
*/
message?: string
} If I understand correctly, it is not that boolean can't be assiged to Doing something like: type WithOptionalMessage<T = Record<string, unknown>> = boolean | (T & {
/**
* A message to be shown if the validation fails.
*/
message?: string
}) Doesn't feel right either because not all validators that extend At least that's the way I understand it, would love to hear your thoughts since I am not an expert in TypeScript. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looked a bit further into this!
Thanks for your patience @aguscha333 - and you're correct this is the expected typing.
would love to hear your thoughts since I am not an expert in TypeScript
👍 Your TS is great, apparently my reading skills needed some improvement 😉 - sometimes hard to follow with the folded code in PRs
…ons-in-correct-validators
…ons-in-correct-validators
…ons-in-correct-validators
This PR fixes #4527
The Typing for
ValidationRecipe
was not consistent for some validators with the docs and what the actualVALIDATORS
thatvalidate
uses accept for options.I am refering to
absence
,acceptance
,email
,numericality
, andpresence
acceptingboolean
for options but theValidationRecipe
only accepting the object version of options for each.As you can see in the reported issue, this results in a type error when using boolean as options for a validator when using
validate
(the overload with optional messages) Here is the image of said error:With my change, the error goes away:
The overload that requires messages shouldn't be affected by this change since it uses
ValidationWithMessagesRecipe
that overrides all validator typings. So you shouldn't be able to not pass a message to that overload by simply passing a boolean instead.As you can see here for that case I receive the correct error from typescript: