Skip to content

Commit

Permalink
Use a better validation regex for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
marcovisserFurore committed Feb 24, 2021
1 parent db9253f commit 7420fb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,13 @@ private static void assertElementConstraints(List<ElementDefinition> patientElem
}
}


[Fact]
public void ValidateNonBreakingWhitespaceInString()
{
var value = new FhirString("Non-breaking" + '\u00A0' + "space");
var result = _validator.Validate(value);
Assert.True(result.Success);
}

private class ClearSnapshotResolver : IResourceResolver
{
Expand Down
6 changes: 6 additions & 0 deletions src/Hl7.Fhir.Specification/Validation/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ private OperationOutcome validateRegexExtension(IExtendable elementDef, ITypedEl
var pattern = elementDef.GetStringExtension(uri);
if (pattern != null)
{
// See issue https://github.com/FirelyTeam/firely-net-sdk/issues/1563 and https://hl7.org/fhir/datatypes.html#string
// the regex provided by the Fhir standard is not sufficient enough. The regex [\r\n\t\u0020-\uFFFF]* is more recommended
if (instance?.InstanceType == FHIRAllTypes.String.GetLiteral() && pattern == @"[ \r\n\t\S]+")
{
pattern = @"[\r\n\t\u0020-\uFFFF]*";
}
var regex = new Regex(pattern);
var value = toStringRepresentation(instance);
var success = Regex.Match(value, "^" + regex + "$").Success;
Expand Down

0 comments on commit 7420fb8

Please sign in to comment.