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 6, 2025
1 parent 699dcd4 commit 25d52bc
Show file tree
Hide file tree
Showing 7 changed files with 673 additions and 1 deletion.
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
43 changes: 43 additions & 0 deletions app/services/schema_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "json-schema"
require "uri"

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
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 25d52bc

Please sign in to comment.