Class: SBOM::CycloneDX::Validator::ArrayValidator
- Inherits:
-
BaseValidator
- Object
- BaseValidator
- SBOM::CycloneDX::Validator::ArrayValidator
- Defined in:
- lib/sbom/cyclone_dx/validator/array_validator.rb
Constant Summary
Constants inherited from BaseValidator
BaseValidator::INVALID_TYPE, BaseValidator::MISSING_REQUIRED
Instance Method Summary collapse
-
#initialize(items:, unique: false, required: false) ⇒ ArrayValidator
constructor
A new instance of ArrayValidator.
- #raw_types ⇒ Object
- #validate(value) ⇒ Object
Methods inherited from BaseValidator
Constructor Details
#initialize(items:, unique: false, required: false) ⇒ ArrayValidator
Returns a new instance of ArrayValidator.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sbom/cyclone_dx/validator/array_validator.rb', line 12 def initialize(items:, unique: false, required: false) super(Array, required: required) @unique = unique @items_validator = case items when :array, :boolean, :date_time, :email_address, :float, :integer, :string, :uri Validator.for(items, required: true) # when Proc # proc_validator(items) when Array Validator.for(items.first, required: true, **items.last) when Class raise "Unsupported items type: #{items}" unless items < Record::Base RecordValidator.new(type: items, required: true) else raise ArgumentError, "Unsupported items type: #{items}" end end |
Instance Method Details
#raw_types ⇒ Object
42 43 44 |
# File 'lib/sbom/cyclone_dx/validator/array_validator.rb', line 42 def raw_types @items_validator.raw_types end |
#validate(value) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/sbom/cyclone_dx/validator/array_validator.rb', line 33 def validate(value) rv = super return rv unless value.is_a?(Array) rv << "Unique array contains non-unique values" if @unique && value.uniq.length != value.length value.each { |item| rv += @items_validator.validate(item) } rv end |