Module: SimpleJSONSchema::Checker

Defined in:
lib/simple_json_schema/checker.rb

Constant Summary collapse

JSON_POINTER_REGEX_STRING =
'(\/([^~\/]|~[01])*)*'
JSON_POINTER_REGEX =
/\A#{JSON_POINTER_REGEX_STRING}\z/.freeze
RELATIVE_JSON_POINTER_REGEX =
/\A(0|[1-9]\d*)(#|#{JSON_POINTER_REGEX_STRING})?\z/.freeze

Class Method Summary collapse

Class Method Details

.at_size(scope, check, operation) ⇒ Object



15
16
17
18
# File 'lib/simple_json_schema/checker.rb', line 15

def at_size(scope, check, operation)
  over = scope[check]
  scope.error(check, count: over) if over && scope.value&.size&.public_send(operation, over)
end

.at_value(scope, check, operation) ⇒ Object



10
11
12
13
# File 'lib/simple_json_schema/checker.rb', line 10

def at_value(scope, check, operation)
  over = scope[check]
  scope.error(check, count: over) if over && scope.value&.public_send(operation, over)
end

.const(scope) ⇒ Object



38
39
40
41
42
# File 'lib/simple_json_schema/checker.rb', line 38

def const(scope)
  return if scope.segment.is_a?(Array)

  scope.error(:const) if scope.key?(:const) && scope[:const] != scope.value
end

.enum(scope) ⇒ Object



33
34
35
36
# File 'lib/simple_json_schema/checker.rb', line 33

def enum(scope)
  enum = scope[:enum]
  scope.error(:enum) if enum && !enum.include?(scope.value)
end

.json_pointer?(value) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/simple_json_schema/checker.rb', line 44

def json_pointer?(value)
  JSON_POINTER_REGEX.match?(value)
end

.relative_json_pointer?(value) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/simple_json_schema/checker.rb', line 48

def relative_json_pointer?(value)
  RELATIVE_JSON_POINTER_REGEX.match?(value)
end

.required_keys(scope) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/simple_json_schema/checker.rb', line 20

def required_keys(scope)
  required = scope[:required]
  return unless required.is_a?(Array)

  missing_keys = required - scope.value.keys
  scope.error(:required, missing_keys: missing_keys) if missing_keys.any?
end

.unique_items(scope) ⇒ Object



28
29
30
31
# File 'lib/simple_json_schema/checker.rb', line 28

def unique_items(scope)
  value = scope.value
  scope.error(:uniqueItems) if scope[:uniqueItems] && value.size != value.uniq.size
end