test/unit/schemacop/v3/string_node_test.rb in schemacop-3.0.9 vs test/unit/schemacop/v3/string_node_test.rb in schemacop-3.0.10
- old
+ new
@@ -1,11 +1,13 @@
require 'test_helper'
module Schemacop
module V3
class StringNodeTest < V3Test
- EXP_INVALID_TYPE = 'Invalid type, expected "string".'.freeze
+ def self.invalid_type_error(type)
+ "Invalid type, got type \"#{type}\", expected \"string\"."
+ end
def test_basic
schema :string
assert_validation 'Hello World'
assert_validation ''
@@ -31,23 +33,23 @@
schema :string
assert_json(type: :string)
assert_validation 42 do
- error '/', EXP_INVALID_TYPE
+ error '/', StringNodeTest.invalid_type_error(Integer)
end
schema { str! :name }
assert_json(type: :object, properties: { name: { type: :string } }, required: %i[name], additionalProperties: false)
assert_validation name: :foo do
- error '/name', EXP_INVALID_TYPE
+ error '/name', StringNodeTest.invalid_type_error(Symbol)
end
assert_validation name: 234 do
- error '/name', EXP_INVALID_TYPE
+ error '/name', StringNodeTest.invalid_type_error(Integer)
end
end
def test_min_length
schema :string, min_length: 5
@@ -188,11 +190,11 @@
assert_validation 'foobar'
assert_validation ''
assert_validation 234 do
- error '/', 'Invalid type, expected "string".'
+ error '/', StringNodeTest.invalid_type_error(Integer)
end
assert_cast(nil, nil)
assert_cast('foobar', :foobar)
assert_cast('039n23$g- sfk3/', :'039n23$g- sfk3/')
@@ -216,11 +218,11 @@
end
# Integer value 42 is in the enum of allowed values, but it's not a string,
# so the validation still fails
assert_validation 42 do
- error '/', 'Invalid type, expected "string".'
+ error '/', StringNodeTest.invalid_type_error(Integer)
end
end
def test_boolean_casting
schema :string, format: :boolean
@@ -276,11 +278,11 @@
)
assert_validation(nil)
assert_validation('Foo')
assert_validation(5) do
- error '/', 'Invalid type, expected "string".'
+ error '/', StringNodeTest.invalid_type_error(Integer)
end
assert_cast('Foo', 'Foo')
assert_cast(nil, 'Hello')
end
@@ -295,11 +297,11 @@
)
assert_validation(nil)
assert_validation('123')
assert_validation(5) do
- error '/', 'Invalid type, expected "string".'
+ error '/', StringNodeTest.invalid_type_error(Integer)
end
assert_cast('123', 123)
assert_cast(nil, 42)
end
@@ -362,16 +364,16 @@
assert_validation('foo')
# Even we put those types in the enum, they need to fail the validations,
# as they are not strings
assert_validation(1) do
- error '/', 'Invalid type, expected "string".'
+ error '/', StringNodeTest.invalid_type_error(Integer)
end
assert_validation(:bar) do
- error '/', 'Invalid type, expected "string".'
+ error '/', StringNodeTest.invalid_type_error(Symbol)
end
assert_validation({ qux: 42 }) do
- error '/', 'Invalid type, expected "string".'
+ error '/', StringNodeTest.invalid_type_error(Hash)
end
# These need to fail validation, as they are not in the enum list
assert_validation('bar') do
error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'