Class: SBOM::CycloneDX::Validator::UnionValidator

Inherits:
BaseValidator
  • Object
show all
Defined in:
lib/sbom/cyclone_dx/validator/union_validator.rb

Constant Summary

Constants inherited from BaseValidator

BaseValidator::INVALID_TYPE, BaseValidator::MISSING_REQUIRED

Instance Method Summary collapse

Methods inherited from BaseValidator

#required?, #valid?

Constructor Details

#initialize(of:, required: false) ⇒ UnionValidator

Returns a new instance of UnionValidator.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sbom/cyclone_dx/validator/union_validator.rb', line 11

def initialize(of:, required: false)
  super(required: required)

  @nested_validators = []

  of.each do |union_item|
    (klass, validator_params) = union_item.is_a?(Array) ? union_item : [union_item, {}]

    @nested_validators << Validator.for(klass, required: required, **validator_params)
  end
end

Instance Method Details

#raw_typesObject



33
34
35
# File 'lib/sbom/cyclone_dx/validator/union_validator.rb', line 33

def raw_types
  @nested_validators.flat_map(&:raw_types)
end

#validate(value) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/sbom/cyclone_dx/validator/union_validator.rb', line 23

def validate(value)
  # TODO: Build message based on type and params, e.g.
  # "Expected one of: [String, Integer], got: Float"
  # "Expected one of: [String with length <= 2, Integer with maximum 99], got: String with length 3"
  rv = @nested_validators.map { |validator| validator.validate(value) }
  return [] if rv.any?(&:empty?)

  rv.flatten
end