Sha256: 2e5cd0c1e9a175b15ec89369465e5197e7443e9e8129f4c8b970f5ba0ac36160

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

module OpenActive
  module Validators
    # https://schema.org/Time
    # format spec: https://www.w3.org/TR/xmlschema-2/#time
    class TimeValidator < BaseValidator
      # https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s07.html#id2175516
      # Minor additions to make second values and timezone optional
      REGEX = /(?<hour>2[0-3]|[01][0-9]):?(?<minute>[0-5][0-9]):?(?<second>[0-5][0-9])?(?<timezone>Z|[+-](?:2[0-3]|[01][0-9])(?::?(?:[0-5][0-9]))?)?/.freeze

      # 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)
        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)
        REGEX =~ value
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
openactive-0.1.2 lib/openactive/validators/time_validator.rb
openactive-0.1.1 lib/openactive/validators/time_validator.rb