class JsonWorld::PropertyDefinition

Attributes

property_name[R]

@return [Symbol]

Public Class Methods

new(property_name: nil, **options) click to toggle source

@param [Symbol, nil] property_name Note that unnamed property can be passed @param [Hash{Symbol => Object}] options

# File lib/json_world/property_definition.rb, line 10
def initialize(property_name: nil, **options)
  @options = options
  @property_name = property_name
end

Public Instance Methods

as_json_schema() click to toggle source

@return [Hash{Symbol => Object}]

# File lib/json_world/property_definition.rb, line 16
def as_json_schema
  if has_json_schema_compatible_type?
    if @options[:links]
      @options[:type].as_json_schema
    else
      @options[:type].as_json_schema_without_links
    end
  else
    {
      description: description,
      example: example,
      format: format_type,
      items: items_as_json_schema,
      pattern: pattern_in_string,
      properties: properties_as_json_schema,
      required: required_property_names,
      type: type,
      uniqueItems: unique_flag,
    }.reject do |_key, value|
      value.nil? || value.respond_to?(:empty?) && value.empty?
    end
  end
end
as_nested_json_schema() click to toggle source

@return [Hash{Symbol => Object}]

# File lib/json_world/property_definition.rb, line 41
def as_nested_json_schema
  { property_name => as_json_schema }
end
optional?() click to toggle source

@return [false, true] True if explicitly this property is defined as optional

# File lib/json_world/property_definition.rb, line 46
def optional?
  !!@options[:optional]
end
raw_options() click to toggle source

@return [Hash{Symbol => Object}]

# File lib/json_world/property_definition.rb, line 51
def raw_options
  @options.merge(
    property_name: @property_name,
  )
end

Private Instance Methods

description() click to toggle source

@return [String, nil]

# File lib/json_world/property_definition.rb, line 60
def description
  @options[:description]
end
example() click to toggle source

@return [Object]

# File lib/json_world/property_definition.rb, line 65
def example
  @options[:example]
end
format_type() click to toggle source

@note format is preserved by Kernel.#format ;( @return [String, nil]

# File lib/json_world/property_definition.rb, line 71
def format_type
  if types.include?(Time)
    "date-time"
  end
end
has_json_schema_compatible_type?() click to toggle source

@return [false, true]

# File lib/json_world/property_definition.rb, line 78
def has_json_schema_compatible_type?
  @options[:type].respond_to?(:as_json_schema)
end
items_as_json_schema() click to toggle source

@note Tuple validation is not supported yet @return [Array<Hash>, nil]

# File lib/json_world/property_definition.rb, line 84
def items_as_json_schema
  if @options[:items]
    JsonWorld::PropertyDefinition.new(@options[:items]).as_json_schema
  end
end
pattern() click to toggle source

@note pattern can be used only when type is String or not specified @return [Regexp, nil]

# File lib/json_world/property_definition.rb, line 92
def pattern
  @options[:pattern]
end
pattern_in_string() click to toggle source

@return [String, nil]

# File lib/json_world/property_definition.rb, line 97
def pattern_in_string
  if pattern
    pattern.inspect[1..-2]
  end
end
properties_as_json_schema() click to toggle source

@return [Hash, nil]

# File lib/json_world/property_definition.rb, line 104
def properties_as_json_schema
  if @options[:properties]
    property_definitions.map(&:as_nested_json_schema).inject({}, :merge)
  end
end
property_definitions() click to toggle source

@return [Array, nil]

# File lib/json_world/property_definition.rb, line 111
def property_definitions
  if @options[:properties]
    @options[:properties].map do |property_name, options|
      JsonWorld::PropertyDefinition.new(
        options.merge(
          property_name: property_name,
        )
      )
    end
  end
end
required_property_names() click to toggle source

@return [Array<Symbol>, nil]

# File lib/json_world/property_definition.rb, line 124
def required_property_names
  if @options[:properties]
    property_definitions.reject(&:optional?).map(&:property_name)
  end
end
type() click to toggle source

@return [Array<String>, String, nil]

# File lib/json_world/property_definition.rb, line 131
def type
  strings = types.map do |this_type|
    case
    when this_type == Array
      "array"
    when this_type == FalseClass || this_type == TrueClass
      "boolean"
    when this_type == Float
      "float"
    when this_type == Hash
      "object"
    when this_type == Integer
      "integer"
    when this_type == NilClass
      "null"
    when this_type == String || this_type == Time
      "string"
    when this_type.is_a?(String)
      this_type
    end
  end.compact.uniq
  strings.length >= 2 ? strings : strings.first
end
types() click to toggle source

@note type can be an Array @return [Array<String>]

# File lib/json_world/property_definition.rb, line 157
def types
  Array(@options[:type])
end
unique_flag() click to toggle source

@return [false, nil, true]

# File lib/json_world/property_definition.rb, line 162
def unique_flag
  if @options.key?(:unique)
    !!@options[:unique]
  end
end