github.com/kaptinlin/jsonschema@v0.4.6/id.go (about)

     1  package jsonschema
     2  
     3  // // validateID checks if the `$id` attribute in the schema conforms to URI standards and JSON Schema Draft 2020-12 specifications.
     4  // // According to the JSON Schema Draft 2020-12:
     5  // //   - `$id` is a URI that uniquely identifies the schema.
     6  // //   - It must be an absolute URI without a fragment.
     7  // //   - This URI serves both as an identifier and as a base URI for resolving relative references.
     8  // //
     9  // // This function ensures that the `$id` value is a well-formed URI and adheres to these requirements.
    10  // // If the `$id` value does not conform, it returns a EvaluationError detailing the specific issues.
    11  // //
    12  // // Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-the-id-keyword
    13  // func evaluateID(schema *Schema) *EvaluationError {
    14  // 	if schema.ID == "" {
    15  // 		return nil // No ID specified, nothing to validate
    16  // 	}
    17  
    18  // 	id := schema.ID
    19  // 	if !isValidURI(id) {
    20  // 		id = resolveRelativeURI(schema.baseURI, getCurrentPathSegment(id))
    21  // 	}
    22  
    23  // 	uri, err := url.Parse(id)
    24  // 	if err != nil {
    25  // 		// Invalid URI format
    26  // 		return NewEvaluationError("$id", "id_invalid", "Invalid `$id` URI: {error}", map[string]interface{}{
    27  // 			"error": err.Error(),
    28  // 		})
    29  // 	}
    30  
    31  // 	if !uri.IsAbs() {
    32  // 		// `$id` must be an absolute URI
    33  // 		return NewEvaluationError("$id", "id_not_absolute", "`$id` must be an absolute URI without a fragment.")
    34  // 	}
    35  
    36  // 	if uri.Fragment != "" {
    37  // 		// `$id` should not contain a fragment
    38  // 		return NewEvaluationError("$id", "id_contains_fragment", "`$id` must not contain a fragment.")
    39  // 	}
    40  
    41  // 	return nil
    42  // }