Sha256: 34d4672f4ceb106a8805eed68e8345a50a948e3730479b6b34ca4faa204cdee2

Contents?: true

Size: 1.27 KB

Versions: 15

Compression:

Stored size: 1.27 KB

Contents

require 'json-schema/attribute'

module JSON
  class Schema
    class LimitAttribute < Attribute
      def self.validate(current_schema, data, fragments, processor, validator, options = {})
        schema = current_schema.schema
        return unless data.is_a?(acceptable_type) && invalid?(schema, value(data))

        property    = build_fragment(fragments)
        description = error_message(schema)
        message = format("The property '%s' %s", property, description)
        validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
      end

      def self.invalid?(schema, data)
        exclusive = exclusive?(schema)
        limit = limit(schema)

        if limit_name.start_with?('max')
          exclusive ? data >= limit : data > limit
        else
          exclusive ? data <= limit : data < limit
        end
      end

      def self.limit(schema)
        schema[limit_name]
      end

      def self.exclusive?(schema)
        false
      end

      def self.value(data)
        data
      end

      def self.acceptable_type
        raise NotImplementedError
      end

      def self.error_message(schema)
        raise NotImplementedError
      end

      def self.limit_name
        raise NotImplementedError
      end
    end
  end
end

Version data entries

15 entries across 14 versions & 3 rubygems

Version Path
json-schema-5.1.0 lib/json-schema/attributes/limit.rb
json-schema-ouidou-2.9.1 lib/json-schema/attributes/limit.rb
json-schema-ouidou-2.9.0 lib/json-schema/attributes/limit.rb
json-schema-5.0.1 lib/json-schema/attributes/limit.rb
json-schema-5.0.0 lib/json-schema/attributes/limit.rb
json-schema-4.3.1 lib/json-schema/attributes/limit.rb
json-schema-4.3.0 lib/json-schema/attributes/limit.rb
json-schema-4.2.0 lib/json-schema/attributes/limit.rb
json-schema-4.1.1 lib/json-schema/attributes/limit.rb
json-schema-4.1.0 lib/json-schema/attributes/limit.rb
json-schema-4.0.0 lib/json-schema/attributes/limit.rb
json-schema-3.0.0 lib/json-schema/attributes/limit.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/bundler/gems/json-schema-2253a5ee6679/lib/json-schema/attributes/limit.rb
mountapi-0.11.1 vendor/json-schema/lib/json-schema/attributes/limit.rb
json-schema-2.8.1 lib/json-schema/attributes/limit.rb