-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation of metadata on record save
- Loading branch information
1 parent
aa6145d
commit a8de821
Showing
13 changed files
with
121 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
module ApplicationHelper | ||
def valid_record_tag(record) | ||
text, colour = if record.json_valid? | ||
%w[Valid green] | ||
else | ||
%w[Invalid red] | ||
end | ||
content_tag :strong, text, class: "govuk-tag govuk-tag--#{colour}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class JsonValidator | ||
JSON_SCHEMA_PATH = "https://co-cddo.github.io/data-catalogue-metadata/schema/dataset_schema.json".freeze | ||
|
||
def self.valid?(json) | ||
new(json).valid? | ||
end | ||
|
||
attr_reader :json | ||
|
||
def initialize(json) | ||
@json = json | ||
end | ||
|
||
def valid? | ||
JSON::Validator.validate JSON_SCHEMA_PATH, json | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddJsonValidToRecords < ActiveRecord::Migration[8.0] | ||
def change | ||
add_column :records, :json_valid, :boolean | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"identifier": "8d085327-21b6-4d8b-9705-88faad231d22", | ||
"supplierIdentifier": "acme-123", | ||
"modified": "2023-12-09T16:09:53+00:00", | ||
"status": "Published", | ||
"title": "Advance Passenger Information", | ||
"description": "Travel data and personal data given to airlines by passenger. API covers both inbound and outbound air passengers. API includes the passenger’s full name, nationality, date of birth, gender and travel document number, type and country of issue.The data does not include those arriving by sea or rail routes, by private aircraft or via the Common Travel Area (CTA).", | ||
"type": "Data Set", | ||
"theme": [ | ||
"Transport and infrastructure", | ||
"Population and society" | ||
], | ||
"keyword": [ | ||
"Air travel", | ||
"Passport", | ||
"Airports", | ||
"leaving UK", | ||
"entering UK" | ||
], | ||
"contactPoint": [ | ||
{ | ||
"name": "Rob Nichols", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"publisher": "academy-for-social-justice", | ||
"securityClassification": "OFFICIAL", | ||
"accessRights": "INTERNAL", | ||
"distribution": [ | ||
{ | ||
"accessService": ["8d085327-21b6-4d8b-9705-88faad231d23"], | ||
"downloadURL": "http://example.com/path/to/file.csv", | ||
"mediaType":["text/csv"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Record, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
describe "metadata validated on save" do | ||
let(:record) { create :record, metadata: { invalid: :content } } | ||
|
||
it "sets json_valid as false if metadata doesn't match schema" do | ||
expect(record.json_valid).to be_falsey | ||
end | ||
|
||
context "with valid data" do | ||
let(:metadata) { json_from_fixture("dataset.json") } | ||
let(:record) { create :record, metadata: } | ||
|
||
it "sets json_valid as true" do | ||
expect(record.json_valid).to be_truthy | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe JsonValidator, type: :service do | ||
describe ".valid?" do | ||
let(:json) { json_from_fixture("dataset.json") } | ||
|
||
it "is true for valid json" do | ||
expect(described_class.valid?(json)).to be_truthy | ||
end | ||
|
||
context "with invalid json" do | ||
let(:json) { { invalid: :content }.to_json } | ||
|
||
it "is false" do | ||
expect(described_class.valid?(json)).not_to be_truthy | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def json_from_fixture(path) | ||
JSON.parse(File.read(fixture_file_upload(path))) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
require "webmock/rspec" | ||
|
||
# Allow calls to JSON schema | ||
WebMock.disable_net_connect!(allow: "https://co-cddo.github.io") |