Class: Hoodoo::Presenters::UUID

Inherits:
Field
  • Object
show all
Defined in:
lib/hoodoo/presenters/types/uuid.rb

Overview

A JSON UUID schema member.

Instance Attribute Summary collapse

Attributes inherited from Field

#default, #name, #required

Instance Method Summary collapse

Methods inherited from Field

#full_path, #has_default?, #render, #walk

Constructor Details

#initialize(name, options = {}) ⇒ UUID

Initialize a UUID instance with the appropriate name and options.

name

The JSON key.

options

A Hash of options, e.g. :resource => :Product, :required => true.



31
32
33
34
# File 'lib/hoodoo/presenters/types/uuid.rb', line 31

def initialize( name, options = {} )
  @resource = options.delete( :resource )
  super name, options
end

Instance Attribute Details

#resourceObject

The optional associated resource kind, as a symbol (e.g. ‘:Product’).



24
25
26
# File 'lib/hoodoo/presenters/types/uuid.rb', line 24

def resource
  @resource
end

Instance Method Details

#validate(data, path = '') ⇒ Object

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hoodoo/presenters/types/uuid.rb', line 38

def validate( data, path = '' )
  errors = super( data, path )
  return errors if errors.has_errors? || ( ! @required && data.nil? )

  unless Hoodoo::UUID.valid?( data )
    errors.add_error(
      'generic.invalid_uuid',
      :message   => "Field `#{ full_path( path ) }` is an invalid UUID",
      :reference => { :field_name => full_path( path ) }
    )
  end

  errors
end