Skip to content

Allow configuration of unevaluatedProperties in JSON Schema emitter, and possibly change the default behaviour of all models. #3549

Closed
@adamscybot

Description

@adamscybot

Clear and concise description of the problem

Currently, there exists no (non-workaround) decorator in order to configure unevaluatedProperties on a schema. If looking to restrict unknown properties, I would hazard unevaluatedProperties: false is usually the tool that you should reach for as opposed to additionalProperties: false -- especially where you have TypeSpec in play which induces dev expectations/assumptions about semantics of things like extends.

That's because additionalProperties: false will restrict properties to those defined on the specific schema, and so prevent the use of properties on related schemas (via allOf for example).

In that sense additionalProperties: false seals the definition and would not actually work with any properties coming from some other model via extends, which is weird semantics.

Incidentally, there is no way to set additionalProperties: false either unless you override it with @extension("additionalProperties", Json<false>). But perhaps that should be added as a sort of @sealed decorator.

We could add a @noUnknown decorator or something that controls unevaluatedProperties . At the moment you have to do @extension("unevaluatedProperties", Json<false>) which feels like a hack.

Perhaps it could be put into one like @strictProperties(preventExtension: boolean) where preventExtension would be false by default. if false, it would add unevaluatedProperties: false to the schema. If true, it would instead use additionalProperties: false, which would ideally a throw an error if used when the schema attempts to use index types (including Record), or if that schema is extended by another.

Note, not being able to define these properties using a first class API is unusual as the default behaviour of JSON Schema is to allow additional properties which is behavior the user has not defined in their schema specifically using Record or index types. Therefore there is probably a debate to be had about if unevaluatedProperties: false should actually be added to schemas by default.

Checklist

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions