class Hoodoo::Presenters::String

A JSON String schema member.

Attributes

length[RW]

The maximum length of the String.

Public Class Methods

new( name, options = {} ) click to toggle source

Initialize a String instance with the appropriate name and options.

name

The JSON key.

options

A Hash of options, e.g. :required => true, :length => 10.

Calls superclass method Hoodoo::Presenters::Field::new
# File lib/hoodoo/presenters/types/string.rb, line 17
def initialize( name, options = {} )
  super( name, options )

  unless options.has_key?( :length )
    raise ArgumentError.new( 'Hoodoo::Presenters::String must have a :length' )
  end

  @length = options[ :length ]
end

Public Instance Methods

validate( data, path = '' ) click to toggle source

Check if data is a valid String and return a Hoodoo::Errors instance.

Calls superclass method Hoodoo::Presenters::Field#validate
# File lib/hoodoo/presenters/types/string.rb, line 29
def validate( data, path = '' )
  errors = super( data, path )
  return errors if errors.has_errors? || ( ! @required && data.nil? )

  if data.is_a?( ::String )
    if data.size > @length
      errors.add_error(
        'generic.invalid_string',
        :message   => "Field `#{ full_path( path ) }` is longer than maximum length `#{ @length }`",
        :reference => { :field_name => full_path( path ) }
      )
    end
  else
    errors.add_error(
      'generic.invalid_string',
      :message   => "Field `#{ full_path( path ) }` is an invalid string",
      :reference => { :field_name => full_path( path ) }
    )
  end

  errors
end