1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.healthconnect.phr.validations; 18 19 import static android.health.connect.datatypes.FhirResource.FhirResourceType; 20 21 import android.health.connect.datatypes.FhirVersion; 22 23 import com.android.healthfitness.flags.Flags; 24 25 import org.json.JSONObject; 26 27 /** 28 * Performs validation on a FHIR JSON Object, based on the FHIR version R4. 29 * 30 * @hide 31 */ 32 public class FhirResourceValidator { 33 34 private final FhirObjectTypeValidator mFhirObjectTypeValidator; 35 FhirResourceValidator()36 public FhirResourceValidator() { 37 if (!Flags.phrFhirStructuralValidation()) { 38 throw new UnsupportedOperationException("Validating FHIR resources is not supported."); 39 } 40 41 // TODO: b/374058373 - When we support R5 or other versions this needs to be updated to 42 // support other fhir versions. 43 FhirSpecProvider fhirSpec = new FhirSpecProvider(FhirVersion.parseFhirVersion("4.0.1")); 44 45 mFhirObjectTypeValidator = new FhirObjectTypeValidator(fhirSpec); 46 } 47 48 /** 49 * Validates the provided {@code fhirJsonObject} against the schema of the provided {@code 50 * fhirResourceType} and {@code fhirVersion}. 51 * 52 * @throws IllegalArgumentException if the resource is invalid. 53 */ validateFhirResource( JSONObject fhirJsonObject, @FhirResourceType int fhirResourceType, FhirVersion fhirVersion)54 public void validateFhirResource( 55 JSONObject fhirJsonObject, 56 @FhirResourceType int fhirResourceType, 57 FhirVersion fhirVersion) { 58 mFhirObjectTypeValidator.validate(fhirJsonObject, fhirResourceType, fhirVersion); 59 } 60 } 61