Пример #1
0
        public void ValidateBundleEntry()
        {
            var e = new ResourceEntry<Patient>();
            e.Id = new Uri("http://someserver.org/fhir/Patient/1");
            e.Title = "Some title";

            // Validates mandatory fields?
            validateErrorOrFail(e);
            e.LastUpdated = DateTimeOffset.Now;
            e.Resource = new Patient();
            FhirValidator.Validate(e);

            // Checks nested errors on resource content?
            e.Resource = new Patient { Deceased = new FhirUri() };
            validateErrorOrFail(e, true);

            e.Resource = new Patient();

            var bundle = new Bundle() { Title = "Some feed title" };
            bundle.Id = new Uri("http://someserver.org/fhir/feed/1424234232342");

            // Validates mandatory fields?
            validateErrorOrFail(bundle);
            bundle.LastUpdated = DateTimeOffset.Now;
            FhirValidator.Validate(bundle);

            // Checks nested errors on nested bundle element?
            bundle.Entries.Add(e);
            e.Id = null;
            validateErrorOrFail(bundle,true);
        }
Пример #2
0
        public virtual IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            // TODO: Contained resources share the same internal id resolution space as the parent
            // resource -> verify id uniqueness
            var result = new List <ValidationResult>();

            // Validate specific invariants for contained items. The content of the contained
            // items is validated by the "normal" validation triggered by the FhirElement attribute
            if (Contained != null)
            {
                foreach (var contained in Contained)
                {
                    if (contained.Contained != null && contained.Contained.Any())
                    {
                        result.Add(FhirValidator.BuildResult(validationContext, "Contained resources cannot contain nested contained resources"));
                    }

                    if (contained.Text != null)
                    {
                        result.Add(FhirValidator.BuildResult(validationContext, "Contained resources should not contain narrative"));
                    }
                }
            }

            return(result);
        }
Пример #3
0
        public virtual IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var result = new List <ValidationResult>();

            if (Id != null && !Id.IsAbsoluteUri)
            {
                result.Add(FhirValidator.BuildResult(validationContext, "Entry id must be an absolute URI"));
            }

            if (Bundle.UriHasValue(SelfLink) && !SelfLink.IsAbsoluteUri)
            {
                result.Add(FhirValidator.BuildResult(validationContext, "Entry selflink must be an absolute URI"));
            }

            if (Links.FirstLink != null || Links.LastLink != null || Links.PreviousLink != null || Links.NextLink != null)
            {
                result.Add(FhirValidator.BuildResult(validationContext, "Paging links can only be used on feeds, not entries"));
            }

            if (Tags != null && validationContext.ValidateRecursively())
            {
                FhirValidator.TryValidate(Tags, result, true);
            }

            return(result);
        }
Пример #4
0
        public void ValidateResourceWithIncorrectChildElement()
        {
            // First create an incomplete encounter (class not supplied)
            var enc = new Encounter();

            enc.Status = Encounter.EncounterState.Planned;
            validateErrorOrFail(enc, membername: "ClassElement");
            validateErrorOrFail(enc, true);  // recursive checking shouldn't matter

            enc.Class = Encounter.EncounterClass.Ambulatory;

            // Now, it should work
            FhirValidator.Validate(enc);
            FhirValidator.Validate(enc, true);  // recursive checking shouldnt matter

            // Hide an incorrect datetime deep into the Encounter
            FhirDateTime dt = new FhirDateTime();

            dt.Value = "Ewout Kramer";  // clearly, a wrong datetime

            enc.Hospitalization        = new Encounter.EncounterHospitalizationComponent();
            enc.Hospitalization.Period = new Period()
            {
                StartElement = dt
            };

            // When we do not validate recursively, we should still be ok
            FhirValidator.Validate(enc);

            // When we recurse, this should fail
            validateErrorOrFail(enc, true, membername: "Value");
        }
