Skip to content

Instantly share code, notes, and snippets.

@msroot
Created April 20, 2015 22:55
rails validations validate html
# assume we have a :data attribute (an hstore hash) in our model that values have html
# data :hstore default({}), not null
validate :invalid_html?
def invalid_html?
self.data.each do |key, value|
doc = Nokogiri::HTML(value) {|config| config.strict }
if doc.errors.any?
errors.add(:base, "#{key}: #{doc.errors.join(", ")}")
end
end
end
# The problem is that Nokogiri its NOT very :strict
#2.1.3 :001 > Nokogiri::HTML("<a/><a/><a/>") {|config| config.strict }.errors.any?
# => false
2.1.3 :002 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment