-
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.
Merge pull request #640 from sul-dlss/iss637-add-bcp47-language-tag-t…
…o-files add BCP 47 language tag attribute for files
- Loading branch information
Showing
10 changed files
with
183 additions
and
11 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ PATH | |
dry-types (~> 1.1) | ||
edtf | ||
equivalent-xml | ||
i18n | ||
jsonpath | ||
nokogiri | ||
openapi3_parser | ||
|
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
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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cocina | ||
module Models | ||
module Validators | ||
# Validates that a languageTag is valid according to RFC 4646, if one is present | ||
class LanguageTagValidator | ||
def self.validate(clazz, attributes) | ||
new(clazz, attributes).validate | ||
end | ||
|
||
def initialize(clazz, attributes) | ||
@clazz = clazz | ||
@attributes = attributes | ||
end | ||
|
||
def validate | ||
return unless meets_preconditions? | ||
|
||
return if valid_language_tag? | ||
|
||
raise ValidationError, 'The provided language tag is not valid according to RFC 4646: ' \ | ||
"#{attributes[:languageTag]}" | ||
end | ||
|
||
private | ||
|
||
attr_reader :clazz, :attributes | ||
|
||
def meets_preconditions? | ||
file? && attributes[:languageTag].present? | ||
end | ||
|
||
def file? | ||
(clazz::TYPES & File::TYPES).any? | ||
rescue NameError | ||
false | ||
end | ||
|
||
def valid_language_tag? | ||
parsed_tag = I18n::Locale::Tag::Rfc4646.tag(attributes[:languageTag]) | ||
|
||
parsed_tag.present? && parsed_tag.is_a?(I18n::Locale::Tag::Rfc4646) | ||
end | ||
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
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
117 changes: 117 additions & 0 deletions
117
spec/cocina/models/validators/language_tag_validator_spec.rb
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,117 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Cocina::Models::Validators::LanguageTagValidator do | ||
let(:validate) { described_class.validate(clazz, props) } | ||
|
||
let(:props) { file_props } | ||
|
||
let(:file_props) do | ||
{ | ||
externalIdentifier: 'bc123df4567_1', | ||
label: 'Page 1', | ||
type: Cocina::Models::ObjectType.file, | ||
version: 1, | ||
access: { view: 'dark', download: 'none' }, | ||
administrative: { | ||
publish: false, | ||
shelve: false, | ||
sdrPreserve: true | ||
}, | ||
hasMessageDigests: [], | ||
hasMimeType: 'text/plain', | ||
filename: 'page1.txt' | ||
} | ||
end | ||
|
||
context 'with no value for languageTag specified' do | ||
context 'with a File' do | ||
let(:clazz) { Cocina::Models::File } | ||
|
||
it 'does not raise' do | ||
expect { validate }.not_to raise_error | ||
end | ||
end | ||
|
||
context 'with a RequestFile' do | ||
let(:clazz) { Cocina::Models::RequestFile } | ||
|
||
it 'does not raise' do | ||
expect { validate }.not_to raise_error | ||
end | ||
end | ||
end | ||
|
||
context 'with RFC 4646 conformant value for languageTag specified' do | ||
let(:props) do | ||
file_props.dup.tap do |props| | ||
props[:languageTag] = language_tag | ||
end | ||
end | ||
|
||
context 'with a recognized language, script, and region' do | ||
let(:language_tag) { 'ru-Cyrl-RU' } | ||
|
||
context 'with a File' do | ||
let(:clazz) { Cocina::Models::File } | ||
|
||
it 'does not raise' do | ||
expect { validate }.not_to raise_error | ||
end | ||
end | ||
|
||
context 'with a RequestFile' do | ||
let(:clazz) { Cocina::Models::RequestFile } | ||
|
||
it 'does not raise' do | ||
expect { validate }.not_to raise_error | ||
end | ||
end | ||
end | ||
|
||
context 'with an unrecognized language, script, and region' do | ||
let(:language_tag) { 'foo-Barr-BZ' } # still conforms to the expected format of BCP 47/RFC 4646, should parse | ||
|
||
context 'with a File' do | ||
let(:clazz) { Cocina::Models::File } | ||
|
||
it 'does not raise' do | ||
expect { validate }.not_to raise_error | ||
end | ||
end | ||
|
||
context 'with a RequestFile' do | ||
let(:clazz) { Cocina::Models::RequestFile } | ||
|
||
it 'does not raise' do | ||
expect { validate }.not_to raise_error | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'with value for languageTag specified that does not conform to RFC 4646' do | ||
let(:props) do | ||
file_props.dup.tap do |props| | ||
props[:languageTag] = 'fooooooooooooo' | ||
end | ||
end | ||
|
||
context 'with a File' do | ||
let(:clazz) { Cocina::Models::File } | ||
|
||
it 'raises a validation error' do | ||
expect { validate }.to raise_error(Cocina::Models::ValidationError, 'The provided language tag is not valid according to RFC 4646: fooooooooooooo') | ||
end | ||
end | ||
|
||
context 'with a RequestFile' do | ||
let(:clazz) { Cocina::Models::RequestFile } | ||
|
||
it 'raises a validation error' do | ||
expect { validate }.to raise_error(Cocina::Models::ValidationError, 'The provided language tag is not valid according to RFC 4646: fooooooooooooo') | ||
end | ||
end | ||
end | ||
end |