Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Hl7.Fhir.Base/Model/ParametersExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public static class ParametersExtensions
{
private const string CODEATTRIBUTE = "code";
private const string URLATTRIBUTE = "url";
private const string SYSTEMATTRIBUTE = "system";
private const string CONTEXTATTRIBUTE = "context";
private const string VALUESETATTRIBUTE = "valueSet";

public static bool TryGetDuplicates(this Parameters parameters, out IEnumerable<string> duplicates)
{
Expand Down Expand Up @@ -41,13 +41,19 @@ internal static void CheckForValidityOfValidateCodeParams(this Parameters parame
parameters.NoDuplicates();

//This error was changed from system to url. See: https://chat.fhir.org/#narrow/channel/179202-terminology/topic/Required.20.24validate-code.20parameters/near/482250225
//If a code is provided, a url or a context must be provided (http://hl7.org/fhir/valueset-operation-validate-code.html)
if (parameters.Parameter.Any(p => p.Name == CODEATTRIBUTE) && !(parameters.Parameter.Any(p => p.Name == URLATTRIBUTE) ||
parameters.Parameter.Any(p => p.Name == CONTEXTATTRIBUTE)))
//If a code is provided, an inline valueset, url or a context must be provided (http://hl7.org/fhir/valueset-operation-validate-code.html)
if (parameters.HasParam(CODEATTRIBUTE) && !hasValueSet(parameters))
{
//422 Unproccesable Entity
throw new FhirOperationException($"If a code is provided, a url or a context must be provided", (HttpStatusCode)422);
}

static bool hasValueSet(Parameters p) =>
p.HasParam(URLATTRIBUTE) || p.HasParam(CONTEXTATTRIBUTE) || p.HasParam(VALUESETATTRIBUTE);

}

internal static bool HasParam(this Parameters parameters, string name)
=> parameters.Parameter.Any(p => p.Name == name);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using FluentAssertions;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Specification.Source;
using Hl7.Fhir.Specification.Terminology;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Task = System.Threading.Tasks.Task;

namespace Hl7.Fhir.Specification.Tests
Expand Down Expand Up @@ -69,5 +71,29 @@ public async Task DefaultCoreServiceTest()
result.Parameter.Should().Contain(p => p.Name == "result")
.Subject.Value.Should().BeEquivalentTo(new FhirBoolean(true));
}

[TestMethod]
[DataRow("code", null, null, null, true)]
[DataRow("code", "<ValueSet />", null, null, false)]
[DataRow("code", null, "http://nu.nl/valueset", null, false)]
[DataRow("code", null, null, "context", false)]
[DataRow("code", "<ValueSet />", null, "context", false)]
public void CheckValidateCodeParams(string code, string valueset, string url, string context, bool throws)
{
var parameters = new Parameters()
{
{ "code", code is not null ? new FhirString(code) : null },
{ "url", url is not null ? new FhirUri("http://hl7.org/fhir/ValueSet/administrative-gender") : null },
{ "context", context is not null ? new FhirUri("context") : null },
{ "valueSet", valueset is not null ? new ValueSet() : null }
};

Action validate = () => parameters.CheckForValidityOfValidateCodeParams();

if (!throws)
validate.Should().NotThrow();
else
validate.Should().Throw<FhirOperationException>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Hl7.Fhir.Specification.Source;
using Hl7.Fhir.Specification.Terminology;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Task = System.Threading.Tasks.Task;

namespace Hl7.Fhir.Specification.Tests
Expand Down Expand Up @@ -114,5 +115,29 @@ public async Task CheckErrorBarrier()
var ex = await ac.Should().ThrowAsync<FhirOperationException>();
ex.WithMessage("*compositional code system*");
}

[TestMethod]
[DataRow("code", null, null, null, true)]
[DataRow("code", "<ValueSet />", null, null, false)]
[DataRow("code", null, "http://nu.nl/valueset", null, false)]
[DataRow("code", null, null, "context", false)]
[DataRow("code", "<ValueSet />", null, "context", false)]
public void CheckValidateCodeParams(string code, string valueset, string url, string context, bool throws)
{
var parameters = new Parameters()
{
{ "code", code is not null ? new FhirString(code) : null },
{ "url", url is not null ? new FhirUri("http://hl7.org/fhir/ValueSet/administrative-gender") : null },
{ "context", context is not null ? new FhirUri("context") : null },
{ "valueSet", valueset is not null ? new ValueSet() : null }
};

Action validate = () => parameters.CheckForValidityOfValidateCodeParams();

if (!throws)
validate.Should().NotThrow();
else
validate.Should().Throw<FhirOperationException>();
}
}
}
}