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

fix: allow boolean for some validators in validation recipe #4528

Conversation

aguscha333
Copy link
Contributor

This PR fixes #4527

The Typing for ValidationRecipewas not consistent for some validators with the docs and what the actual VALIDATORS that validate uses accept for options.

I am refering to absence, acceptance, email, numericality, and presence accepting boolean for options but the ValidationRecipe 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:
image

With my change, the error goes away:
image

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:
image

@simoncrypta
Copy link
Collaborator

Hey @aguscha333 Thanks for this PR, I will take a look on it soon!
Meanwhile @dac09 or @cannikin can be interested to review it.

@simoncrypta simoncrypta self-assigned this Feb 19, 2022
@aguscha333
Copy link
Contributor Author

I see that there is a tests failing:
image

Not very familiar with the code yet and I am not sure how my changes could have affected that. Would apreciate some help debugging.

@simoncrypta simoncrypta added the release:fix This PR is a fix label Feb 20, 2022
@simoncrypta
Copy link
Collaborator

Not very familiar with the code yet and I am not sure how my changes could have affected that. Would apreciate some help debugging.

Don't worry, It's not cause by your changes.

@jtoar I think your last PR didn't work out for this #4526

@jtoar
Copy link
Contributor

jtoar commented Feb 21, 2022

@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.

@cannikin
Copy link
Member

cannikin commented Feb 21, 2022

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?

Copy link
Collaborator

@dac09 dac09 left a 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

@simoncrypta simoncrypta assigned dac09 and unassigned simoncrypta Feb 21, 2022
@aguscha333
Copy link
Contributor Author

aguscha333 commented Feb 22, 2022

@dac09 thanks for taking a look.
Not sure I understand what solution you are thinking of. Looking at the line you mentioned, the def of WithOptionalMessage:

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 unknown but that it can't be assigned to Record<String, unknown> as a whole. This is because instead of passing an object with options for some cases we just send a boolean since we already have all the default options for that particuar validator when the message is optional.

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 WithOptionalMessage are allowed to be passed just a boolean as value instead of the proper record. Only absence, acceptance, email, numericality, and presence accept a boolean as options. Something like length would require to be passed some options like min or max even if the message is optional.

At least that's the way I understand it, would love to hear your thoughts since I am not an expert in TypeScript.

Copy link
Collaborator

@dac09 dac09 left a 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

@dac09 dac09 enabled auto-merge (squash) February 22, 2022 03:59
@dac09 dac09 merged commit bf1451a into redwoodjs:main Feb 22, 2022
@jtoar jtoar added this to the next-release milestone Feb 22, 2022
@thedavidprice thedavidprice modified the milestones: next-release, v0.47.0 Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Archived
Development

Successfully merging this pull request may close these issues.

Error in ValidationRecipe type for some of the validators that can receive boolean as options
6 participants