Skip to content

Instantly share code, notes, and snippets.

@wireframe
Created April 25, 2012 19:21
Show Gist options
  • Save wireframe/2492492 to your computer and use it in GitHub Desktop.
Save wireframe/2492492 to your computer and use it in GitHub Desktop.
class UuidValidator < ActiveModel::EachValidator
VALID_UUID_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "is invalid") unless UuidValidator.valid_uuid?(value)
end
def self.valid_uuid?(value)
!!VALID_UUID_REGEX.match(value.to_s)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment