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

Presence of default value for an object property should not imply required. #211

Open
nhollander-alert opened this issue Oct 3, 2024 · 3 comments

Comments

@nhollander-alert
Copy link

According to the json schema specification, the default keyword is a meta-data annotation for "documentation and user interface display purposes" 1. The schema reference states that "...default is typically used to express that if a value is missing, then the value is semantically the same as if the value was present with the default value." 2

Based on this, I would expect that the default value would be entirely ignored by this library, as it is a purely semantic marker which tells users that an undefined value will be treated by the tool as if it were the given value, but there is no requirement that processing tools substitute undefined for the default value.

Given the following schema definition this library correctly produces an object where foo is required but bar is optional.

type test = FromSchema<{
    type: "object",
    additionalProperties: false,
    required: ["foo"],
    properties: {
        foo: { type: "string" },
        bar: { type: "string" }
    }
}>;
type test = {
    bar?: string | undefined;
    foo: string;
}

If we alter bar to include a default value, the resulting type no longer accepts undefined as a valid value, despite it still being an allowed value per the spec.

type test = FromSchema<{
    type: "object",
    additionalProperties: false,
    required: ["foo"],
    properties: {
        foo: { type: "string" },
        bar: { type: "string", default: "abcd" }
    }
}>;
type test = {
    foo: string;
    bar: string;
}

Footnotes

  1. https://json-schema.org/draft/2020-12/json-schema-validation#section-9.2

  2. https://json-schema.org/understanding-json-schema/reference/annotations

@Caesar2011
Copy link

It depends on the perspective. If you have an API request following this schema and you then use the data, it will be present. If you are on the other side and you need to fulfil a given API spec (JSON schema), then it sould be optional.

@nhollander-alert
Copy link
Author

While APIs are permitted to apply default values to incoming or outgoing requests, it is not mandated by the specification and APIs are free to send or omit default values. Unless you pass your incoming or outgoing data through some kind of function to apply default values, it is not safe to assume that a value will ever be defined just because it has a default field.

@ThomasAribart
Copy link
Owner

ThomasAribart commented Oct 10, 2024

@nhollander-alert Good point. There's already a keepDefaultedPropertiesOptional option to revert this, but the default is false.

I could reverse it and rename it requireDefaultedProperties (with false by default). That would be a breaking change but there's not been any breaking change for a while so I'm actually fine with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants