001package org.hl7.fhir.r5.utils.validation;
002
003import java.util.EnumSet;
004import java.util.List;
005
006import org.hl7.fhir.r5.elementmodel.Element;
007import org.hl7.fhir.r5.model.ElementDefinition;
008import org.hl7.fhir.r5.model.StructureDefinition;
009import org.hl7.fhir.r5.model.ValueSet;
010import org.hl7.fhir.r5.utils.validation.constants.ContainedReferenceValidationPolicy;
011import org.hl7.fhir.r5.utils.validation.constants.ReferenceValidationPolicy;
012import org.hl7.fhir.utilities.validation.ValidationMessage;
013import org.hl7.fhir.r5.utils.validation.constants.CodedContentValidationPolicy;
014import org.hl7.fhir.r5.utils.validation.IValidationPolicyAdvisor.ElementValidationAction;
015import org.hl7.fhir.r5.utils.validation.constants.BindingKind;
016
017public interface IValidationPolicyAdvisor {
018
019  /**
020   *
021   * @param validator
022   * @param appContext What was originally provided from the app for it's context
023   * @param path Path that led us to this resource.
024   * @param url Url of the profile the container resource is being validated against.
025   * @return {@link ReferenceValidationPolicy}
026   */
027  ReferenceValidationPolicy policyForReference(IResourceValidator validator,
028                                               Object appContext,
029                                               String path,
030                                               String url);
031
032  /**
033   * //TODO pass through the actual containing Element as opposed to the type, id
034   * @param validator
035   * @param appContext What was originally provided from the app for it's context
036   * @param containerType Type of the resources that contains the resource being validated
037   * @param containerId Id of the resources that contains the resource being validated
038   * @param containingResourceType Type of the resource that will be validated (BUNDLE_ENTRY, BUNDLE_OUTCOME, CONTAINED_RESOURCE, PARAMETER)
039   * @param path Path that led us to this resource.
040   * @param url Url of the profile the container resource is being validated against.
041   * @return {@link ReferenceValidationPolicy}
042   */
043  ContainedReferenceValidationPolicy policyForContained(IResourceValidator validator,
044                                                        Object appContext,
045                                                        StructureDefinition structure,
046                                                        ElementDefinition element,
047                                                        String containerType,
048                                                        String containerId,
049                                                        Element.SpecialElement containingResourceType,
050                                                        String path,
051                                                        String url);
052
053
054  public enum ResourceValidationAction {
055    BaseType,
056    StatedProfiles,
057    MetaProfiles,
058    GlobalProfiles
059  }
060  
061  EnumSet<ResourceValidationAction> policyForResource(IResourceValidator validator,
062      Object appContext,
063      StructureDefinition type,
064      String path);
065
066  public enum ElementValidationAction {
067    Cardinality, // though you can't stop slice matching cardinality checks from happening 
068    Invariants, 
069    Bindings,
070    AdditionalBindings,
071    StatusCheck
072  }
073  
074  EnumSet<ElementValidationAction> policyForElement(IResourceValidator validator,
075                                                      Object appContext,
076                                                      StructureDefinition structure,
077                                                      ElementDefinition element,
078                                                      String path);
079  
080  public enum AdditionalBindingPurpose {
081    Minimum,
082    Required,
083    Extensible,
084    Current,
085    Preferred,
086    Ui
087  }
088  
089  public enum CodedContentValidationAction {
090    VSCheck,  
091    VSCheckThisCode,
092    NotFound, 
093    InvalidCode,
094    InvalidDisplay,
095    CannotInfer,
096    CodeRule,
097    VSInvalid,
098    StatusCheck
099  }
100  
101  /**
102   * Called before validating a concept in an instance against the terminology sub-system
103   * 
104   * There's two reasons to use this policy advisor feature:
105   *   - save time by not calling the terminology server for validation that don't bring value to the context calling the validation
106   *   - suppressing known issues from being listed as a problem
107   *   
108   * Note that the terminology subsystem has two parts: a mini-terminology server running inside the 
109   * validator, and then calling out to an external terminology service (usually tx.fhir.org, though you
110   * run your own local copy of this - see https://confluence.hl7.org/display/FHIR/Running+your+own+copy+of+tx.fhir.org).
111   * You can't tell which subsystem will handle the terminology validation directly from the content provided here which
112   * subsystem will be called - you'll haev to investigate based on your set up. (matters, since it makes a huge performance 
113   * difference, though it also depends on caching, and the impact of caching is also not known at this point)
114   *   
115   * @param validator
116   * @param appContext What was originally provided from the app for it's context
117   * @param stackPath The current path for the stack. Note that the because of cross-references and FHIRPath conformsTo() statements, the stack can wind through the content unpredictably. 
118   * @param definition the definition being validated against (might be useful: ElementDefinition.base.path, ElementDefinition.type, ElementDefinition.binding
119   * @param structure The structure definition that contains the element definition being validated against (may be from the base spec, may be from a profile)
120   * @param kind The part of the binding being validated
121   * @param valueSet The value set for the binding part that's being validated 
122   * @param systems A list of canonical URls (including versions if known) of the systems in the instance that's being validated. Note that if a plain code is being validated, then there'll be no known system when this is called (systems will be empty, not null) 
123   * @return {@link CodedContentValidationPolicy}
124   */
125  EnumSet<CodedContentValidationAction> policyForCodedContent(IResourceValidator validator,
126                                                        Object appContext,
127                                                        String stackPath,
128                                                        ElementDefinition definition,
129                                                        StructureDefinition structure,
130                                                        BindingKind kind,
131                                                        AdditionalBindingPurpose purpose,
132                                                        ValueSet valueSet,
133                                                        List<String> systems);
134
135  /**
136   * This is called after a resource has been validated against the base structure, 
137   * but before it's validated against any profiles specified in .meta.profile or in the parameters. 
138   * This can be used to determine what additional profiles should be applied, for instance
139   * those derived from the http://hl7.org/fhir/tools/StructureDefinition/profile-mapping extension
140   *  
141   * Note that the resource is an elementModel resource, not an IBaseResource. This is less convenient to 
142   * read values from, but is the way the internals of the validator works (e.g. the version of the resource 
143   * might be any version from R2-R6)
144   * 
145   * The base implementation applies the mandatory vital signs to observations that have LOINC or SNOMED CT
146   * codes that indicate that they are vital signs. Note that these profiles are not optional; all vital sign resources 
147   * are required to conform to them. For this reason, if you're providing your own policy advisor, you should
148   * keep a reference to the default one, or call BasePolicyAdvisorForFullValidation directly. You can choose not to,
149   * but if you do, you are allowing for resources that deviate from the FHIR specification (in a way that the 
150   * community considers clinically unsafe, since it means that software (probably) will miss vital signs for 
151   * patients).
152   * 
153   * @param validator
154   * @param appContext What was originally provided from the app for it's context
155   * @param stackPath The current path for the stack. Note that the because of cross-references and FHIRPath conformsTo() statements, the stack can wind through the content unpredictably. 
156   * @param definition the definition being validated against (might be useful: ElementDefinition.base.path, ElementDefinition.type, ElementDefinition.binding
157   * @param structure The structure definition that contains the element definition being validated against (may be from the base spec, may be from a profile)
158   * @param resource The actual resource (as an element model) so that the implementation can inspect the values in order to decide what profiles to apply 
159   * @param valid true if the resource is so far considered valid
160   * @param messages all the validation messages. Implementations can inspect this, but the real purpose is to populate the messages with information messages explaining why profiles were (or weren't) applied
161   * @return
162   */
163  List<StructureDefinition> getImpliedProfilesForResource(IResourceValidator validator,
164                                                        Object appContext,
165                                                        String stackPath,
166                                                        ElementDefinition definition,
167                                                        StructureDefinition structure,
168                                                        Element resource,
169                                                        boolean valid,
170                                                        IMessagingServices msgServices,
171                                                        List<ValidationMessage> messages);
172  
173}