You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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.
@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.
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." 2Based 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 anundefined
value will be treated by the tool as if it were the given value, but there is no requirement that processing tools substituteundefined
for the default value.Given the following schema definition this library correctly produces an object where
foo
is required butbar
is optional.If we alter
bar
to include adefault
value, the resulting type no longer acceptsundefined
as a valid value, despite it still being an allowed value per the spec.Footnotes
https://json-schema.org/draft/2020-12/json-schema-validation#section-9.2 ↩
https://json-schema.org/understanding-json-schema/reference/annotations ↩
The text was updated successfully, but these errors were encountered: