FirelyTeam / firely-validator-api

Firely's official FHIR validator API for validating HL7 FHIR resources against profiles.
Other
7 stars 2 forks source link

Unable to validate resources in R4/R4B where Attachement.size is set because CQL types differ #326

Closed almostchristian closed 1 week ago

almostchristian commented 2 months ago

When validating an Attachment element with the size field in R4B (size is mapped to unsignedInt in R4/R4B and integer64 in R5), The MinMaxValueValidator fails with an ArgumentException with message:

'Cannot compare 234 (of type Hl7.Fhir.ElementModel.Types.Long) to -2147483648 (of type Hl7.Fhir.ElementModel.Types.Integer), because the types differ.'

Reproduction code:

var r = new DiagnosticReport
{
    Code = new CodeableConcept("http://loinc.org", "abc"),
    Status = DiagnosticReport.DiagnosticReportStatus.Partial,
    PresentedForm = [new Attachment { Size = 234 }],
};

var result = validator.Validate(r);

I believe the fix should be in Hl7.Fhir.ElementModel.Types.Long to allow comparisons to Integer which should to be in line with the csharp comparison semantics where int values are automatically converted to long.

public int CompareTo(object? obj)
{
    return obj switch
    {
        null => 1,
        Long i => Value.CompareTo(i.Value),
+       Integer i => Value.CompareTo(i.Value),
        _ => throw NotSameTypeComparison(this, obj)
    };
}
mmsmits commented 1 month ago

This needs to be solved in the ElementSchemaCompilar of the MinMaxValidator, not in the Long comparison itself.

almostchristian commented 1 month ago

Yeah, changing it in the MinMaxValidator would be the minimal change with the least impact to existing code,

How about using EqualityOperators. Compare? I notice that this method has the tryCoerce function which will convert the int to long.