Skip to content

Commit

Permalink
Merge pull request #264 from FirelyTeam/feature/back-to-itypedelement
Browse files Browse the repository at this point in the history
Avoid use of Inspector.Base
  • Loading branch information
mmsmits authored Jan 22, 2024
2 parents 2b21a2b + 7260265 commit 8544608
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\CardinalityBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\ContentReferenceBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\FhirPathBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\FixedBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\MaxValueBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\MinValueBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\PatternBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\RegexBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\BindingBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\StandardBuilders.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\CanonicalBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\PatternBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\FixedBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\MaxLengthBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SchemaBuilders\TypeReferenceBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CommonTypeRefComponent.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Firely.Fhir.Validation.Compilation
/// </summary>
internal class FixedBuilder : ISchemaBuilder
{

/// <inheritdoc/>
public IEnumerable<IAssertion> Build(ElementDefinitionNavigator nav, ElementConversionMode? conversionMode = ElementConversionMode.Full)
{
Expand All @@ -29,7 +30,10 @@ public IEnumerable<IAssertion> Build(ElementDefinitionNavigator nav, ElementConv
var def = nav.Current;

if (def.Fixed is not null)
yield return new FixedValidator(def.Fixed.ToTypedElement(ModelInspector.Base));
{
var inspector = ModelInspector.ForType(def.Fixed.GetType());
yield return new FixedValidator(def.Fixed.ToTypedElement(inspector));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public IEnumerable<IAssertion> Build(ElementDefinitionNavigator nav, ElementConv
var def = nav.Current;

if (def.MaxValue is not null)
yield return new MinMaxValueValidator(def.MaxValue.ToTypedElement(ModelInspector.Base), MinMaxValueValidator.ValidationMode.MaxValue);
{
var inspector = ModelInspector.ForType(def.MaxValue.GetType());
yield return new MinMaxValueValidator(def.MaxValue.ToTypedElement(inspector),
MinMaxValueValidator.ValidationMode.MaxValue);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public IEnumerable<IAssertion> Build(ElementDefinitionNavigator nav, ElementConv
var def = nav.Current;

if (def.MinValue is not null)
yield return new MinMaxValueValidator(def.MinValue.ToTypedElement(ModelInspector.Base), MinMaxValueValidator.ValidationMode.MinValue);
{
var inspector = ModelInspector.ForType(def.MinValue.GetType());
yield return new MinMaxValueValidator(def.MinValue.ToTypedElement(inspector),
MinMaxValueValidator.ValidationMode.MinValue);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Firely.Fhir.Validation.Compilation
/// <remarks>This constraint is not part of an element refering to a backbone type (see eld-5).</remarks>
internal class PatternBuilder : ISchemaBuilder
{

/// <inheritdoc/>
public IEnumerable<IAssertion> Build(ElementDefinitionNavigator nav, ElementConversionMode? conversionMode = ElementConversionMode.Full)
{
Expand All @@ -28,7 +29,10 @@ public IEnumerable<IAssertion> Build(ElementDefinitionNavigator nav, ElementConv
var def = nav.Current;

if (def.Pattern is not null)
yield return new PatternValidator(def.Pattern.ToTypedElement(ModelInspector.Base));
{
var inspector = ModelInspector.ForType(def.Pattern.GetType());
yield return new PatternValidator(def.Pattern.ToTypedElement(inspector));
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Firely.Fhir.Validation.Shared/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ public Validator(
return toTypedElement(r);
}
}

private static ITypedElement? toTypedElement(object? o) =>
o switch
{
null => null,
ElementNode en => en,
Resource r => r.ToTypedElement(ModelInfo.ModelInspector),
Resource r => r.ToTypedElement(),
_ => throw new ArgumentException("Reference resolver must return either a Resource or ElementNode.")
};

private readonly ValidationSettings _settings;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Firely.Fhir.Validation/Impl/FixedValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static string displayValue(ITypedElement te) =>
}

/// <inheritdoc />
public JToken ToJson() => new JProperty($"Fixed[{FixedValue.InstanceType}]", FixedValue.ToPropValue());
public JToken ToJson() => new JProperty($"fixed[{FixedValue.InstanceType}]", FixedValue.ToPropValue());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"pathSelector": {
"path": "system",
"assertion": {
"Fixed[uri]": "http://example.com/some-bsn-uri"
"fixed[uri]": "http://example.com/some-bsn-uri"
}
}
},
Expand Down Expand Up @@ -160,7 +160,7 @@
},
"system": {
"id": "#Patient.identifier.system",
"Fixed[uri]": "http://example.com/some-bsn-uri",
"fixed[uri]": "http://example.com/some-bsn-uri",
"FastInvariant-ele1": {},
"cardinality": "0..1",
"ref": "http://hl7.org/fhir/StructureDefinition/uri"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"pathSelector": {
"path": "system",
"assertion": {
"Fixed[uri]": "http://example.com/some-bsn-uri"
"fixed[uri]": "http://example.com/some-bsn-uri"
}
}
},
Expand Down Expand Up @@ -160,7 +160,7 @@
},
"system": {
"id": "#Patient.identifier.system",
"Fixed[uri]": "http://example.com/some-bsn-uri",
"fixed[uri]": "http://example.com/some-bsn-uri",
"FastInvariant-ele1": {},
"cardinality": "0..1",
"ref": "http://hl7.org/fhir/StructureDefinition/uri"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"pathSelector": {
"path": "system",
"assertion": {
"Fixed[uri]": "http://example.com/some-bsn-uri"
"fixed[uri]": "http://example.com/some-bsn-uri"
}
}
},
Expand Down Expand Up @@ -139,7 +139,7 @@
},
"system": {
"id": "#Patient.identifier.system",
"Fixed[uri]": "http://example.com/some-bsn-uri",
"fixed[uri]": "http://example.com/some-bsn-uri",
"cardinality": "0..1",
"ref": "http://hl7.org/fhir/StructureDefinition/uri"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,5 @@ public void InRangeAsync()
result = cardinality.Validate(ElementNode.CreateList("1"), ValidationSettings.BuildMinimalContext(), new ValidationState());
Assert.IsFalse(result.IsSuccessful);
}

[TestMethod]
[Ignore]
public void TestFromEvidence()
{
var b = new ValidatorBenchmarks();
b.GlobalSetup();
b.WipValidator();
}
}
}
9 changes: 4 additions & 5 deletions test/Firely.Fhir.Validation.Tests/Impl/TestSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ public void ValidateSchema()
humanName.Add("given", "Patrick", "string");
humanName.Add("given", new string('x', 41), "string");
humanName.Add("given", "1", "integer");


var result = myHumanNameSchema.ToJson().ToString();

FluentActions.Invoking(() => myHumanNameSchema.ToJson().ToString()).Should().NotThrow();

var schemaResolver = new InMemoryElementSchemaResolver(new[] { stringSchema });

Expand Down Expand Up @@ -121,7 +120,7 @@ public InMemoryElementSchemaResolver(IEnumerable<ElementSchema> schemas)
}

public ElementSchema? GetSchema(Canonical schemaUri) =>
_schemas.TryGetValue(schemaUri, out var schema) ? schema : null;
_schemas.GetValueOrDefault(schemaUri);
}

[TestMethod]
Expand All @@ -133,7 +132,7 @@ public void ValidateBloodPressureSchema()
("code", new CardinalityValidator(min: 1)),
("value[x]", new AllValidator(new CardinalityValidator(min: 1), new FhirTypeLabelValidator("Quantity")))
)
); ;
);

static ITypedElement buildCodeableConcept(string system, string code)
{
Expand Down

0 comments on commit 8544608

Please sign in to comment.