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

Add an option to allow undefined for optional properties #1191

Open
JesusTheHun opened this issue Oct 30, 2024 · 8 comments
Open

Add an option to allow undefined for optional properties #1191

JesusTheHun opened this issue Oct 30, 2024 · 8 comments
Labels
confirmed The maintainers of the repo would like to address this

Comments

@JesusTheHun
Copy link

Request a feature

Add a global option to allow undefined as a value for optional properties.

configure({ optionalAllowsUndefined: true });

🤷 Motivation

const arkUser = type({ phoneNumber: 'string?' });

The type above produces the type { phoneNumber?: string } and will not validate { phoneNumber: undefined } at runtime.

💡 Solution

Another solution could be a new-ish syntax like string??.

@github-project-automation github-project-automation bot moved this to To do in arktypeio Oct 30, 2024
@ssalbdivad ssalbdivad added the confirmed The maintainers of the repo would like to address this label Oct 30, 2024
@Asfamilybank
Copy link

Are there any updates?🧐

@ssalbdivad
Copy link
Member

I would accept external contributions on this. Otherwise it would likely be sometime after stable 2.0.

@LukeAbby
Copy link

Out of merely interest is there actually any way to get exactly this proposed API to work?

configure({ optionalAllowsUndefined: true });

Traditionally I've had to have something like this:

configure({ optionalAllowsUndefined: true });

declare module "arktype/configuration" {
    interface Config {
        optionalAllowsUndefined: true
    }
}

@ssalbdivad
Copy link
Member

ssalbdivad commented Dec 17, 2024

ArkType uses a global called ArkEnv for type-level configuration, e.g.:

image

This is something I'm working on documenting more now.

However, this wouldn't be a type-level config. You would just change this setting to match the exactOptionalPropertyTypes option in your tsconfig. Only the runtime behavior would need to be configured from within ArkType

@trsaunders
Copy link

I would like to have this feature in Arktype. I am dealing with data that often has null values as the python schemas have no means of precluding it (though they do make it possible to drop such values during JSON encoding, it's just not guaranteed to be used). My solution up until now is to define fields like "field?": "number | null", but it would be much more desirable for typescript consumers to be able to use definitions "field?": "number" and automatically drop null | undefined values.

It seems the proposal so far has focused on just undefined rather than null. I see the reasoning here, but I wonder how useful such a limitation would prove in practice, when AFAIK it is not possible to represent undefined in JSON?

@ssalbdivad
Copy link
Member

@trsaunders Since this would parallel the exactOptionalPropertyTypes TS setting, I'm not sure it would include an option for null.

That may be addressed separately though along with some other cases like empty strings that potentially should be treated as not present in contexts like forms.

@trsaunders
Copy link

That may be addressed separately though along with some other cases like empty strings that potentially should be treated as not present in contexts like forms.

That could work. For my use-case I don't mind specifically annotating a field to say that null implies not present, such that the fields get ignored during validation, and the type appears as optional. Is that the sort of thing you had in mind?

@ssalbdivad
Copy link
Member

@trsaunders I'm not sure exactly what the API would be yet. It would likely be configurable both globally and for an individual field.

Actually if you wouldn't mind creating an issue for that I'd appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed The maintainers of the repo would like to address this
Projects
Status: Backlog
Development

No branches or pull requests

5 participants