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

EES-5112 - adding validation to ensure Candidate Keys are valid #5034

Merged

Conversation

duncan-at-hiveit
Copy link
Collaborator

This PR:

  • adds in validation for the selected candidate keys for update requests, for both Locations and Filter Options.

Location candidate validation

This validation is pretty simple. We only need to take the "Level" supplied in the update request, and see if there is a candidate Location within that same Level with the given CandidateKey.

Filter option validation

This validation is a little more involved. In order to validate, we:

  1. Take the "FilterKey" from the update request, which identifies the owning filter of the filter option being updated.
  2. Find the filter that that owning filter is mapped to.
  3. If the owning filter has not yet been mapped, return an error.
  4. If the owning filter has been mapped, look for the candidate filter option underneath the filter that the owning filter has been mapped to.

New "KeyExistsAtJsonbPath" method

This is an optimised method that in this PR we are using to check for the existence of a key within a given chunk of JSON.

@duncan-at-hiveit duncan-at-hiveit force-pushed the EES-5112-and-5115-add-validation-for-mapping-candidates branch 3 times, most recently from 59de0c9 to e2cf678 Compare July 8, 2024 11:14
Base automatically changed from EES-5115-autosave-endpoint-for-filter-options to EES-5111-endpoint-to-retrieve-location-mappings July 8, 2024 11:21
Base automatically changed from EES-5111-endpoint-to-retrieve-location-mappings to EES-5112-endpoint-for-autosaving-location-mappings July 8, 2024 11:21
Base automatically changed from EES-5112-endpoint-for-autosaving-location-mappings to dev July 8, 2024 11:24
@duncan-at-hiveit duncan-at-hiveit force-pushed the EES-5112-and-5115-add-validation-for-mapping-candidates branch from e2cf678 to d24739a Compare July 8, 2024 11:30
… a boolean for testing the existence of a CandidateKey rather than having to deserialise the Candidate just to test for its existence.
@duncan-at-hiveit duncan-at-hiveit force-pushed the EES-5112-and-5115-add-validation-for-mapping-candidates branch from d24739a to 5097b35 Compare July 8, 2024 11:32
.Set<JsonBool>()
.FromSqlRaw(
sql: $"""
SELECT COALESCE("{request.JsonbColumnName}" #> @jsonPath, '[]'::jsonb)
Copy link
Collaborator

Choose a reason for hiding this comment

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

is the , '[]'::jsonb part needed?

Copy link
Collaborator Author

@duncan-at-hiveit duncan-at-hiveit Jul 9, 2024

Choose a reason for hiding this comment

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

Yup, this means that if {request.JsonbColumnName}" #> @jsonPath resolves to NULL, then this will fall back to a non-null JSON object for the key to be compared against by using "[]" instead of null.

Without this fallback, it goes kaboom!

Copy link
Collaborator

Choose a reason for hiding this comment

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

but doesn't COALESCE return the first non-null value in the parameter list? And if none are non-null, then it returns null itself? So I thought it would have the same behaviour?

Copy link
Collaborator Author

@duncan-at-hiveit duncan-at-hiveit Jul 9, 2024

Choose a reason for hiding this comment

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

Yup, but given that "[]" is never null, the whole COALESCE will be guaranteed to return something, which is what's important here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh, we're saying it's the ? @keyValue part which will blow up, unless we have that []? That makes sense.

Sorry, I thought we were trying to prevent the COALESCE blowing up

{
"Target filter 1 option 2 key", new MappableFilterOption("Filter 1 option 2 label")
},
{ "Target filter 1 option 3 key", new MappableFilterOption("Filter 1 option 3 label") }
Copy link
Collaborator

Choose a reason for hiding this comment

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

might be nice to format this the same as the ones above :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've given this file a blanket reformat, which cleans this one up too!

Comment on lines +809 to +812
Level = GeographicLevel.Country,
SourceKey = "source-la-location-3-key",
Type = MappingType.ManualMapped,
CandidateKey = "target-la-location-1-key"
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 not super fussed, but at first I didn't realise that the reason this candidate is invalid is because it is not a candidate for the Country level, even though it is a candidate for the LocalAuthority level.

This is a different failure scenario to the candidate key target-la-location-2-key above, which doesn't exist at all.

I just wondered if we should break these out into 2 separate tests to make it clearer - and because they could potentially go down different code paths? But I don't feel strongly either way

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've gone for a middle ground if that's OK, and added comments to explain the different scenarios being tested. Done the same for the filter option equivalent too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice! Yep, I prefer this compromise actually :)

@duncan-at-hiveit duncan-at-hiveit merged commit 61a3f6f into dev Jul 9, 2024
2 checks passed
@duncan-at-hiveit duncan-at-hiveit deleted the EES-5112-and-5115-add-validation-for-mapping-candidates branch July 9, 2024 15:58
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.

2 participants