class GraphQL::Schema::Validator::FormatValidator
# It’s pretty hard to come up with a legitimate use case for ‘without:`
validates: { format: { without: /A/ } }
argument :word_that_doesnt_begin_with_a_vowel, String, required: true,
@example reject inputs that match a pattern
validates: { format: { with: /A+Z/ } }
argument :handle, String, required: true,
@example requiring input to match a pattern
Use this to assert that string values match (or don’t match) the given RegExp.
def initialize(
-
message
(String
) -- -
without
(Regexp, nil
) -- -
with
(RegExp, nil
) --
def initialize( with: nil, without: nil, message: "%{validated} is invalid", **default_options ) @with_pattern = with @without_pattern = without @message = message super(**default_options) end
def validate(_object, _context, value)
def validate(_object, _context, value) if permitted_empty_value?(value) # Do nothing elsif value.nil? || (@with_pattern && !value.match?(@with_pattern)) || (@without_pattern && value.match?(@without_pattern)) @message end end