Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Remove the ignore option in the validation setting to check best practices constraints #1946

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public void DoNotFollowRefsSuppressesWarning()
[Fact]
public void TestConstraintBestPractices()
{
var validator = new Validator(new ValidationSettings { ResourceResolver = _source });
var validator = new Validator(new ValidationSettings { ResourceResolver = _source, ConstraintsToIgnore = Array.Empty<string>() });

Patient p = new Patient
{
Expand All @@ -550,17 +550,17 @@ public void TestConstraintBestPractices()

var result = validator.Validate(p);
Assert.True(result.Success);
Assert.Equal(0, result.Warnings);
Assert.Equal(1, result.Warnings);
Assert.Equal(0, result.Errors);

validator.Settings.ConstraintBestPractices = ConstraintBestPractices.Enabled;
validator.Settings.ConstraintBestPracticesSeverity = ConstraintBestPracticesSeverity.Error;
result = validator.Validate(p);
Assert.False(result.Success);
Assert.Equal(0, result.Warnings);
Assert.Equal(1, result.Errors);
Assert.Contains("Instance failed constraint dom-6 \"A resource should have narrative for robust management\"", result.Issue[0].ToString());

validator.Settings.ConstraintBestPractices = ConstraintBestPractices.Disabled;
validator.Settings.ConstraintBestPracticesSeverity = ConstraintBestPracticesSeverity.Warning;
result = validator.Validate(p);
Assert.True(result.Success);
Assert.Equal(1, result.Warnings);
Expand Down Expand Up @@ -1357,7 +1357,7 @@ public void ConditionCon3ConstraintTest()

var settings = new ValidationSettings
{
ConstraintBestPractices = ConstraintBestPractices.Enabled,
ConstraintBestPracticesSeverity = ConstraintBestPracticesSeverity.Error,
ResourceResolver = new CachedResolver(ZipSource.CreateValidationSource())
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ public static OperationOutcome ValidateFp(this Validator v, string structureDefi
// 20190703 Issue 447 - rng-2 is incorrect in DSTU2 and STU3. EK
// should be removed from STU3/R4 once we get the new normative version
// of FP up, which could do comparisons between quantities.
if (constraintElement.Key == "rng-2") continue;
if (v.Settings.ConstraintsToIgnore.Contains(constraintElement.Key)) continue;

if (constraintElement.GetBoolExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-bestpractice") == true)
if (v.Settings.ConstraintBestPractices == ConstraintBestPractices.Ignore)
continue;
else if (v.Settings.ConstraintBestPractices == ConstraintBestPractices.Enabled)
if (v.Settings.ConstraintBestPracticesSeverity == ConstraintBestPracticesSeverity.Error)
constraintElement.Severity = ElementDefinition.ConstraintSeverity.Error;
else if (v.Settings.ConstraintBestPractices == ConstraintBestPractices.Disabled)
else if (v.Settings.ConstraintBestPracticesSeverity == ConstraintBestPracticesSeverity.Warning)
constraintElement.Severity = ElementDefinition.ConstraintSeverity.Warning;

bool success = false;
Expand Down
21 changes: 13 additions & 8 deletions src/Hl7.Fhir.Specification/Validation/ValidationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@

namespace Hl7.Fhir.Validation
{
public enum ConstraintBestPractices
public enum ConstraintBestPracticesSeverity
{
Ignore,
Enabled,
Disabled
Warning,
Error
}

/// <summary>Configuration settings for the <see cref="Validator"/> class.</summary>
Expand Down Expand Up @@ -82,6 +81,11 @@ public SnapshotGeneratorSettings GenerateSnapshotSettings
/// </summary>
public bool SkipConstraintValidation { get; set; } // = false;

/// <summary>
/// A list of constraints to be ignored by the validator. Default values are dom-6 and rng-2
/// </summary>
public string[] ConstraintsToIgnore { get; set; } = new string[] { "dom-6", "rng-2" };


/// <summary>
/// If a reference is encountered that references to a resource outside of the current instance being validated,
Expand All @@ -101,11 +105,11 @@ public SnapshotGeneratorSettings GenerateSnapshotSettings
public bool EnableXsdValidation { get; set; } // = false;

/// <summary>
/// If set to enabled, the validator will treat as error the violations of the invariants marked as best practices, if disabled they will be marked as warnings.
/// Choose whether the validator will treat the violations of the invariants marked as best practices as errors or as warnings.
/// </summary>
public ConstraintBestPractices ConstraintBestPractices { get; set; } // = Ignore;
public ConstraintBestPracticesSeverity ConstraintBestPracticesSeverity { get; set; }



/// <summary>
/// Determine where to retrieve the XSD schemas from when when Xsd validation is enabled and run.
/// </summary>
Expand All @@ -130,7 +134,7 @@ public void CopyTo(ValidationSettings other)
{
if (other == null) throw Error.ArgumentNull(nameof(other));

other.ConstraintBestPractices = ConstraintBestPractices;
other.ConstraintBestPracticesSeverity = ConstraintBestPracticesSeverity;
other.GenerateSnapshot = GenerateSnapshot;
other.GenerateSnapshotSettings = GenerateSnapshotSettings?.Clone();
other.EnableXsdValidation = EnableXsdValidation;
Expand All @@ -142,6 +146,7 @@ public void CopyTo(ValidationSettings other)
other.FhirPathCompiler = FhirPathCompiler;
other.ResourceMapping = ResourceMapping;
other.XsdSchemaCollection = XsdSchemaCollection;
other.ConstraintsToIgnore = ConstraintsToIgnore;
}

/// <summary>Creates a new <see cref="ValidationSettings"/> object that is a copy of the current instance.</summary>
Expand Down