-
Notifications
You must be signed in to change notification settings - Fork 223
Fix input-object validation issue
#1215
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
base: master
Are you sure you want to change the base?
Conversation
* Adjust validation for nested and partial input objects. * Refactor metadata handling in `InputValidator`. * Add test for `partialInputObjectsCollectionValidation`. * Remove test `testOnlyPassedFieldsValidated`. * Update validation constraints in YAML configuration files.
* Add missing trailing commas to array elements.
eecaaee to
8b32024
Compare
input-object validation issue
| private function getMetadata(ValidationNode $rootObject): ObjectMetadata | ||
| { | ||
| // Return existing metadata if present | ||
| if ($this->metadataFactory->hasMetadataFor($rootObject)) { | ||
| return $this->metadataFactory->getMetadataFor($rootObject); | ||
| } | ||
|
|
||
| // Create new metadata and add it to the factory | ||
| $metadata = new ObjectMetadata($rootObject); | ||
| $this->metadataFactory->addMetadata($metadata); | ||
|
|
||
| return $metadata; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method reuses metadata if it was already created for the input-object of the same type
| $metadata->addPropertyConstraint($property, $valid); | ||
|
|
||
| // Skip the rest as the validation was delegated to the nested object. | ||
| continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to proceed and add any more constraints if it was set to cascade
| if ($metadata->hasPropertyMetadata($property)) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If constraints were set for the property, don't add them the second time
| $query = ' | ||
| mutation { | ||
| partialInputObjectsCollectionValidation( | ||
| addresses: [ | ||
| { | ||
| street: "Washington Street" | ||
| city: "Berlin" | ||
| zipCode: 10000 | ||
| # Country is present, but the language is invalid | ||
| country: { | ||
| name: "Germany" | ||
| officialLanguage: "ru" | ||
| } | ||
| # Period is completely missing, skip validation | ||
| }, | ||
| { | ||
| street: "Washington Street" | ||
| city: "New York" | ||
| zipCode: 10000 | ||
| # Country is partially present | ||
| country: { | ||
| name: "" # Name should not be blank | ||
| # language is missing | ||
| } | ||
| period: { | ||
| startDate: "2000-01-01" | ||
| endDate: "1990-01-01" | ||
| } | ||
| }, | ||
| { | ||
| street: "Washington Street" | ||
| city: "New York" | ||
| zipCode: 10000 | ||
| country: {} # Empty input object, skip validation | ||
| period: {} # Empty input object, skip validation | ||
| } | ||
| ] | ||
| ) | ||
| } | ||
| '; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A new test case to cover scenarios when input objects (country and period) have all fields, partial fields, or no fields at all.
As mentioned in the ticket, there was an issue with the validation metadata for collections of input objects: only the metadata of the last item in the collection was used to validate all items in the collection.
This change introduces metadata caching, so that the same object is used for all elements in the collection.