Class: HashValidator::Validator::JsonValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/hash_validator/validators/json_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#should_validate?, #validate

Constructor Details

#initializeJsonValidator

Returns a new instance of JsonValidator.



4
5
6
# File 'lib/hash_validator/validators/json_validator.rb', line 4

def initialize
  super('json')  # The name of the validator
end

Instance Method Details

#error_messageObject



8
9
10
# File 'lib/hash_validator/validators/json_validator.rb', line 8

def error_message
  'is not valid JSON'
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/hash_validator/validators/json_validator.rb', line 12

def valid?(value)
  return false unless value.is_a?(String)
  JSON.parse(value)
  true
rescue JSON::ParserError
  false
end