Skip to content

Commit

Permalink
ACD-573: Add schema and sample for the new application detail endpoint
Browse files Browse the repository at this point in the history
Add new service to validate the response from the CP endpoint (for now court_application_details/all_fields.json) against the schema.
  • Loading branch information
alexdesi committed Feb 7, 2025
1 parent 699dcd4 commit 192f81c
Show file tree
Hide file tree
Showing 10 changed files with 1,022 additions and 398 deletions.
14 changes: 14 additions & 0 deletions app/controllers/api/internal/v2/court_applications_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Api
module Internal
module V2
class CourtApplicationsController < ApplicationController
def show
# TODO: this will be the new application details endpoint
raise NotImplementedError
end
end
end
end
end
49 changes: 49 additions & 0 deletions app/services/common_platform/api/schema_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "json-schema"
require "uri"

module CommonPlatform
module Api
class SchemaValidator < ApplicationService
def initialize(schema:, json_response:)
@schema = schema
@json_response = json_response

load_schema_references
end

def call
JSON::Validator.validate!(@schema, @json_response)
rescue JSON::Schema::ValidationError => e
Rails.logger.error("JSON Schema Validation Error: #{e.message}")
false
end

private

def load_schema_references
refs = [
{
filename: "lib/schemas/global/apiCourtsDefinitions.json",
url: "http://justice.gov.uk/core/courts/external/apiCourtsDefinitions.json",
},
{
filename: "lib/schemas/global/apiJudicialResult.json",
url: "http://justice.gov.uk/core/courts/external/apiJudicialResult.json",
},
{
filename: "lib/schemas/global/apiDelegatedPowers.json",
url: "http://justice.gov.uk/core/courts/external/apiDelegatedPowers.json",
},
]

refs.each do |ref|
json_schema = JSON::Schema.new(
JSON.parse(File.open(Rails.root.join(ref[:filename]).to_s).read),
Addressable::URI.parse(ref[:url]),
)
JSON::Validator.add_schema(json_schema)
end
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
end

api_version(module: "V2", path: { value: "v2" }) do
resources :court_applications, only: [:show]
resources :prosecution_cases, only: [:index], param: :reference do
resources :defendants, only: %i[show]
end
Expand Down
Loading

0 comments on commit 192f81c

Please sign in to comment.