FHIR Validation using HAPI for Custom Resources (Non-FHIR Resources)

I am trying to validate a “custom resource” using HAPI FHIR library. But its not identifying the resource for validation.
I think the issue is that my custom resource is not recognized as a resource. Is there a way to register my custom resource so that validation happens.

Could any one suggest a way to validate with HAPI Framework.

Example:

My structure defenition: ( Custom Resource say Sterilize

{
		"resourceType": "StructureDefinition",
		"meta": {
			"lastUpdated": "2017-04-19T07:44:43.294+10:00"
		},
		"text": {
			"status": "generated",
			"div": "<div>!-- Snipped for Brevity --></div>"
		},
		"extension": [{
				"url": "http://hl7.org/fhir/StructureDefinition/structuredefinition-fmm",
				"valueInteger": 5
			},
			{
				"url": "http://hl7.org/fhir/StructureDefinition/structuredefinition-wg",
				"valueCode": "pa"
			}
		],
		"url": "http://hl7.org/fhir/StructureDefinition/Sterilize",
		"name": "Sterilize",
		"status": "draft",
		"date": "2017-04-19T07:44:43+10:00",
		"fhirVersion": "3.0.1",
		"kind": "resource",
		"abstract": false,
		"type": "Sterilize",
		"baseDefinition": "http://hl7.org/fhir/StructureDefinition/DomainResource",
		"derivation": "specialization",
		"snapshot": {
			"element": [{
					"id": "Sterilize",
					"path": "Sterilize",
					"short": "Information about an individual or animal receiving health care services",
					"definition": "Demographics and other administrative information about an individual or animal receiving care or other health-related services.",
					"alias": [
						"SubjectOfCare Client Resident"
					],
					"min": 0,
					"max": "*",
					"constraint": [{
							"key": "dom-2",
							"severity": "error",
							"human": "If the resource is contained in another resource, it SHALL NOT contain nested Resources",
							"expression": "contained.contained.empty()",
							"xpath": "not(parent::f:contained and f:contained)",
							"source": "DomainResource"
						},
						{
							"key": "dom-1",
							"severity": "error",
							"human": "If the resource is contained in another resource, it SHALL NOT contain any narrative",
							"expression": "contained.text.empty()",
							"xpath": "not(parent::f:contained and f:text)",
							"source": "DomainResource"
						},
						{
							"key": "dom-4",
							"severity": "error",
							"human": "If a resource is contained in another resource, it SHALL NOT have a meta.versionId or a meta.lastUpdated",
							"expression": "contained.meta.versionId.empty() and contained.meta.lastUpdated.empty()",
							"xpath": "not(exists(f:contained/*/f:meta/f:versionId)) and not(exists(f:contained/*/f:meta/f:lastUpdated))",
							"source": "DomainResource"
						}
					],
					"mapping": [{
							"identity": "rim",
							"map": "Entity. Role, or Act"
						},
						{
							"identity": "rim",
							"map": "Patient[classCode=PAT]"
						},
						{
							"identity": "cda",
							"map": "ClinicalDocument.recordTarget.patientRole"
						},
						{
							"identity": "w5",
							"map": "administrative.individual"
						}
					]
				},
				{
					"id": "Sterilize.id",
					"path": "Sterilize.id",
					"short": "Logical id of this artifact",
					"definition": "The logical id of the resource, as used in the URL for the resource. Once assigned, this value never changes.",
					"comment": "The only time that a resource does not have an id is when it is being submitted to the server using a create operation.",
					"min": 0,
					"max": "1",
					"base": {
						"path": "Resource.id",
						"min": 0,
						"max": "1"
					},
					"type": [{
						"code": "id"
					}],
					"isSummary": true
				},
				{
					"id": "Sterilize.meta",
					"path": "Sterilize.meta",
					"short": "Metadata about the resource",
					"definition": "The metadata about the resource. This is content that is maintained by the infrastructure. Changes to the content may not always be associated with version changes to the resource.",
					"min": 0,
					"max": "1",
					"base": {
						"path": "Resource.meta",
						"min": 0,
						"max": "1"
					},
					"type": [{
						"code": "Meta"
					}],
					"isSummary": true
				},
				{
					"id": "Sterilize.implicitRules",
					"path": "Sterilize.implicitRules",
					"short": "A set of rules under which this content was created",
					"definition": "A reference to a set of rules that were followed when the resource was constructed, and which must be understood when processing the content.",
					"comment": "Asserting this rule set restricts the content to be only understood by a limited set of trading partners. This inherently limits the usefulness of the data in the long term. However, the existing health eco-system is highly fractured, and not yet ready to define, collect, and exchange data in a generally computable sense. Wherever possible, implementers and/or specification writers should avoid using this element. \n\nThis element is labelled as a modifier because the implicit rules may provide additional knowledge about the resource that modifies it's meaning or interpretation.",
					"min": 0,
					"max": "1",
					"base": {
						"path": "Resource.implicitRules",
						"min": 0,
						"max": "1"
					},
					"type": [{
						"code": "uri"
					}],
					"isModifier": true,
					"isSummary": true
				},
				{
					"id": "Sterilize.language",
					"path": "Sterilize.language",
					"short": "Language of the resource content",
					"definition": "The base language in which the resource is written.",
					"comment": "Language is provided to support indexing and accessibility (typically, services such as text to speech use the language tag). The html language tag in the narrative applies  to the narrative. The language tag on the resource may be used to specify the language of other presentations generated from the data in the resource  Not all the content has to be in the base language. The Resource.language should not be assumed to apply to the narrative automatically. If a language is specified, it should it also be specified on the div element in the html (see rules in HTML5 for information about the relationship between xml:lang and the html lang attribute).",
					"min": 0,
					"max": "1",
					"base": {
						"path": "Resource.language",
						"min": 0,
						"max": "1"
					},
					"type": [{
						"code": "code"
					}],
					"binding": {
						"extension": [{
								"url": "http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet",
								"valueReference": {
									"reference": "http://hl7.org/fhir/ValueSet/all-languages"
								}
							},
							{
								"url": "http://hl7.org/fhir/StructureDefinition/elementdefinition-bindingName",
								"valueString": "Language"
							},
							{
								"url": "http://hl7.org/fhir/StructureDefinition/elementdefinition-isCommonBinding",
								"valueBoolean": true
							}
						],
						"strength": "extensible",
						"description": "A human language.",
						"valueSetReference": {
							"reference": "http://hl7.org/fhir/ValueSet/languages"
						}
					}
				},
				{
					"id": "Sterilize.text",
					"path": "Sterilize.text",
					"short": "Text summary of the resource, for human interpretation",
					"definition": "A human-readable narrative that contains a summary of the resource, and may be used to represent the content of the resource to a human. The narrative need not encode all the structured data, but is required to contain sufficient detail to make it \"clinically safe\" for a human to just read the narrative. Resource definitions may define what content should be represented in the narrative to ensure clinical safety.",
					"comment": "Contained resources do not have narrative. Resources that are not contained SHOULD have a narrative. In some cases, a resource may only have text with little or no additional discrete data (as long as all minOccurs=1 elements are satisfied).  This may be necessary for data from legacy systems where information is captured as a \"text blob\" or where text is additionally entered raw or narrated and encoded in formation is added later.",
					"alias": [
						"narrative",
						"html",
						"xhtml",
						"display"
					],
					"min": 0,
					"max": "1",
					"base": {
						"path": "DomainResource.text",
						"min": 0,
						"max": "1"
					},
					"type": [{
						"code": "Narrative"
					}],
					"condition": [
						"dom-1"
					],
					"mapping": [{
						"identity": "rim",
						"map": "Act.text?"
					}]
				},
				{
					"id": "Sterilize.contained",
					"path": "Sterilize.contained",
					"short": "Contained, inline Resources",
					"definition": "These resources do not have an independent existence apart from the resource that contains them - they cannot be identified independently, and nor can they have their own independent transaction scope.",
					"comment": "This should never be done when the content can be identified properly, as once identification is lost, it is extremely difficult (and context dependent) to restore it again.",
					"alias": [
						"inline resources",
						"anonymous resources",
						"contained resources"
					],
					"min": 0,
					"max": "*",
					"base": {
						"path": "DomainResource.contained",
						"min": 0,
						"max": "*"
					},
					"type": [{
						"code": "Resource"
					}],
					"mapping": [{
						"identity": "rim",
						"map": "N/A"
					}]
				},
				
				{
					"id": "Sterilize.identifier",
					"path": "Sterilize.identifier",
					"short": "An identifier for this patient",
					"definition": "An identifier for this patient.",
					"requirements": "Patients are almost always assigned specific numerical identifiers.",
					"min": 0,
					"max": "*",
					"type": [{
						"code": "Identifier"
					}],
					"isSummary": true,
					"mapping": [{
							"identity": "v2",
							"map": "PID-3"
						},
						{
							"identity": "rim",
							"map": "id"
						},
						{
							"identity": "cda",
							"map": ".id"
						},
						{
							"identity": "w5",
							"map": "id"
						}
					]
				},
				{
					"id": "Sterilize.active",
					"path": "Sterilize.active",
					"short": "Whether this patient's record is in active use",
					"definition": "Whether this patient record is in active use.",
					"comment": "Default is true. If a record is inactive, and linked to an active record, then future patient/record updates should occur on the other patient\n\nThis element is labeled as a modifier because when the patient record is marked as not active it is not expected to be used/referenced without being changed back to active.",
					"requirements": "Need to be able to mark a patient record as not to be used because it was created in error.",
					"min": 0,
					"max": "1",
					"type": [{
						"code": "boolean"
					}],
					"defaultValueBoolean": true,
					"isModifier": true,
					"isSummary": true,
					"mapping": [{
							"identity": "rim",
							"map": "statusCode"
						},
						{
							"identity": "cda",
							"map": "n/a"
						},
						{
							"identity": "w5",
							"map": "status"
						}
					]
				},
				{
					"id": "Sterilize.devName",
					"path": "Sterilize.devName",
					"short": "A name associated with the patient",
					"definition": "A name associated with the individual.",
					"comment": "A patient may have multiple names with different uses or applicable periods. For animals, the name is a \"HumanName\" in the sense that is assigned and used by humans and has the same patterns.",
					"requirements": "Need to be able to track the patient by multiple names. Examples are your official name and a partner name.",
					"min": 0,
					"max": "*",
					"type": [{
						"code": "string"
					}]						
					
				}					
				
			]
		}
}

My Input validation resource file:

{
  "resourceType": "Sterilize",  
  "id1": "example",  
  "meta": {
    "profile": [
      "http://hl7.org/fhir/StructureDefinition/Sterilize"
    ]
  },
  "text": {
    "status": "generated",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n\t\t\t<table>\n\t\t\t\t<tbody>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Name</td>\n\t\t\t\t\t\t<td>Peter James \n              <b>Chalmers</b> (&quot;Jim&quot;)\n            </td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Address</td>\n\t\t\t\t\t\t<td>534 Erewhon, Pleasantville, Vic, 3999</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Contacts</td>\n\t\t\t\t\t\t<td>Home: unknown. Work: (03) 5555 6473</td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td>Id</td>\n\t\t\t\t\t\t<td>MRN: 12345 (Acme Healthcare)</td>\n\t\t\t\t\t</tr>\n\t\t\t\t</tbody>\n\t\t\t</table>\n\t\t</div>"
  },  
  "active": "true",
  "devName": "needle"
}

The validator takes a parameter for the ‘context’ when starting up - this stores all the structure definitions, value sets, code systems etc for the validator to use. You need to do context.seeResource(yourStructureDefinition) before calling it, and then it should identify it as a resource

Note 1: I write the underlying validator, but I’m not sure how it’s packaged in HAPI, so I can’t tell you where to find the context

Note 2: HAPI supports custom resources, but the validator doesn’t really, and they’re not conformant from a FHIR perspective; so it’s possible that even if you do all the steps above, the validator still won’t validate as you expect, and that may or may not be something we’d fix

I want to validate fhir resource with custom profile. so, I created my own custom validation support class and load custom profile for the resources. But when I do validate, it is giving me an error like ‘This does not appear to be a FHIR resource (unknown namespace/name ‘http://hl7.org/fhir::Practitioner)

What are you trying to validate with? What does your profile look like?