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: allow nullable input objects and arrays #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

peterc1731
Copy link

@peterc1731 peterc1731 commented Mar 2, 2023

Fixes #57

When inferring types from the schema, the library does not consider nullability for objects or arrays. The test that validates nullable input types ("should support nullable input type arguments") was only validating string type inputs, when adding validation for a few more input types as I've done in this PR, that test fails.

I've applied a fix for the issue as well as updating the tests, I'm not sure if the approach is the best one, but it works and its a simple change with low impact on the rest of the behaviour.

@jonnydgreen
Copy link
Collaborator

jonnydgreen commented Mar 2, 2023

Thanks for submitting - I'll take a look tonight :)

@jonnydgreen jonnydgreen added the bug Something isn't working label Mar 2, 2023
Copy link
Collaborator

@jonnydgreen jonnydgreen left a comment

Choose a reason for hiding this comment

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

Thanks - just one comment about the object side of things!

@@ -106,6 +111,7 @@ class JSONSchemaValidator extends Validator {
let typeValidationSchema = {
$id: `https://mercurius.dev/validation/${typeName}`,
type: 'object',
nullable: true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering if it's better to define nullability on the usage rather than the definition itself so we have a validation schema that fully aligns with the GraphQL schema, WDYT?

Copy link
Author

@peterc1731 peterc1731 Mar 3, 2023

Choose a reason for hiding this comment

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

Without this we are technically already still defining nullability on the definition, just implicitly setting it to false, so even if the validation schema contains nullable: true at the point of usage it will fail validation when we send null because the definition itself is non-nullable.

I'm not sure it would be possible to allow objects to be null without this part, unless you have any other ideas how we could do it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we could maybe do it with an anyOf in this function: https://github.com/mercurius-js/validation/pull/67/files#diff-ee1a5eaed8c16e887653f519f05ddf10825fb4acfe4320521cda3e701825046dR25

That way we only set the nullability when it's applicable - wdyt?

Copy link
Author

Choose a reason for hiding this comment

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

Ooh that is a good idea, I'll try it

Copy link
Author

Choose a reason for hiding this comment

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

Sorry for the long wait getting back to you on this!

I've done some investigation and anyOf/oneOf in the spot you described does work to allow input objects to be null without needing this line, but it introduces a a few different issues that I think make this approach unviable.

Firstly, if the object that's being validated has failed on any of the nested fields, it will fail the anyOf check, as it doesn't exactly match the defined object or null, so we get back a lot of extra noise in the error response describing the real issue as nested underneath the failed anyOf check, which I think makes the error harder to understand.

Secondly, if there is a nullable object nested inside the input object, it seems to not be running validation on the fields inside that nested object. I wonder if a conditional schema check has some kind of depth limit? Or possibly I'm missing something with the implementation, but I've tried experimenting with a few similar approaches (like if/else/then) and haven't got this case to work correctly.

I'm all out of ideas for trying to avoid this line, but if you have any more suggestions please let me know and I'm happy to try them out!

Choose a reason for hiding this comment

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

I comprehend the logic behind the removal of L114, but I'm not entirely clear on the significance of opting for oneOf over nullable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Apologies for my late reply on this! If the approach isn't viable, I think it's okay to go with your original approach as we are protected by the underlying GraphQL validation :) do you agree with this @mcollina ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Got some time this weekend, so gonna have a closer look at this one then :)

Copy link
Author

Choose a reason for hiding this comment

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

Would love to eventually get this PR merged if you have any time to follow up @jonnydgreen or @mcollina !

@bglgwyng
Copy link

@peterc1731 Thanks for addressing this fix!
I've seen the significance of this issue, and I've found that this solution works accurately. Is there any reason to hold back from merging this pull request?

@mcollina
Copy link
Contributor

@peterc1731 are you still interested in addressing @jonnydgreen comment?
@bglgwyng maybe would you like to take over?

@peterc1731
Copy link
Author

@peterc1731 are you still interested in addressing @jonnydgreen comment?

Apologies, completely forgot to get back to this one! I will take a look today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array or null validation error
5 participants