Пример #5
0
        public void TestAllowedChoices()
        {
            Patient p = new Patient();

            p.Deceased = new FhirBoolean(true);
            FhirValidator.Validate(p);

            // Deceased can either be boolean or dateTime, not FhirUri
            p.Deceased = new FhirUri();
            validateErrorOrFail(p);
        }
Пример #6
0
        public void ContainedResourcesAreValidatedToo()
        {
            Patient p = new Patient();
            // Deceased can either be boolean or dateTime, not FhirUri
            p.Deceased = new FhirUri();

            var pr = new Patient();
            pr.Contained = new List<Resource> { p };

            validateErrorOrFail(pr,true);
            FhirValidator.Validate(pr);
        }
Пример #7
0
        public void ValidateDemoPatient()
        {
            var s = this.GetType().Assembly.GetManifestResourceStream("Hl7.Fhir.Test.patient-example.xml");

            var patient = FhirParser.ParseResource(XmlReader.Create(s));

            ICollection <ValidationResult> results = new List <ValidationResult>();

            FhirValidator.Validate(patient, true);

            Assert.IsTrue(FhirValidator.TryValidate(patient, results, true));
        }
Пример #8
0
        public void TestXhtmlValidation()
        {
            var p = new Patient();

            p.Text = new Narrative() { Div = "<div xmlns='http://www.w3.org/1999/xhtml'><p>should be valid</p></div>", Status = Narrative.NarrativeStatus.Generated  };
            FhirValidator.Validate(p,true);

            p.Text.Div = "<div xmlns='http://www.w3.org/1999/xhtml'><p>should not be valid<p></div>";
            validateErrorOrFail(p,true);

            p.Text.Div = "<div xmlns='http://www.w3.org/1999/xhtml'><img onmouseover='bigImg(this)' src='smiley.gif' alt='Smiley' /></div>";
            validateErrorOrFail(p,true);
        }
Пример #9
0
        void Validate()
        {
            String        savePath = $"\\Temp\\test.json";
            FhirValidator fv       = new FhirValidator();

            fv.Validate("4.0.0", savePath);
            StringBuilder sb      = new StringBuilder();
            bool          success = fv.FormatMessages(sb);

            Trace.WriteLine(sb.ToString());
            Assert.True(success);
            Trace.WriteLine("Validation complete");
        }
Пример #10
0
 private void validateErrorOrFail(object instance, bool recurse=false, string membername=null)
 {
     try
     {
         // should throw error
         FhirValidator.Validate(instance, recurse);
         Assert.Fail();
     }
     catch (ValidationException ve)
     {
         if (membername != null)
             Assert.IsTrue(ve.ValidationResult.MemberNames.Contains(membername));
     }
 }
        public void Z_ValidateInputResources()
        {
            FhirValidator fv = new FhirValidator(Path.Combine(this.cacheDir, "validation.xml"));

            fv.StatusErrors   += this.StatusErrors;
            fv.StatusInfo     += this.StatusInfo;
            fv.StatusWarnings += this.StatusWarnings;
            bool success = fv.ValidateDir(this.resourcesDir, "*.json", "4.0.0");

            //StringBuilder sb = new StringBuilder();
            //fv.FormatMessages(sb);
            //Trace.WriteLine(sb.ToString());
            //Assert.IsTrue(success);
            Trace.WriteLine("Validation complete");
        }
