Skip to content

Validation of enum values #23

@simonvadee

Description

@simonvadee
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions