Class: CrowbarValidator
- Inherits:
-
Kwalify::Validator
- Object
- Kwalify::Validator
- CrowbarValidator
- Defined in:
- lib/crowbar/validate/crowbarvalidator.rb
Instance Method Summary collapse
-
#initialize(schema_filename) ⇒ CrowbarValidator
constructor
A new instance of CrowbarValidator.
-
#validate_hook(value, rule, path, errors) ⇒ Object
hook method called by Validator#validate().
Constructor Details
#initialize(schema_filename) ⇒ CrowbarValidator
Returns a new instance of CrowbarValidator.
6 7 8 |
# File 'lib/crowbar/validate/crowbarvalidator.rb', line 6 def initialize(schema_filename) super(Kwalify::Yaml.load_file(schema_filename)) end |
Instance Method Details
#validate_hook(value, rule, path, errors) ⇒ Object
hook method called by Validator#validate()
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/crowbar/validate/crowbarvalidator.rb', line 11 def validate_hook(value, rule, path, errors) case rule.name when "DiskURL" begin arr = URI.split(value) if arr[0] != "disk" msg = "Should have protocol disk: #{value}" errors << Kwalify::ValidationError.new(msg, path) end if arr[2] == "" msg = "Should have host specified: #{value}" errors << Kwalify::ValidationError.new(msg, path) end if arr[5] == "" msg = "Should have path specified: #{value}" errors << Kwalify::ValidationError.new(msg, path) end rescue URI::InvalidURIError msg = "Should be a valid URI: #{value}" errors << Kwalify::ValidationError.new(msg, path) end when "FQDN" subregex = /[a-zA-Z0-9\-]{1,63}/ endregex = /[\-]\z/ startregex = /\A[\-]/ numberregex = /\A\d+\z/ if value.length > 254 msg = "Should be a shorter than 254 characters: #{value}" errors << Kwalify::ValidationError.new(msg, path) end arr = value.split(".") if arr.length < 2 msg = "Must contain 2 or more subdomains: #{value}" errors << Kwalify::ValidationError.new(msg, path) end if arr.last.length < 2 msg = "Last subdomain must be more than 1 character: #{value}" errors << Kwalify::ValidationError.new(msg, path) end arr.each do |dn| if dn[subregex].nil? msg = "Can only contain _-A-Za-z0-9: #{value}" errors << Kwalify::ValidationError.new(msg, path) end if !dn[endregex].nil? msg = "Last character can not be - or _: #{value}" errors << Kwalify::ValidationError.new(msg, path) end if !dn[startregex].nil? msg = "First character can not be - or _: #{value}" errors << Kwalify::ValidationError.new(msg, path) end if !dn[numberregex].nil? msg = "Subdomain can not be only numbers: #{value}" errors << Kwalify::ValidationError.new(msg, path) end end when "Email" regex = /\A([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)\z/ if value[regex].nil? msg = "Should be an Email Address: #{value}" errors << Kwalify::ValidationError.new(msg, path) end when "DomainName" # http://tools.ietf.org/html/rfc1034#section-3.5 label = /[a-z]([a-z0-9\-]{0,61}[a-z0-9])?/i regex = /\A#{label}(\.#{label})*\z/ if value[regex].nil? msg = "Should be a Domain Name: #{value}" errors << Kwalify::ValidationError.new(msg, path) end when "IpAddress" regex = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/ if value[regex].nil? msg = "Should be an IP Address: #{value}" errors << Kwalify::ValidationError.new(msg, path) end when "IpAddressMap" regex = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/ value.each_key do |key| if key[regex].nil? msg = "Should be an IP Address: #{key}" errors << Kwalify::ValidationError.new(msg, path) end end end end |