Method: MediaTypes::Scheme#any

Defined in:
lib/media_types/scheme.rb

#any(scheme = nil, expected_type: ::Hash, allow_empty: false, &block) ⇒ Object

Allow for any key.

The +&block+ defines the Schema for each value.

Examples:

Add a collection named foo, expecting any key with a defined value


class MyMedia
  include MediaTypes::Dsl

  validations do
    collection :foo do
      any do
        attribute :bar, String
      end
    end
  end
end

MyMedia.valid?({ foo: [{ anything: { bar: 'my-string' }, other_thing: { bar: 'other-string' } }] })
# => true

Any key, but all of them String or Numeric


class MyMedia
  include MediaTypes::Dsl

  validations do
    any AnyOf(String, Numeric)
  end
end

MyMedia.valid?({ foo: 'my-string', bar: 42 })
# => true

Parameters:

  • scheme (Scheme, NilClass) (defaults to: nil)

    scheme to use if no &block is given

  • allow_empty (TrueClass, FalseClass) (defaults to: false)

    if true, empty (no key/value present) is allowed

  • expected_type (Class) (defaults to: ::Hash)

    forces the validated object to have this type

See Also:



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/media_types/scheme.rb', line 218

def any(scheme = nil, expected_type: ::Hash, allow_empty: false, &block)
  unless block_given?
    if scheme.is_a?(Scheme)
      return rules.default = scheme
    end

    return rules.default = Attribute.new(scheme)
  end

  rules.default = Scheme.new(allow_empty: allow_empty, expected_type: expected_type, &block)
end