Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Validation of enum values #23

Closed
simonvadee opened this issue Sep 3, 2020 · 2 comments
Closed

Validation of enum values #23

simonvadee opened this issue Sep 3, 2020 · 2 comments

Comments

@simonvadee
Copy link
Contributor

  • fhir.resources version: 6.0.0b4.dev0
  • Python version: 3.8
  • Operating System: macOS

Description

I want FHIRAbstractModel to be able to validate enum values. I think it's already supported by the current version but I couldn't find how to do it.

class Patient(domainresource.DomainResource):
    ...
    gender: fhirtypes.Code = Field(
        None,
        alias="gender",
        title="male | female | other | unknown",
        description=(
            "Administrative Gender - the gender that the patient is considered to "
            "have for administration and record keeping purposes."
        ),
        # if property is element of this resource.
        element_property=True,
        # note: Enum values can be used in validation,
        # but use in your own responsibilities, read official FHIR documentation.
        enum_values=["male", "female", "other", "unknown"],
    )

I don't quite understand what this means:
note: Enum values can be used in validation, but use in your own responsibilities, read official FHIR documentation.
because the pydantic model does not enforce the value of this field.

What I Did

from fhir.resources import construct_fhir_element
resource = {"resourceType": "Patient", "gender": "toto"}
construct_fhir_element(resource["resourceType"], resource)
# everything went fine, but it should raise a validation error
@nazrulworld
Copy link
Owner

@simonvadee thank you so much for raising this issue and sorry for any inconvenience.

note: Enum values can be used in validation, but use in your own responsibilities, read official FHIR documentation.
Means this validation is not part of built-in model validation, in other words not implemented (but possible to make custom validation depends on project needs (I will give example for this))

Why not ENUM validation included:

  1. first of all FHIR specification doesn’t enforce this kind of validation, on the other hand enums are generated by parsing string, so all enums are not trusted. You will find that some enum list contains value '+', which means more value could be acceptable.
  2. I think it is possible to give other value, rather than only from the enum list by adding a new extension.
  3. If we add this enum validation as core part that could be a disaster for any project.

But we have that flexibility kept to make custom validation, moreover, it will be possible extend enum values!

Example:

from typing import Dict
from fhir.resources.patient import Patient

def validate_gender(cls, values: Dict):
    if not values:
        return values
    enums = cls.__fields__["gender"].field_info.extra["enum_values"]
    if "gender" in values and values["gender"] not in enums:
        raise ValueError("write your message")
    return values

Patient.add_root_validator(validate_gender, pre=True)

Here you will find, about better way to add custom validator #17 (comment)

@simonvadee
Copy link
Contributor Author

Thank you for the example, now I understand why (and how) enum validation is not enforced by default!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants