Class: Strelka::ParamValidator::BuiltinConstraint

Inherits:
RegexpConstraint show all
Defined in:
lib/strelka/paramvalidator.rb

Overview

A constraint class that uses a collection of predefined patterns.

Constant Summary collapse

RFC822_EMAIL_ADDRESS =

RFC822 Email Address Regex

Originally written by Cal Henderson c.f. http://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 http://creativecommons.org/licenses/by-sa/2.5/

begin
	qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
	dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
	atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
		'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
	quoted_pair = '\\x5c[\\x00-\\x7f]'
	domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
	quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
	domain_ref = atom
	sub_domain = "(?:#{domain_ref}|#{domain_literal})"
	word = "(?:#{atom}|#{quoted_string})"
	domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
	local_part = "#{word}(?:\\x2e#{word})*"
	addr_spec = "#{local_part}\\x40#{domain}"
	/\A#{addr_spec}\z/n
end
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.

begin
	alphadigit = /[a-z0-9]/i
	# toplabel		 = alpha | alpha *[ alphadigit | "-" ] alphadigit
	toplabel = /[a-z]((#{alphadigit}|-)*#{alphadigit})?/i
	# domainlabel	 = alphadigit | alphadigit *[ alphadigit | "-" ] alphadigit
	domainlabel = /#{alphadigit}((#{alphadigit}|-)*#{alphadigit})?/i
	# hostname		 = *[ domainlabel "." ] toplabel
	hostname = /\A(#{domainlabel}\.)*#{toplabel}\z/
end
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
begin
	pair     = ''

	json     = /^
		(?<json> \s* (?:
			# number
			(?: 0 | -? [1-9]\d* (?:\.\d+)? (?:[eE][+-]?\d+)? )
			 |
			# boolean
			(?: true | false | null )
			 |
			# string
			"(?: [^"\\[:cntrl:]]* | \\["\\bfnrt\/] | \\u\p{XDigit}{4} )*"
			 |
			# array
			\[ (?: \g<json> (?: , \g<json> )* )? \s* \]
			 |
			# object
			\{
				(?:
					# first pair
					\s* "(?: [^"\\]* | \\["\\bfnrt\/] | \\u\p{XDigit}{4} )*" \s* : \g<json>
					# following pairs
				    (?: , \s* "(?: [^"\\]* | \\["\\bfnrt\/] | \\u\p{XDigit}{4} )*" \s* : \g<json> )*
				)?
				\s*
			\}
		) \s* )
	\z/ux
end
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

{
	:boolean	  => /^(?<boolean>t(?:rue)?|y(?:es)?|[10]|no?|f(?:alse)?)$/i,
	:integer	  => /^(?<integer>[\-\+]?\d+)$/,
	:float		  => /^(?<float>[\-\+]?(?:\d*\.\d+|\d+)(?:e[\-\+]?\d+)?)$/i,
	:alpha		  => /^(?<alpha>[[:alpha:]]+)$/,
	:alphanumeric => /^(?<alphanumeric>[[:alnum:]]+)$/,
	:printable	  => /\A(?<printable>[[:print:][:space:]]+)\z/,
	:string		  => /\A(?<string>[[:print:][:space:]]+)\z/,
	:word		  => /^(?<word>[[:word:]]+)$/,
	:email		  => /^(?<email>#{RFC822_EMAIL_ADDRESS})$/,
	:hostname	  => /^(?<hostname>#{RFC1738_HOSTNAME})$/,
	:uri		  => /^(?<uri>#{URI::URI_REF})$/,
	:uuid		  => /^(?<uuid>[[:xdigit:]]{8}(?:-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12})$/i,
	:date         => /.*\d.*/,
	:datetime     => /.*\d.*/,
	:json         => JSON_VALIDATOR_RE,
	:md5sum       => /^(?<md5sum>[[:xdigit:]]{32})$/i,
	:sha1sum      => /^(?<sha1sum>[[:xdigit:]]{40})$/i,
	:sha256sum    => /^(?<sha256sum>[[:xdigit:]]{64})$/i,
	:sha384sum    => /^(?<sha384sum>[[:xdigit:]]{96})$/i,
	:sha512sum    => /^(?<sha512sum>[[:xdigit:]]{128})$/i,
}
TRUE_VALUES =

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

%w[t true y yes 1]

Constants inherited from Constraint

Constraint::FLAGS, Constraint::TYPES

Instance Attribute Summary collapse

Attributes inherited from RegexpConstraint

#pattern

Attributes inherited from Constraint

#description, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RegexpConstraint

#check, #matched_hash

Methods inherited from Constraint

#==, #apply, for, #multiple?, register_type, #required?, #to_s

Methods included from MethodUtilities

#attr_predicate, #attr_predicate_accessor, #singleton_attr_accessor, #singleton_attr_reader, #singleton_attr_writer, #singleton_method_alias, #singleton_predicate_accessor, #singleton_predicate_reader

Constructor Details

#initialize(field, name, *options, &block) ⇒ BuiltinConstraint

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



456
457
458
459
460
461
462
463
# File 'lib/strelka/paramvalidator.rb', line 456

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

Instance Attribute Details

#pattern_nameObject (readonly)

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



471
472
473
# File 'lib/strelka/paramvalidator.rb', line 471

def pattern_name
  @pattern_name
end

Class Method Details

.reset_constraint_patternsObject

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



445
446
447
# File 'lib/strelka/paramvalidator.rb', line 445

def self::reset_constraint_patterns
	@constraint_patterns.replace( BUILTIN_CONSTRAINT_PATTERNS )
end

.valid?(name) ⇒ Boolean

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

Returns:

  • (Boolean)


439
440
441
# File 'lib/strelka/paramvalidator.rb', line 439

def self::valid?( name )
	return BUILTIN_CONSTRAINT_PATTERNS.key?( name.to_sym )
end

Instance Method Details

#blockObject

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



476
477
478
479
480
481
482
483
484
# File 'lib/strelka/paramvalidator.rb', line 476

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_patternsObject

Hash of named constraint patterns



434
# File 'lib/strelka/paramvalidator.rb', line 434

singleton_attr_reader :constraint_patterns

#validator_descriptionObject

Return the constraint expressed as a String.



488
489
490
# File 'lib/strelka/paramvalidator.rb', line 488

def validator_description
	return "a '%s'" % [ self.pattern_name ]
end