medic / cht-core

The CHT Core Framework makes it faster to build responsive, offline-first digital health apps that equip health workers to provide better care in their communities. It is a central resource of the Community Health Toolkit.
https://communityhealthtoolkit.org
GNU Affero General Public License v3.0
438 stars 209 forks source link

Include xpath function for validation of Luhn identifiers #9227

Closed garethbowen closed 2 months ago

garethbowen commented 3 months ago

Is your feature request related to a problem? Please describe.

The Luhn algorithm provides for checkbits to detect typos on the client side, but right now there's no way to run this validation in enketo forms.

Describe the solution you'd like

Include a custom xpath function to validate the given number is valid.

ChinHairSaintClair commented 3 months ago

Thank you @garethbowen ! To expand a little on the above given our use case, we intend to use the Luhn-validated ID number to auto-fill some of the other fields in our form, making the process of registering a person easier for our CHWs. The larger impact is that downstream, it will enable us to link this person to our other healthcare systems, reduce duplicates, and, in so doing, provide better care.

garethbowen commented 3 months ago

@ChinHairSaintClair Does that mean you need to normalize the data, and not just validate it? I'm thinking about if there are spaces included, and that passes validation, but then it's auto-filled later on with spaces intact?

ChinHairSaintClair commented 2 months ago

@garethbowen you have a valid point. That seems to be the case, yes. If we only used the value downstream, it could be easily addressed. However it's still better to fix the issue at source. I've read up a little and played around with the translate xpath function, and it seems something like translate(., ' ', '') would strip spaces from a string, contrary to my original understanding about such a function being available. See the below screenshot: image This should suffice. However, do you think we need to rethink what the Luhn XPath method returns?

garethbowen commented 2 months ago

I think the validation can only return a boolean, right? If so then you could have two xpath functions - one to validate and one to transform (more or less what you have demoed). The other option would be to have an enketo widget which does both but that's much more complex to write and maintain than xpath functions.

ChinHairSaintClair commented 2 months ago

@garethbowen thank you for your suggestions. I've added a function that strips white spaces and pushed it to the open PR for your review.

garethbowen commented 2 months ago

Thank you! I'll review on Monday.

garethbowen commented 2 months ago

Merged. Thank you!