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)



1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
# File 'lib/parse/query/constraints.rb', line 1632

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 1616

#regexRegularExpressionConstraint

Alias for #like



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

constraint_keyword :$regex