1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
# frozen_string_literal: true
require "spec_helper"
require_relative "./validator_helpers"
describe GraphQL::Schema::Validator::AllowNullValidator do
include ValidatorHelpers
it "allows nil when permitted" do
schema = build_schema(String, {length: { minimum: 5 }, allow_null: true})
result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil })
assert_nil result["data"]["validated"]
refute result.key?("errors")
end
it "rejects null by default" do
schema = build_schema(String, {length: { minimum: 5 }})
result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil })
assert_nil result["data"]["validated"]
assert_equal ["value is too short (minimum is 5)"], result["errors"].map { |e| e["message"] }
end
it "can be used standalone" do
schema = build_schema(String, { allow_null: false })
result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil })
assert_nil result["data"]["validated"]
assert_equal ["value can't be null"], result["errors"].map { |e| e["message"] }
end
it "allows nil when no validations are configured" do
schema = build_schema(String, {})
result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil })
assert_nil result["data"]["validated"]
refute result.key?("errors")
result = schema.execute("query { validated }")
assert_nil result["data"]["validated"]
refute result.key?("errors")
end
end
|