A JSON String schema member.
Methods
Attributes
[RW] | length | The maximum length of the String. |
Class Public methods
new( name, options = {} )
Link
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.
Source: show
# 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
Instance Public methods
validate( data, path = '' )
Link
Check if data is a valid String and return a Hoodoo::Errors instance.
Source: show
# 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