mastodon/app/validators/regexp_syntax_validator.rb

12 lines
316 B
Ruby
Raw Normal View History

2024-02-16 08:14:23 +01:00
# frozen_string_literal: true
class RegexpSyntaxValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?
2024-02-16 17:30:25 +01:00
Regexp.compile(value)
rescue RegexpError => e
record.errors.add(attribute, I18n.t('applications.invalid_regexp', message: e.message))
2024-02-16 08:14:23 +01:00
end
end