arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Add an option to allow `undefined` for optional properties

Open JesusTheHun opened this issue 1 year ago • 8 comments

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

JesusTheHun avatar Oct 30 '24 17:10 JesusTheHun

Are there any updates?🧐

asfamilybank avatar Dec 17 '24 01:12 asfamilybank

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

ssalbdivad avatar Dec 17 '24 16:12 ssalbdivad

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
    }
}

LukeAbby avatar Dec 17 '24 17:12 LukeAbby

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

ssalbdivad avatar Dec 17 '24 17:12 ssalbdivad

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?

trsaunders avatar Jan 27 '25 12:01 trsaunders

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

ssalbdivad avatar Jan 27 '25 12:01 ssalbdivad

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?

trsaunders avatar Jan 27 '25 12:01 trsaunders

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

ssalbdivad avatar Jan 28 '25 19:01 ssalbdivad

Is this not the same as https://arktype.io/docs/configuration#exactoptionalpropertytypes?

RobertVillalba avatar Nov 17 '25 20:11 RobertVillalba