Пример #12
0
        public void TestIdValidation()
        {
            Id id = new Id("az23");

            FhirValidator.Validate(id);
            FhirValidator.Validate(id, true);        // recursive checking shouldnt matter

            id = new Id("!notgood!");
            validateErrorOrFail(id);

            id = new Id("NotGood");
            validateErrorOrFail(id);

            id = new Id("1234567890123456789012345678901234567");
            validateErrorOrFail(id);
        }
        public void G_ValidateAcrExamples()
        {
            FhirValidator fv = new FhirValidator(Path.Combine(this.cacheDir, "validation.xml"));

            fv.StatusErrors   += this.StatusErrors;
            fv.StatusInfo     += this.StatusInfo;
            fv.StatusWarnings += this.StatusWarnings;
            fv.ValidatorArgs   = $" -ig {resourcesDir} -ig {acrResourcesDir}";
            // C:\Development\HL7\BreastRadiologyProfilesV2\IG\Guide\input
            bool          success = fv.ValidateDir(this.acrExamplesDir, "*.json", "4.0.0");
            StringBuilder sb      = new StringBuilder();

            fv.FormatMessages(sb);
            Trace.WriteLine(sb.ToString());
            Assert.IsTrue(success);
        }
Пример #14
0
        public void TestCardinality()
        {
            OperationOutcome oo = new OperationOutcome();
            validateErrorOrFail(oo,true);

            oo.Issue = new List<OperationOutcome.OperationOutcomeIssueComponent>();
            validateErrorOrFail(oo,true);

            var issue = new OperationOutcome.OperationOutcomeIssueComponent();

            oo.Issue.Add(issue);
            validateErrorOrFail(oo,true);

            issue.Severity = OperationOutcome.IssueSeverity.Information;
            FhirValidator.Validate(oo, true);
        }
Пример #15
0
        void RunValidate()
        {
            Console.Clear();
            Thread.Sleep(500);

            Console.WriteLine("Starting validation");
            FhirValidator fv = new FhirValidator(ValidationPath);

            fv.JarPath         = this.JarPath;
            fv.StatusErrors   += this.StatusErrors;
            fv.StatusInfo     += this.StatusInfo;
            fv.StatusWarnings += this.StatusWarnings;
            fv.ValidatorArgs   = $" -ig {this.ResourcesPath} ";
            fv.ValidateDir(this.ExamplesPath, "*.json", "4.0.0");
            Console.WriteLine("Validation complete");
            Console.WriteLine("Press 'q' to quit the sample.");
        }
Пример #16
0
        public void ValidateEntry()
        {
            var pe = new ResourceEntry<Patient>(new Uri("http://www.nu.nl/fhir/Patient/1"), DateTimeOffset.Now, new Patient());
            Assert.IsNotNull(pe.Id);
            Assert.IsNotNull(pe.Title);
            Assert.IsNotNull(pe.LastUpdated);
            Assert.IsNotNull(pe.Resource);
            FhirValidator.Validate(pe);

            var b = new Bundle("A test feed", DateTimeOffset.Now);
            b.AuthorName = "Ewout";

            Assert.IsNotNull(pe.Id);
            Assert.IsNotNull(pe.Title);
            Assert.IsNotNull(pe.LastUpdated);
            b.Entries.Add(pe);
            FhirValidator.Validate(b);
        }
        public void D5_ValidateAcr()
        {
            //
            IGBuilder.RemoveFragmentExtensions(this.acrResourcesDir);
            FhirValidator fv = new FhirValidator(Path.Combine(this.cacheDir, "validation.xml"));

            fv.StatusErrors   += this.StatusErrors;
            fv.StatusInfo     += this.StatusInfo;
            fv.StatusWarnings += this.StatusWarnings;
            bool          success = fv.ValidateDir(this.acrResourcesDir, "*.json", "4.0.0");
            StringBuilder sb      = new StringBuilder();

            fv.FilterMessages("not resolve");
            fv.FormatMessages(sb);
            Trace.WriteLine(sb.ToString());
            Assert.IsTrue(success);
            Trace.WriteLine("Validation complete");
        }
Пример #18
0
        public override IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var result = new List <ValidationResult>();

            result.AddRange(base.Validate(validationContext));

            if (Content == null)
            {
                result.Add(FhirValidator.BuildResult(validationContext, "Entry must contain (possibly 0-length) data in Content element"));
            }

            if (ContentType == null)
            {
                result.Add(FhirValidator.BuildResult(validationContext, "Entry must contain a ContentType"));
            }

            return(result);
        }
