Class: JSON::Schema::LimitAttribute

Inherits:
Attribute
  • Object
show all
Defined in:
lib/json-schema/attributes/limit.rb

Constant Summary

Constants inherited from Attribute

Attribute::TYPE_CLASS_MAPPINGS

Class Method Summary collapse

Methods inherited from Attribute

build_fragment, data_valid_for_type?, type_of_data, validation_error, validation_errors

Class Method Details

.acceptable_typeObject

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/json-schema/attributes/limit.rb', line 39

def self.acceptable_type
  raise NotImplementedError
end

.error_message(schema) ⇒ Object

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/json-schema/attributes/limit.rb', line 43

def self.error_message(schema)
  raise NotImplementedError
end

.exclusive?(schema) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/json-schema/attributes/limit.rb', line 31

def self.exclusive?(schema)
  false
end

.invalid?(schema, data) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
# File 'lib/json-schema/attributes/limit.rb', line 16

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

.limit(schema) ⇒ Object



27
28
29
# File 'lib/json-schema/attributes/limit.rb', line 27

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

.limit_nameObject

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/json-schema/attributes/limit.rb', line 47

def self.limit_name
  raise NotImplementedError
end

.validate(current_schema, data, fragments, processor, validator, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/json-schema/attributes/limit.rb', line 6

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

.value(data) ⇒ Object



35
36
37
# File 'lib/json-schema/attributes/limit.rb', line 35

def self.value(data)
  data
end