Skip to content

Allow null to be treated as not-present for optional fields #1283

Open

Description

@trsaunders

Request a feature

(Related to #1191)

I wish to be able to have a type with an optional non-null field that accepts null as being equivalent to not present:

import { scope } from "arktype";

export const mod = scope({
    Model: {
        "field?": "number.integer",
        other: "string",
    },
});

export const types = mod.export();
export const Model = types.Model;

// should not give an error
console.log(Model(JSON.parse('{"other": "hi", "field": null}')).summary);

🤷 Motivation

I use arktype models to validate data produced by pydantic models. Optional fields in pydantic are, by default, emitted as "field": null when JSON encoded. I wish to have typescript types and validations that accept "field": null as if it were absent from the JSON. It is undesirable to have the field defined as number.intger | null as it adds extra complexity to consumption code.

For this use-case undefined is not an option as it cannot be JSON serialised.

💡 Solution

I am not sure what the best interface would look like, but perhaps a field specified as e.g "field?": "number.integer?" could behave in this way? or a global config, or a mode that applies specifically to JSON parsing?

Activity

  1. added
    confirmed
    on Jan 29, 2025
  2. changed the title [-]Allow null to be treated as not-present[/-] [+]Allow null to be treated as not-present for optional fields[/+] on Jan 29, 2025
  3. ssalbdivad commented on Jan 29, 2025

    @ssalbdivad
    Member

    Not sure exactly what the solution will look like yet either, but I want to build something general here that allows certain types to be treated as if they weren't present as opposed to just special-casing null.

    For example, devs working with forms may also want an empty string to be treated as not present.

  4. moved this from To do to Planned in arktypeioon Jan 30, 2025
  5. moved this from Planned to Backlog in arktypeioon Jan 30, 2025
  6. plehnen commented on Feb 12, 2025

    @plehnen

    +1

    I'd love to be able to write type.string.optional({ nullable: true }) or something like this, that normalizes null | undefined to undefined.

    status: type.string.or(type.null.pipe(() => undefined)).optional(), seems to be a bit long, also the resulting types doesn't look nice.

  7. trsaunders commented on Feb 12, 2025

    @trsaunders
    Author

    @plehnen thank you for the suggestion of using pipe as an intermediate solution. It was on my todo list to investigate this, so you've just saved me some effort 😄

    For now I shall use the following form, but I still see significant value in formal support.

        Model: {
            "field?": type("number.integer").or(type.null.pipe(() => undefined)),
            other: "string",
        },
  8. guersam commented on Jun 10, 2025

    @guersam

    I'd be happy if I could just configure the decoding more tolerant, rather than loosening the entire type constraint in the model.

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