Sha256: b53b4eb55a9db08158b3c575e30316a3c201ccc61e644b9e3e3239112cd9b221

Contents?: true

Size: 1.38 KB

Versions: 10

Compression:

Stored size: 1.38 KB

Contents

module OpenActive
  module Validators
    class ArrayOfValidator < BaseValidator
      attr_accessor :item_validator

      def initialize(item_validator)
        @item_validator = item_validator
      end

      # Coerce given value to the type the validator is validating against.
      # PLEASE NOTE: no checks are performed on the given value.
      # It is therefore recommended to call the "run" method first before this.
      #
      # @param value [mixed] The value to coerce.
      # @return [mixed] The same value.

      def coerce(value)
        # NOTE: OpenActive is more strict than plain json-ld, so no coercion into arrays
        value
      end

      # Run validation on the given value.
      #
      # @param value [mixed] The value to validate.
      # @return [Boolean] Whether validation passes or not.

      def run(value)
        null_validator = NullValidator.new

        # NOTE: OpenActive is more strict than plain json-ld, so no coercion into arrays

        # Check if value is an array
        return true if item_validator.run(value) == true

        return false if ArrayValidator.new.run(value) == false

        value.each do |item|
          # If any of the provided items is not null nor an instance of the provided class name
          return false if null_validator.run(item) == false && item_validator.run(item) == false
        end
        true
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
openactive-0.5.0 lib/openactive/validators/array_of_validator.rb
openactive-0.4.0 lib/openactive/validators/array_of_validator.rb
openactive-0.3.0 lib/openactive/validators/array_of_validator.rb
openactive-0.2.2 lib/openactive/validators/array_of_validator.rb
openactive-0.2.1 lib/openactive/validators/array_of_validator.rb
openactive-0.2.0 lib/openactive/validators/array_of_validator.rb
openactive-0.1.2 lib/openactive/validators/array_of_validator.rb
openactive-0.1.1 lib/openactive/validators/array_of_validator.rb
openactive-0.1.0 lib/openactive/validators/array_of_validator.rb
openactive-0.1.0.rc1 lib/openactive/validators/array_of_validator.rb