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

Have validate accept null and undefined #606

Open
grosch opened this issue Dec 7, 2021 · 12 comments
Open

Have validate accept null and undefined #606

grosch opened this issue Dec 7, 2021 · 12 comments
Labels
stale-exempt Exempts issue from being marked as stale

Comments

@grosch
Copy link

grosch commented Dec 7, 2021

Just to make life easier, it would be great if when I call uuidValidate and pass in null or undefined, it returned false. Right now the compiler doesn't allow a potentially null/undefined value to be given.

Right now I'm doing something like this:

this.route.paramMap
    .pipe(
        map(x => x.get('id')),
        filter(x => !!x && uuidValidate(x)),
        mustHaveValue(),
        switchMap(x => this.clientService.get(x)),
        untilDestroyed(this)
    )
@broofa
Copy link
Member

broofa commented Dec 7, 2021

Thanks for the report. This is actually a @types/uuid issue. The validate() implementation actually works just fine with any value.

(I'm actually surprised nobody has complained about this yet.)

> require('uuid').validate(null)
false
> require('uuid').validate(undefined)
false

@grosch: We'll fix this up on our end, but this taps into a bigger debate we've been having about whether we should provide TS types natively as pat of this project. In the meantime, I suggest just casting the argument to a string, ala filter(x => uuidValidate(x as string)).

@ctavan Looks like the validate() types were introduced here. I'd put up a PR but I'm wondering if it wouldn't be simpler to pull type definitions into this project.

@ctavan
Copy link
Member

ctavan commented Jan 4, 2022

I think it could make sense to fix that on @types/uuid as it's a clear bug and we don't have a timeline for inlining the type definitions into this library.

@ctavan ctavan closed this as completed Jan 4, 2022
@ctavan ctavan reopened this Jan 4, 2022
@ctavan
Copy link
Member

ctavan commented Jan 4, 2022

@grosch feel free to propose a pull request with a fix to https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/uuid/index.d.ts

I'll be more than happy to help get this merged quickly over there.

@LinusU
Copy link
Member

LinusU commented Jan 4, 2022

If you are sending a PR, I would recommend to change it to from (uuid: string) => boolean to (uuid: any) => uuid is string. That way TypeScript will know that you can pass anything in to the validate function, and that if true was returned it was definitely a string.

@broofa
Copy link
Member

broofa commented Jan 4, 2022

@LinusU Can you provide a link to the typescript docs that discuss whatever that behavior is that you're describing? I'm still learning TS and this sort of conditional typing sounds interesting.

@grosch
Copy link
Author

grosch commented Jan 4, 2022

@LinusU Can you provide a link to the typescript

https://www.typescripttutorial.net/typescript-tutorial/typescript-type-guards/

@LinusU
Copy link
Member

LinusU commented Jan 5, 2022

some more refs:

https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
https://stackoverflow.com/q/40081332/148072

@github-actions
Copy link

Marking as stale due to 90 days with no activity.

@github-actions
Copy link

Closing issue due to 30 days since being marked as stale.

@ctavan
Copy link
Member

ctavan commented Sep 12, 2022

Reopening as this may be solved in #654.

@ctavan ctavan reopened this Sep 12, 2022
@github-actions github-actions bot removed the stale label Sep 13, 2022
@github-actions
Copy link

Marking as stale due to 90 days with no activity.

@github-actions github-actions bot added the stale label Dec 13, 2022
@github-actions
Copy link

Closing issue due to 30 days since being marked as stale.

@broofa broofa added stale-exempt Exempts issue from being marked as stale and removed stale labels Jan 13, 2023
@broofa broofa reopened this Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale-exempt Exempts issue from being marked as stale
Projects
None yet
Development

No branches or pull requests

4 participants