Module: TableSchema::Constraints::Pattern

Included in:
TableSchema::Constraints
Defined in:
lib/tableschema/constraints/pattern.rb

Instance Method Summary collapse

Instance Method Details

#check_patternObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tableschema/constraints/pattern.rb', line 5

def check_pattern
  constraint = lambda { |value| value.match(/#{@constraints[:pattern]}/) }
  if @field.type == 'yearmonth'
    valid = constraint.call(Date.new(@value[:year], @value[:month]).strftime('%Y-%m'))
  else
    valid = constraint.call(@value.to_json)
  end

  unless valid
    raise TableSchema::ConstraintError.new("The value for the field `#{@field[:name]}` must match the pattern")
  end
  true
end