Module: Ibex::TableArtifact::ValidationSupport

Included in:
MetadataValidator, TableValidator, Validator
Defined in:
lib/ibex/table_artifact/validator/support.rb,
sig/ibex/table_artifact/validator/support.rbs

Constant Summary collapse

DIGEST =

RBS:

  • type json_value = String | Integer | Float | bool | nil | Array[json_value] | Hash[String, json_value]

Returns:

  • (Regexp)
/\Asha256:[0-9a-f]{64}\z/

Instance Method Summary collapse

Instance Method Details

#array(value, path) ⇒ Array[json_value]

RBS:

  • (json_value value, String path) -> Array[json_value]

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (Array[json_value])


26
27
28
29
# File 'lib/ibex/table_artifact/validator/support.rb', line 26

def array(value, path)
  invalid(path, "must be an array") unless value.is_a?(Array)
  value #: Array[json_value]
end

#boolean(value, path) ⇒ Boolean

RBS:

  • (json_value value, String path) -> bool

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/ibex/table_artifact/validator/support.rb', line 46

def boolean(value, path)
  invalid(path, "must be true or false") unless [true, false].include?(value)
  value #: bool
end

#digest(value, path) ⇒ String

RBS:

  • (json_value value, String path) -> String

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (String)


58
59
60
61
# File 'lib/ibex/table_artifact/validator/support.rb', line 58

def digest(value, path)
  invalid(path, "must be a sha256 digest") unless value.is_a?(String) && DIGEST.match?(value)
  value
end

#enum(value, path, values) ⇒ void

This method returns an undefined value.

RBS:

  • [T] (T value, String path, Array[T] values) -> T



52
53
54
55
# File 'lib/ibex/table_artifact/validator/support.rb', line 52

def enum(value, path, values)
  invalid(path, "must be one of #{values.join(', ')}") unless values.include?(value)
  value
end

#integer(value, path, minimum: nil) ⇒ Integer

RBS:

  • (json_value value, String path, ?minimum: Integer?) -> Integer

Parameters:

  • value (json_value)
  • path (String)
  • minimum: (Integer, nil) (defaults to: nil)

Returns:

  • (Integer)


39
40
41
42
43
# File 'lib/ibex/table_artifact/validator/support.rb', line 39

def integer(value, path, minimum: nil)
  invalid(path, "must be an integer") unless value.is_a?(Integer)
  invalid(path, "must be at least #{minimum}") if minimum && value < minimum
  value #: Integer
end

#invalid(path, message) ⇒ bot

RBS:

  • (String path, String message) -> bot

Parameters:

  • path (String)
  • message (String)

Returns:

  • (bot)


81
82
83
# File 'lib/ibex/table_artifact/validator/support.rb', line 81

def invalid(path, message)
  raise ValidationError, "#{path} #{message}"
end

#nullable_integer(value, path, minimum: nil) ⇒ Integer?

RBS:

  • (json_value value, String path, ?minimum: Integer?) -> Integer?

Parameters:

  • value (json_value)
  • path (String)
  • minimum: (Integer, nil) (defaults to: nil)

Returns:

  • (Integer, nil)


70
71
72
73
# File 'lib/ibex/table_artifact/validator/support.rb', line 70

def nullable_integer(value, path, minimum: nil)
  integer(value, path, minimum: minimum) unless value.nil?
  value #: Integer?
end

#nullable_string(value, path) ⇒ String?

RBS:

  • (json_value value, String path) -> String?

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (String, nil)


64
65
66
67
# File 'lib/ibex/table_artifact/validator/support.rb', line 64

def nullable_string(value, path)
  string(value, path, allow_empty: true) unless value.nil?
  value #: String?
end

#record(value, path, keys) ⇒ Hash[String, json_value]

RBS:

  • (json_value value, String path, Array[String] keys) -> Hash[String, json_value]

Parameters:

  • value (json_value)
  • path (String)
  • keys (Array[String])

Returns:

  • (Hash[String, json_value])


14
15
16
17
18
19
20
21
22
23
# File 'lib/ibex/table_artifact/validator/support.rb', line 14

def record(value, path, keys)
  invalid(path, "must be an object") unless value.is_a?(Hash)
  invalid(path, "must use string keys") unless value.keys.all?(String)

  missing = keys - value.keys
  extra = value.keys - keys
  invalid(path, "is missing #{missing.join(', ')}") unless missing.empty?
  invalid(path, "contains unknown fields #{extra.join(', ')}") unless extra.empty?
  value #: Hash[String, json_value]
end

#sorted_unique!(values, path) ⇒ void

This method returns an undefined value.

RBS:

  • (Array[json_value] values, String path) -> void

Parameters:

  • values (Array[json_value])
  • path (String)


76
77
78
# File 'lib/ibex/table_artifact/validator/support.rb', line 76

def sorted_unique!(values, path)
  invalid(path, "must be sorted and unique") unless values == values.sort.uniq
end

#string(value, path, allow_empty: false) ⇒ String

RBS:

  • (json_value value, String path, ?allow_empty: bool) -> String

Parameters:

  • value (json_value)
  • path (String)
  • allow_empty: (Boolean) (defaults to: false)

Returns:

  • (String)


32
33
34
35
36
# File 'lib/ibex/table_artifact/validator/support.rb', line 32

def string(value, path, allow_empty: false)
  invalid(path, "must be a string") unless value.is_a?(String)
  invalid(path, "must not be empty") if !allow_empty && value.empty?
  value #: String
end