Skip to content

Commit acde7cc

Browse files
authored
Merge pull request #24 from mikoto2000/add-isbn-validation
Fix ISBN validation to BookMaster model
2 parents 17d896e + 5854823 commit acde7cc

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,5 @@ gem "rubocop-i18n", group: "development"
6868
gem "rubocop-thread_safety", group: "development"
6969
gem "erb_lint", group: "development"
7070
gem "date_validator"
71+
72+
gem "isbn_validation", "~> 1.2"

Gemfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ GEM
131131
irb (1.14.0)
132132
rdoc (>= 4.0.0)
133133
reline (>= 0.4.2)
134+
isbn_validation (1.2.2)
135+
activerecord (>= 3)
134136
jbuilder (2.12.0)
135137
actionview (>= 5.0.0)
136138
activesupport (>= 5.0.0)
@@ -329,6 +331,7 @@ DEPENDENCIES
329331
debug
330332
erb_lint
331333
importmap-rails
334+
isbn_validation
332335
jbuilder
333336
pagy
334337
pg (~> 1.1)
@@ -351,4 +354,4 @@ DEPENDENCIES
351354
web-console
352355

353356
BUNDLED WITH
354-
2.5.11
357+
2.5.16

app/models/book_master.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ def self.ransackable_associations(_auth_object = nil)
99
%w[authors book_author_relationship ndc_category]
1010
end
1111

12-
# TODO: ちゃんとした判定ロジックを作る
13-
VALID_ISBN_PATTERN = /[0-9\-]+/
14-
15-
validates :isbn, presence: true, uniqueness: true, format: { with: VALID_ISBN_PATTERN }
12+
validates :isbn, presence: true, uniqueness: true, isbn_format: true
1613
validates :title, presence: true
1714
validates :publication_date, presence: true
1815
validates :ndc_category, presence: true

test/models/book_master_test.rb

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,68 @@
11
require "test_helper"
22

33
class BookMasterTest < ActiveSupport::TestCase
4-
# test "the truth" do
5-
# assert true
6-
# end
4+
test "valid ISBN-10" do
5+
book = BookMaster.new(isbn: "0306406152", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
6+
assert book.valid?
7+
end
8+
9+
test "invalid ISBN-10" do
10+
book = BookMaster.new(isbn: "0306406153", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
11+
assert_not book.valid?
12+
end
13+
14+
test "valid ISBN-13" do
15+
book = BookMaster.new(isbn: "9780306406157", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
16+
assert book.valid?
17+
end
18+
19+
test "invalid ISBN-13" do
20+
book = BookMaster.new(isbn: "9780306406158", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
21+
assert_not book.valid?
22+
end
23+
24+
test "ISBN-10 with X as checksum" do
25+
book = BookMaster.new(isbn: "048665088X", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
26+
assert book.valid?
27+
end
28+
29+
test "ISBN-10 with invalid characters" do
30+
book = BookMaster.new(isbn: "03064O6152", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
31+
assert_not book.valid?
32+
end
33+
34+
test "ISBN-13 with invalid characters" do
35+
book = BookMaster.new(isbn: "978O306406157", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
36+
assert_not book.valid?
37+
end
38+
39+
test "ISBN-10 with incorrect length" do
40+
book = BookMaster.new(isbn: "030640615", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
41+
assert_not book.valid?
42+
end
43+
44+
test "ISBN-13 with incorrect length" do
45+
book = BookMaster.new(isbn: "978030640615", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
46+
assert_not book.valid?
47+
end
48+
49+
test "valid hyphenated ISBN-10" do
50+
book = BookMaster.new(isbn: "0-306-40615-2", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
51+
assert book.valid?
52+
end
53+
54+
test "invalid hyphenated ISBN-10" do
55+
book = BookMaster.new(isbn: "0-306-40615-3", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
56+
assert_not book.valid?
57+
end
58+
59+
test "valid hyphenated ISBN-13" do
60+
book = BookMaster.new(isbn: "978-0-306-40615-7", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
61+
assert book.valid?
62+
end
63+
64+
test "invalid hyphenated ISBN-13" do
65+
book = BookMaster.new(isbn: "978-0-306-40615-8", title: "Test Book", publication_date: Date.today, ndc_category: ndc_categories(:one))
66+
assert_not book.valid?
67+
end
768
end

0 commit comments

Comments
 (0)