Class: Parse::Constraint::RegularExpressionConstraint

Inherits:
Parse::Constraint show all
Defined in:
lib/parse/query/constraints.rb

Overview

Equivalent to the $regex Parse query operation. Requires that a field value match a regular expression.

q.where :field.like => /ruby_regex/i :name.like => /Bob/i

Instance Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Builds the regex constraint with security validation.

Returns:

  • (Hash)

    the compiled constraint

Raises:

  • (ArgumentError)

    if the pattern is potentially dangerous (ReDoS)



1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
# File 'lib/parse/query/constraints.rb', line 1661

def build
  value = formatted_value
  pattern_str = value.is_a?(Regexp) ? value.source : value.to_s
  options = value.is_a?(Regexp) && value.casefold? ? "i" : nil

  # Validate the regex pattern for ReDoS vulnerabilities
  Parse::RegexSecurity.validate!(pattern_str)

  if options
    { @operation.operand => { key => pattern_str, :$options => options } }
  else
    { @operation.operand => { key => pattern_str } }
  end
end

#likeRegularExpressionConstraint

A registered method on a symbol to create the constraint. Maps to Parse operator "$regex".

Examples:

q.where :field.like => /ruby_regex/i

Returns:



# File 'lib/parse/query/constraints.rb', line 1645

#regexRegularExpressionConstraint

Alias for #like



1654
# File 'lib/parse/query/constraints.rb', line 1654

constraint_keyword :$regex