Пример #19
0
        public void ValidateDemoPatient()
        {
            var s = this.GetType().Assembly.GetManifestResourceStream("Hl7.Fhir.Test.patient-example.xml");

            var patient = (Patient)FhirParser.ParseResource(XmlReader.Create(s));

            ICollection <ValidationResult> results = new List <ValidationResult>();

            FhirValidator.Validate(patient, true);

            Assert.IsTrue(FhirValidator.TryValidate(patient, results, true));

            patient.Identifier[0].System = "urn:oid:crap really not valid";

            results = new List <ValidationResult>();

            Assert.IsFalse(FhirValidator.TryValidate(patient, results, true));
            Assert.IsTrue(results.Count > 0);
        }
Пример #20
0
        public void TestContainedConstraints()
        {
            var pat = new Patient();
            var patn = new Patient();
            pat.Contained = new List<Resource> { patn } ;
            patn.Contained = new List<Resource> { new Patient() };

            // Contained resources should not themselves contain resources
            validateErrorOrFail(pat);

            patn.Contained = null;
            FhirValidator.Validate(pat);

            patn.Text = new Narrative();
            patn.Text.Div = "<div>Narrative in contained resource</div>";

            // Contained resources should not contain narrative
            validateErrorOrFail(pat);
        }
Пример #21
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var result = new List <ValidationResult>();

            //if (String.IsNullOrWhiteSpace(Title))
            //    result.Add(new ValidationResult("Feed must contain a title", FhirValidator.SingleMemberName("Title"));

            //if (!UriHasValue(Id))
            //    result.Add(new ValidationResult("Feed must have an id"));
            //else
            //    if (!Id.IsAbsoluteUri)
            //        result.Add(new ValidationResult("Feed id must be an absolute URI"));

            if (Id != null && !Id.IsAbsoluteUri)
            {
                result.Add(FhirValidator.BuildResult(validationContext, "Feed id must be an absolute URI"));
            }

            //if (LastUpdated == null)
            //    result.Add(new ValidationResult("Feed must have a updated date"));

            if (Links.SearchLink != null)
            {
                result.Add(FhirValidator.BuildResult(validationContext, "Links with rel='search' can only be used on feed entries"));
            }

            bool feedHasAuthor = !String.IsNullOrEmpty(this.AuthorName);

            if (Entries != null && validationContext.ValidateRecursively())
            {
                foreach (var entry in Entries.Where(e => e != null))
                {
                    if (!feedHasAuthor && entry is ResourceEntry && String.IsNullOrEmpty(((ResourceEntry)entry).AuthorName))
                    {
                        result.Add(FhirValidator.BuildResult(validationContext, "Bundle's author and Entry author cannot both be empty"));
                    }

                    FhirValidator.TryValidate(entry, result, validationContext.ValidateRecursively());
                }
            }

            return(result);
        }
        public void Z_ValidateOutputResources()
        {
            String rDir = Path.Combine(this.guideDir,
                                       "input",
                                       "resources");
            FhirValidator fv = new FhirValidator();

            fv.StatusErrors   += this.StatusErrors;
            fv.StatusInfo     += this.StatusInfo;
            fv.StatusWarnings += this.StatusWarnings;

            bool          success = fv.ValidateDir(rDir, "*.json", "4.0.0");
            StringBuilder sb      = new StringBuilder();

            //fv.FormatMessages(sb);
            //Trace.WriteLine(sb.ToString());
            Assert.IsTrue(success);
            Assert.IsTrue(fv.HasErrors == false);
            Trace.WriteLine("Validation complete");
        }
 private static void validateElement(object value, ValidationContext validationContext, List <ValidationResult> result)
 {
     FhirValidator.TryValidate(value, result, validationContext.ValidateRecursively());
 }