A constraint class that uses a collection of predefined 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
Validation regexp for JSON Converted to oniguruma syntax from the PCRE example at:
http://stackoverflow.com/questions/2583472/regex-to-validate-json
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 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/
Field values which result in a valid ‘true’ value for :boolean constraints
The name of the builtin pattern the field should be constrained by
Create a new BuiltinConstraint using the pattern named name for the specified field.
# File lib/strelka/paramvalidator.rb, line 470
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 the named patterns to the defaults. Mostly used for testing.
# File lib/strelka/paramvalidator.rb, line 459
def self::reset_constraint_patterns
@constraint_patterns.replace( BUILTIN_CONSTRAINT_PATTERNS )
end
Return true if name is the name of a built-in constraint.
# File lib/strelka/paramvalidator.rb, line 453
def self::valid?( name )
return BUILTIN_CONSTRAINT_PATTERNS.key?( name.to_sym )
end
Check for an additional post-processor method, and if it exists, return it as a Method object.
# File lib/strelka/paramvalidator.rb, line 490
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
Hash of named constraint patterns
# File lib/strelka/paramvalidator.rb, line 448
singleton_attr_reader :constraint_patterns
Return the constraint expressed as a String.
# File lib/strelka/paramvalidator.rb, line 502
def validator_description
return "a '%s'" % [ self.pattern_name ]
end
Post-process a :boolean value.
# File lib/strelka/paramvalidator.rb, line 512
def post_process_boolean( val )
return TRUE_VALUES.include?( val.to_s.downcase )
end
Constrain a value to a parseable Date
# File lib/strelka/paramvalidator.rb, line 518
def post_process_date( val )
return Date.parse( val )
rescue ArgumentError
return nil
end
Constrain a value to a parseable Date
# File lib/strelka/paramvalidator.rb, line 526
def post_process_datetime( val )
return Time.parse( val )
rescue ArgumentError
return nil
end
Constrain a value to a Float
# File lib/strelka/paramvalidator.rb, line 534
def post_process_float( val )
return Float( val.to_s )
end
Post-process a valid :integer field.
# File lib/strelka/paramvalidator.rb, line 540
def post_process_integer( val )
return Integer( val.to_s )
end
Post-process a valid :uri field.
# File lib/strelka/paramvalidator.rb, line 546
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