Strelka::ParamValidator::

RegexpConstraint

class
Superclass
Strelka::ParamValidator::Constraint

A constraint expressed as a regular expression.

Attributes

pattern[R]

The constraint's pattern

Public Class Methods

anchor
new( name, pattern, *args, &block )

Create a new RegexpConstraint that will validate the field of the given name with the specified pattern.

# File lib/strelka/paramvalidator.rb, line 265
def initialize( name, pattern, *args, &block )
        @pattern = pattern

        super( name, *args, &block )
end

Public Instance Methods

anchor
check( value, untaint )

Check the value against the regular expression and return its match groups if successful.

# File lib/strelka/paramvalidator.rb, line 282
def check( value, untaint )
        self.log.debug "Validating %p via regexp %p" % [ value, self.pattern ]
        match = self.pattern.match( value.to_s ) or return nil

        if match.captures.empty?
                self.log.debug "  no captures, using whole match: %p" % [match[0]]
                return super( match[0], untaint )

        elsif match.names.length > 1
                self.log.debug "  extracting hash of named captures: %p" % [ match.names ]
                rhash = self.matched_hash( match, untaint )
                return super( rhash, untaint )

        elsif match.captures.length == 1
                self.log.debug "  extracting one capture: %p" % [match.captures.first]
                return super( match.captures.first, untaint )

        else
                self.log.debug "  extracting multiple captures: %p" % [match.captures]
                values = match.captures
                values.map {|val| val.untaint if val } if untaint
                return super( values, untaint )
        end
end
anchor
matched_hash( match, untaint )

Return a Hash of the given match object's named captures, untainting the values if untaint is true.

# File lib/strelka/paramvalidator.rb, line 310
def matched_hash( match, untaint )
        return match.names.inject( {} ) do |accum,name|
                value = match[ name ]
                value.untaint if untaint && value
                accum[ name.to_sym ] = value
                accum
        end
end
anchor
validator_description()

Return the constraint expressed as a String.

# File lib/strelka/paramvalidator.rb, line 321
def validator_description
        return "a value matching the pattern %p" % [ self.pattern ]
end