mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
refactor validators
add a new setting for min pm body length use that setting for flags scale entropy check down for pms
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
class PostValidator < ActiveModel::Validator
|
||||
require_dependency 'validators/stripped_length_validator'
|
||||
module Validators; end
|
||||
class Validators::PostValidator < ActiveModel::Validator
|
||||
def validate(record)
|
||||
presence(record)
|
||||
stripped_length(record)
|
||||
raw_quality(record)
|
||||
max_mention_validator(record)
|
||||
max_images_validator(record)
|
||||
@@ -7,8 +11,19 @@ class PostValidator < ActiveModel::Validator
|
||||
unique_post_validator(record)
|
||||
end
|
||||
|
||||
def presence(post)
|
||||
[:raw,:user_id,:topic_id].each do |attr_name|
|
||||
post.errors.add(attr_name, :blank, options) if post.send(attr_name).blank?
|
||||
end
|
||||
end
|
||||
|
||||
def stripped_length(post)
|
||||
range = post.topic.try(:private_message?) ? SiteSetting.private_message_post_length : SiteSetting.post_length
|
||||
Validators::StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
||||
end
|
||||
|
||||
def raw_quality(post)
|
||||
sentinel = TextSentinel.body_sentinel(post.raw)
|
||||
sentinel = TextSentinel.body_sentinel(post.raw, private_message: post.topic.try(:private_message?))
|
||||
post.errors.add(:raw, I18n.t(:is_invalid)) unless sentinel.valid?
|
||||
end
|
||||
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
class StrippedLengthValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
module Validators; end
|
||||
class Validators::StrippedLengthValidator < ActiveModel::EachValidator
|
||||
def self.validate(record, attribute, value, range)
|
||||
unless value.nil?
|
||||
stripped_length = value.strip.length
|
||||
# the `in` parameter might be a lambda when the range is dynamic
|
||||
range = options[:in].lambda? ? options[:in].call : options[:in]
|
||||
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_short', count: range.begin)) unless
|
||||
record.errors.add attribute, (I18n.t('errors.messages.too_short', count: range.begin)) unless
|
||||
stripped_length >= range.begin
|
||||
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_long', count: range.end)) unless
|
||||
record.errors.add attribute, (I18n.t('errors.messages.too_long', count: range.end)) unless
|
||||
stripped_length <= range.end
|
||||
else
|
||||
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.blank'))
|
||||
record.errors.add attribute, (I18n.t('errors.messages.blank'))
|
||||
end
|
||||
end
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
# the `in` parameter might be a lambda when the range is dynamic
|
||||
range = options[:in].lambda? ? options[:in].call : options[:in]
|
||||
self.class.validate(record, attribute, value, range)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user