class Strelka::ParamValidator::BuiltinConstraint

A constraint class that uses a collection of predefined patterns.

Constants

BUILTIN_CONSTRAINT_PATTERNS

The Hash of builtin constraints that are validated against a regular expression. :todo: Document that these are the built-in constraints that can be used in a route

JSON_VALIDATOR_RE

Validation regexp for JSON Converted to oniguruma syntax from the PCRE example at:

http://stackoverflow.com/questions/2583472/regex-to-validate-json
RFC1738_HOSTNAME

Pattern for (loosely) matching a valid hostname. This isn't strictly RFC-compliant because, in practice, many hostnames used on the Internet aren't.

RFC822_EMAIL_ADDRESS

RFC822 Email Address Regex


Originally written by Cal Henderson c.f. iamcal.com/publish/articles/php/parsing_email/

Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.

Licensed under a Creative Commons Attribution-ShareAlike 2.5 License creativecommons.org/licenses/by-sa/2.5/

TRUE_VALUES

Field values which result in a valid ‘true’ value for :boolean constraints

Attributes

pattern_name[R]

The name of the builtin pattern the field should be constrained by

Public Class Methods

new( field, name, *options, &block ) click to toggle source

Create a new BuiltinConstraint using the pattern named name for the specified field.

# File lib/strelka/paramvalidator.rb, line 455
def initialize( field, name, *options, &block )
        name ||= field
        @pattern_name = name
        pattern = BUILTIN_CONSTRAINT_PATTERNS[ name.to_sym ] or
                raise ScriptError, "no such builtin constraint %p" % [ name ]

        super( field, pattern, *options, &block )
end
reset_constraint_patterns() click to toggle source

Reset the named patterns to the defaults. Mostly used for testing.

# File lib/strelka/paramvalidator.rb, line 444
def self::reset_constraint_patterns
        @constraint_patterns.replace( BUILTIN_CONSTRAINT_PATTERNS )
end
valid?( name ) click to toggle source

Return true if name is the name of a built-in constraint.

# File lib/strelka/paramvalidator.rb, line 438
def self::valid?( name )
        return BUILTIN_CONSTRAINT_PATTERNS.key?( name.to_sym )
end

Public Instance Methods

block() click to toggle source

Check for an additional post-processor method, and if it exists, return it as a Method object.

Calls superclass method
# File lib/strelka/paramvalidator.rb, line 475
def block
        if custom_block = super
                return custom_block
        else
                post_processor = "post_process_%s" % [ @pattern_name ]
                return nil unless self.respond_to?( post_processor, true )
                return self.method( post_processor )
        end
end
constraint_patterns() click to toggle source

Hash of named constraint patterns

# File lib/strelka/paramvalidator.rb, line 433
singleton_attr_reader :constraint_patterns
validator_description() click to toggle source

Return the constraint expressed as a String.

# File lib/strelka/paramvalidator.rb, line 487
def validator_description
        return "a '%s'" % [ self.pattern_name ]
end

Protected Instance Methods

post_process_boolean( val ) click to toggle source

Post-process a :boolean value.

# File lib/strelka/paramvalidator.rb, line 497
def post_process_boolean( val )
        return TRUE_VALUES.include?( val.to_s.downcase )
end
post_process_date( val ) click to toggle source

Constrain a value to a parseable Date

# File lib/strelka/paramvalidator.rb, line 503
def post_process_date( val )
        return Date.parse( val )
rescue ArgumentError
        return nil
end
post_process_datetime( val ) click to toggle source

Constrain a value to a parseable Date

# File lib/strelka/paramvalidator.rb, line 511
def post_process_datetime( val )
        return Time.parse( val )
rescue ArgumentError
        return nil
end
post_process_float( val ) click to toggle source

Constrain a value to a Float

# File lib/strelka/paramvalidator.rb, line 519
def post_process_float( val )
        return Float( val.to_s )
end
post_process_integer( val ) click to toggle source

Post-process a valid :integer field.

# File lib/strelka/paramvalidator.rb, line 525
def post_process_integer( val )
        return Integer( val.to_s )
end
post_process_uri( val ) click to toggle source

Post-process a valid :uri field.

# File lib/strelka/paramvalidator.rb, line 531
def post_process_uri( val )
        return URI.parse( val.to_s )
rescue URI::InvalidURIError => err
        self.log.error "Error trying to parse URI %p: %s" % [ val, err.message ]
        return nil
rescue NoMethodError
        self.log.debug "Ignoring bug in URI#parse"
        return nil
end