Class: RegexpValidation

Inherits:
Validation show all
Defined in:
lib/json_patterns.rb

Constant Summary

Constants included from Inspectable

Inspectable::INSPECTING_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Validation

#as_object_members, #expects_an_object?, memoized_new_from_pattern, new_from_pattern, #shallow_describe, #shallow_match?, #validate_from_root

Methods included from HashInitialized

#initialize

Methods included from DeepEquality

#==

Methods included from Inspectable

#inspect

Instance Attribute Details

#regexpObject

Returns the value of attribute regexp.



959
960
961
# File 'lib/json_patterns.rb', line 959

def regexp
  @regexp
end

Instance Method Details

#to_sObject



977
978
979
# File 'lib/json_patterns.rb', line 977

def to_s
  @regexp.inspect
end

#validate(path, data) ⇒ Object



961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File 'lib/json_patterns.rb', line 961

def validate(path, data)
  if data.is_a? String
    if data =~ regexp
      return []
    else
      return [ValidationUnexpected.new(
        path: path,
        expected: "string matching #{@regexp.inspect}",
        found: data.inspect,
      )]
    end
  else
    return [ValidationUnexpected.new(path: path, expected: 'string', found: json_type_name(data))]
  end
end