Skip to content

Instantly share code, notes, and snippets.

@joshwlewis
Created November 19, 2012 20:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshwlewis/4113551 to your computer and use it in GitHub Desktop.
Save joshwlewis/4113551 to your computer and use it in GitHub Desktop.
Rails validation for scss (or sass)
# app/validators/scss_format_validator.rb
class ScssFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
begin
# Attempt to parse SCSS
Sass::Engine.new(value, syntax: :scss).render
rescue Exception => e
# Add error if parsing fails
object.errors.add(attribute, :invalid_scss, error: e.inspect)
end
end
end
class Client < ActiveRecord::Base
attr_accessible :style_text
validates :style_text, scss_format: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment