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

Non-Boolean Additional Properties #264

Merged
merged 4 commits into from
Oct 23, 2022
Merged

Conversation

sinclairzx81
Copy link
Owner

@sinclairzx81 sinclairzx81 commented Oct 23, 2022

This PR implements support for additionalProperties: TSchema on Value and TypeCompiler API's to allow for additional properties of a known type. This functionality aligns TypeBox validators with non-boolean additional properties constraints which are outlined at http://json-schema.org/understanding-json-schema/reference/object.html#additional-properties

You can use non-boolean schemas to put more complex constraints on the additional properties of an instance. For example, one can allow additional properties, but only if their values are each a string:

This PR also includes a new generic argument of TAdditionalProperties on TObject. This generic argument is used to infer a intersection of the normal TObject inference type with a Record<string, TAdditionalProperties> type. Intersection is only mapped when passing a non-boolean additionalProperties value. The TAdditionalProperties argument itself is static and reflectable.

Testing has been completed on TS 4.1.5 and should be fine; however the inference for TAdditionalProperties can be considered provisional as the output type produced results in a non-assignable type whose usage is somewhat limited. Future updates may default the inference to unknown.

const T = Type.Object({
  x: Type.Number(),
  y: Type.Number(),
}, {
  additionalProperties: Type.String()
})

function test(value: Static<typeof T>) {
  value.x = 1
  value.y = 1
  value.z = 'hello world' // ok
}

@sinclairzx81 sinclairzx81 changed the title Additional Property Generics Non-Boolean Additional Properties Oct 23, 2022
@sinclairzx81 sinclairzx81 merged commit 4bcacd4 into master Oct 23, 2022
@sinclairzx81 sinclairzx81 deleted the additional-property-generics branch October 23, 2022 08:26
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

Successfully merging this pull request may close these issues.

None yet

1 participant