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 =
/\Asha256:[0-9a-f]{64}\z/
Instance Method Summary collapse
- #array(value, path) ⇒ Array[json_value]
- #boolean(value, path) ⇒ Boolean
- #digest(value, path) ⇒ String
- #enum(value, path, values) ⇒ void
- #integer(value, path, minimum: nil) ⇒ Integer
- #invalid(path, message) ⇒ bot
- #nullable_integer(value, path, minimum: nil) ⇒ Integer?
- #nullable_string(value, path) ⇒ String?
- #record(value, path, keys) ⇒ Hash[String, json_value]
- #sorted_unique!(values, path) ⇒ void
- #string(value, path, allow_empty: false) ⇒ String
Instance Method Details
#array(value, path) ⇒ 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
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
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.
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
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
81 82 83 |
# File 'lib/ibex/table_artifact/validator/support.rb', line 81 def invalid(path, ) raise ValidationError, "#{path} #{}" end |
#nullable_integer(value, path, minimum: nil) ⇒ Integer?
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?
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]
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.
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
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 |