Module: Zen::Validation

Included in:
Settings::Setting, Settings::SettingsGroup, Language, Language::Translation, Package, Theme
Defined in:
lib/zen/validation.rb

Overview

The Validation module is a very basic validation framework that's used by various internal modules/classes such as Zen::Plugin and Zen::Package.

Usage

Using the module is pretty simple. Include it, specify the validation rules in a method and call it. All official modules and classes use a method called "validate" but you're free to name it whatever you want. A basic example looks like the following:

class Something
  include Zen::Validation

  attr_accessor :name

  def validate
    validates_presence(:name)
  end
end

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) validates_filepath(attribute)

Checks if the specified attribute contains a valid filepath.

Examples:

validates_filepath(:directory)

Parameters:

  • attribute (String/Symbol)

    The attribute to validate.

Raises:

Since:

  • 0.2.5



135
136
137
138
139
140
141
142
143
144
# File 'lib/zen/validation.rb', line 135

def validates_filepath(attribute)
  path = send(attribute)

  if !File.exist?(path)
    raise(
      ValidationError,
      "The path #{path} in \"#{attribute}\" doesn't exist."
    )
  end
end

- (Object) validates_format(attribute, regexp = nil)

Checks if the given attributes match the specified regular expressions. When a hash is specified the keys should be the names of the attributes to validate and the values the regular expressions to use.

Examples:

validates_format(:name, /[\w\-]+/)
validates_format(:name => /[\w\-]+/, :age => /[0-9]+/)

Parameters:

  • attribute (Hash/Symbol)

    The name of the attribute to validate or a hash containing all the attributes and their regular expressions.

  • regexp (Regexp) (defaults to: nil)

    The regular expression to use when validating a single attribute.

Raises:

  • (ValidationError)

    Raised when one of the attributes doesn't matches the regular expression.

Since:

  • 0.2.5



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/zen/validation.rb', line 106

def validates_format(attribute, regexp = nil)
  if attribute.class != Hash
    attribute = {attribute => regexp}
  end

  # Try to match all attributes
  attribute.each do |attr, regexp|
    val   = send(attr)
    match = val =~ regexp

    if !match
      raise(
        ValidationError,
        "The attribute \"#{attr}\" doesn't match #{regexp}"
      )
    end
  end
end

- (Object) validates_length(attribute, options)

Checks if the length of a string matches the given length. You can specify a minimum length, a maximum one as well as both.

Examples:

validates_length(:foobar, :min => 5, :max => 10)

Parameters:

  • attribute (String/Symbol)

    The attribute to validate.

  • options (Hash)

    Hash containing the options to use for determining how long the attribute's value should be.

Options Hash (options):

  • :min (Fixnum)

    The minimum length of the attribute's value.

  • :max (Fixnum)

    The maximum length of the value.

Raises:

  • (ValidationError)

    Raised then the value of the attribute isn't long or short enough.

Since:

  • 0.2.5



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/zen/validation.rb', line 66

def validates_length(attribute, options)
  value = send(attribute)

  if !value.respond_to?(:length)
    raise(
      ValidationError,
      "The length of \"#{attribute}\" can't be checked as the method " + \
        "\"length\" doesn't exist."
    )
  end

  # Time to validate the length
  length = value.length

  if options.key?(:min) and length < options[:min]
    raise(ValidationError, "The attribute \"#{attribute}\" is too short.")
  end

  if options.key?(:max) and length > options[:max]
    raise(ValidationError, "The attribute \"#{attribute}\" is too long.")
  end
end

- (Object) validates_presence(attributes)

Checks if the specified attributes exist and aren't set to nil.

Examples:

validates_presence(:name)

Parameters:

  • attributes (Array/Symbol/String)

    Either a single or multiple attributes to validate.

Raises:

  • (ValidationError)

    Raised whenever a attribute is missing or is set to nil.

Since:

  • 0.2.5



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zen/validation.rb', line 38

def validates_presence(attributes)
  if attributes.class != Array
    attributes = [attributes]
  end

  attributes.each do |f|
    if !respond_to?(f) or send(f).nil?
      raise(ValidationError, "The attribute \"#{f}\" doesn't exist.")
    end
  end
end