class J2119::FieldValueConstraint

Verify constraints on values of a named field

Public Class Methods

new(name, params) click to toggle source
Calls superclass method J2119::Constraint::new
# File lib/j2119/constraints.rb, line 223
def initialize(name, params)
  super()
  @name = name
  @params = params
end

Public Instance Methods

check(node, path, problems) click to toggle source
# File lib/j2119/constraints.rb, line 234
def check(node, path, problems)

  # value-checking is orthogonal to existence checking
  return if !node.key?(@name)

  value = node[@name]

  if @params[:enum]
    if !(@params[:enum].include?(value))
      problems <<
        "#{path}.#{@name} is \"#{value}\", " +
        "not one of the allowed values #{@params[:enum]}"
    end
    
    # if enum constraint are provided, others are ignored
    return
  end

  if @params[:equal]
    begin
      if value != @params[:equal]
        problems <<
          "#{path}.#{@name} is #{value} " +
          "but required value is #{@params[:equal]}"
      end
    rescue Exception
      # should be caught by type constraint
    end
  end
  if @params[:floor]
    begin
      if value <= @params[:floor]
        problems <<
          "#{path}.#{@name} is #{value} " +
          "but allowed floor is #{@params[:floor]}"
      end
    rescue Exception
      # should be caught by type constraint
    end
  end
  if @params[:min]
    begin
      if value < @params[:min]
        problems <<
          "#{path}.#{@name} is #{value} " +
          "but allowed minimum is #{@params[:min]}"
      end
    rescue Exception
      # should be caught by type constraint
    end
  end
  if @params[:ceiling]
    begin
      if value >= @params[:ceiling]
        problems <<
          "#{path}.#{@name} is #{value} " +
          "but allowed ceiling is #{@params[:ceiling]}"
      end
    rescue Exception
      # should be caught by type constraint
    end
  end
  if @params[:max]
    begin
      if value > @params[:max]
        problems <<
          "#{path}.#{@name} is #{value} " +
          "but allowed maximum is #{@params[:max]}"
      end
    rescue Exception
      # should be caught by type constraint
    end
  end
end
to_s() click to toggle source
# File lib/j2119/constraints.rb, line 229
def to_s
  conds = (@conditions.empty?) ? '' : " #{@conditions.size} conditions"
  "<Field #{@name} has constraints #{@params}#{conds}>"
end