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

fix(kernel): validate presence of required struct properties #591

Merged
merged 2 commits into from
Jul 12, 2019

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Jul 8, 2019

Detect when required struct properties are not supplied, so that
we can produce useful error messages to untyped jsii languages (in
particular, Python).

Also check for an array in place of a variadic argument, and throw
a hopefully helpful message to hint at a common misuse.

Fix the test to be actually variadic, which it wasn't :o.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Detect when required struct properties are not supplied, so that
we can produce useful error messages to untyped jsii languages (in
particular, Python).

Also check for an array in place of a variadic argument, and throw
a hopefully helpful message to hint at a common misuse.

Fix the test to be actually variadic, which it wasn't :o.
@rix0rrr rix0rrr requested a review from a team as a code owner July 8, 2019 13:58
const namedType = host.lookupType((optionalValue.type as spec.NamedTypeReference).fqn);
const props = propertiesOf(namedType, host.lookupType);

if (Array.isArray(value)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to verify that the type is the expected type instead of what it is not. What if it's a map for example or a primitive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A map is actually correct, that's what a struct is (or should be), and we do already check that a primitive is not passed.

We just missed the Array because of the fact that typeof array === 'object' in JavaScript.

By doing the explicit check we also get a nicer error message.

@rix0rrr rix0rrr merged commit 90135f9 into master Jul 12, 2019
@rix0rrr rix0rrr deleted the huijbers/struct-validation branch July 12, 2019 08:53
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.

3 participants