Class: SiriusApi::Validators::YearCweek

Inherits:
Grape::Validations::Validator
  • Object
show all
Defined in:
lib/sirius_api/validators/year_cweek.rb

Overview

A Grape validator for date in format %G-%V; an ISO 8601 week-based year and a week number.

Examples:

How to use

params { requires :week, type: String, year_cweek: true }

Instance Method Summary collapse

Instance Method Details

#validate_param!(attr_name, params) ⇒ Object


14
15
16
17
18
19
20
21
22
# File 'lib/sirius_api/validators/year_cweek.rb', line 14

def validate_param!(attr_name, params)
  value = params[attr_name]

  unless value =~ /\A\d{4}-\d{1,2}\z/ && (Date.strptime(value, '%G-%V') rescue false)
    raise Grape::Exceptions::Validation,
      params: [@scope.full_name(attr_name)],
      message: 'must be valid ISO 8601 week-based year and week number, e.g. 2016-42'
  end
end