Skip to content

Commit fb11113

Browse files
committed
Return original license value as-is from normalization if no match
1 parent fd8ed52 commit fb11113

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

app/validators/license_validator.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class LicenseValidator < ActiveModel::EachValidator
22

33
def validate_each(record, attribute, value)
4-
return if Seek::License.find(value)
5-
# Try looking up by URI
6-
license = Seek::License.normalize(license)
7-
if license
8-
record.send("#{attribute}=", license)
4+
normalized_license = Seek::License.normalize(value)
5+
if Seek::License.find(normalized_license)
6+
record.send("#{attribute}=", normalized_license)
97
return
108
end
119
record.errors.add(attribute, options[:message] || "isn't a recognized license")

lib/seek/license.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ def self.normalize(license)
5252
return nil if license.blank?
5353
license = license.strip
5454
if license.start_with?(/https?:/)
55-
license = uri_to_id(license)
55+
uri_to_id(license) || license
5656
else
57-
license = normalized_id_map[license.downcase]
57+
normalized_id_map[license.downcase] || license
5858
end
59-
license
6059
end
6160

6261
def is_null_license?

test/unit/license_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ class LicenseTest < ActiveSupport::TestCase
180180
assert_equal 'CC-BY-4.0', Seek::License.normalize('cc-by-4.0')
181181
assert_equal 'CC-BY-4.0', Seek::License.normalize('cc-By-4.0')
182182
assert_equal 'CC-BY-4.0', Seek::License.normalize(" cc-By-4.0\n ")
183-
assert_nil Seek::License.normalize('huh what')
183+
assert_equal 'HUH what', Seek::License.normalize('HUH what'), 'Should not modify unrecognized licenses'
184+
assert_equal 'huh what', Seek::License.normalize('huh what'), 'Should not modify unrecognized licenses'
185+
assert_equal 'http://cool-license.golf', Seek::License.normalize('http://cool-license.golf'), 'Should not modify unrecognized licenses'
184186
assert_nil Seek::License.normalize(nil)
185187
assert_nil Seek::License.normalize('')
186188
end

0 commit comments

Comments
 (0)