Sha256: 96368479d129780b3ae246cb70caa45c50e50961999221b8626b12c11f4e6744

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

require_relative '../transform_keys'

module Jimmy
  class SchemaTypes::Object < SchemaType
    register!

    trait :require do |*required_keys|
      if required_keys == [0]
        attrs[:required] = SymbolArray.new
      else
        attrs[:required] ||= SymbolArray.new
        attrs[:required] |= required_keys.flatten.map(&:to_s).uniq
      end
    end

    trait :all do
      SymbolArray.new(attrs[:properties].keys.select { |x| x.is_a? Symbol })
    end

    trait(:none) { 0 }

    trait(:allow_additional) { attrs[:additional_properties] = true }

    nested do |schema, property_name|
      (attrs[:properties] ||= {})[TransformKeys.transformer.transform(property_name, domain.options[:transform_keys])] = schema
    end

    compile do |hash|
      (attrs[:properties] || {}).each do |key, value|
        collection, key =
            if key.is_a? Regexp
              ['patternProperties', key.inspect.gsub(%r`^/|/[a-z]*$`, '')]
            else
              ['properties', key.to_s]
            end
        hash[collection] ||= {}
        hash[collection][key] = value.compile
      end
      required = attrs[:required]
      hash['required'] = required.to_a if required && !required.empty?
      hash['additionalProperties'] = !!attrs[:additional_properties]
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jimmy-0.5.5 lib/jimmy/schema_types/object.rb
jimmy-0.5.4 lib/jimmy/schema_types/object.rb
jimmy-0.5.3 lib/jimmy/schema_types/object.rb
jimmy-0.5.2 lib/jimmy/schema_types/object.rb
jimmy-0.5.1 lib/jimmy/schema_types/object.rb
jimmy-0.5.0 lib/jimmy/schema_types/object.rb