Sha256: 5380003ec48b7496140a53aca4ced18bd01a7509e8f21d316e5b28ceebb7b644

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'media_types/scheme/errors'

module MediaTypes
  class Scheme
    class MissingValidation

      def validate!(_output, options, context:, **_opts)
        # Check that no unknown keys are present
        return true unless options.strict
        raise_strict!(key: context.key, strict_keys: context.rules, backtrace: options.backtrace, found: options.scoped_output)
      end

      def raise_strict!(key:, backtrace:, strict_keys:, found:)
        raise StrictValidationError, format(
          "Unknown key %<key>s in data.\n" \
          "\tFound at: %<backtrace>s\n" \
          "\tExpected:\n\n" \
          "%<strict_keys>s\n\n" \
          "\tBut I Found:\n\n" \
          '%<found>s',
          key: key.inspect,
          backtrace: backtrace.join('->'),
          strict_keys: keys_to_str(strict_keys.keys),
          found: (found.respond_to? :keys) ? keys_to_str(found.keys) : found.class.name
        )
      end

      def inspect
        '((raise when strict))'
      end

      def keys_to_str(keys)
        converted = keys.map { |k| k.is_a?(Symbol) ? ":#{k}" : "'#{k}'" }
        "[#{converted.join ', '}]"
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
media_types-2.3.2 lib/media_types/scheme/missing_validation.rb
media_types-2.2.0 lib/media_types/scheme/missing_validation.rb
media_types-2.1.1 lib/media_types/scheme/missing_validation.rb
media_types-2.1.0 lib/media_types/scheme/missing_validation.rb
media_types-2.0.1 lib/media_types/scheme/missing_validation.rb