Skip to content

Add an option to allow undefined for optional properties #1191

Open

Description

@JesusTheHun

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

Activity

  1. added
    confirmed
    on Oct 30, 2024
  2. asfamilybank commented on Dec 17, 2024

    @asfamilybank

    Are there any updates?🧐

  3. ssalbdivad commented on Dec 17, 2024

    @ssalbdivad
    Member

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

  4. LukeAbby commented on Dec 17, 2024

    @LukeAbby
    Contributor

    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
        }
    }
  5. ssalbdivad commented on Dec 17, 2024

    @ssalbdivad
    Member

    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

  6. trsaunders commented on Jan 27, 2025

    @trsaunders

    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?

  7. ssalbdivad commented on Jan 27, 2025

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

  8. trsaunders commented on Jan 27, 2025

    @trsaunders

    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?

  9. ssalbdivad commented on Jan 28, 2025

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed

    Type

    No type

    Projects

    • Status
      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions