Module: Occi::Core::Helpers::IdentifierValidator
- Included in:
- Category
- Defined in:
- lib/occi/core/helpers/identifier_validator.rb
Overview
Introduces validation capabilities to every receiver class. Should be used via the ‘extend` keyword.
Constant Summary collapse
- PROHIBITED_SCHEMA_CHARS =
Characters prohibited in ‘schema` attribute
%w[% & ? ! \\].freeze
- REGEXP_TERM =
Definition of characters allowed in ‘term` attribute
/^([[:alpha:]]|[[:digit:]])([[:alpha:]]|[[:digit:]]|-|_)*$/
Instance Method Summary collapse
-
#prohibited_chars!(schema) ⇒ Object
:nodoc:.
-
#valid_identifier!(identifier) ⇒ Object
Similar to ‘::valid_identifier?`, raises an `Occi::Core::Errors::CategoryValidationError` error in case of failure.
-
#valid_identifier?(identifier) ⇒ TrueClass, FalseClass
Validates given ‘identifier` as a combination of rules for `term` and `schema`.
-
#valid_schema!(schema) ⇒ Object
Similar to ‘::valid_schema?`, raises an `Occi::Core::Errors::CategoryValidationError` error in case of failure.
-
#valid_schema?(schema) ⇒ TrueClass, FalseClass
Validates given ‘schema` against the restrictions imposed by the URI specification.
-
#valid_term!(term) ⇒ Object
Similar to ‘::valid_term?`, raises an `Occi::Core::Errors::CategoryValidationError` error in case of failure.
-
#valid_term?(term) ⇒ TrueClass, FalseClass
Validates given ‘term` against the restrictions imposed by the OCCI specification.
-
#valid_uri!(uri) ⇒ Object
:nodoc:.
Instance Method Details
#prohibited_chars!(schema) ⇒ Object
:nodoc:
127 128 129 130 131 |
# File 'lib/occi/core/helpers/identifier_validator.rb', line 127 def prohibited_chars!(schema) PROHIBITED_SCHEMA_CHARS.each do |char| raise "Schema #{schema.inspect} contains prohibited character #{char.inspect}" if schema.include?(char) end end |
#valid_identifier!(identifier) ⇒ Object
Similar to ‘::valid_identifier?`, raises an `Occi::Core::Errors::CategoryValidationError` error in case of failure.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/occi/core/helpers/identifier_validator.rb', line 110 def valid_identifier!(identifier) elements = identifier.split('#') if elements.count != 2 raise Occi::Core::Errors::CategoryValidationError, "Identifier #{identifier.inspect} is malformed" end valid_schema! "#{elements.first}#" valid_term! elements.last end |
#valid_identifier?(identifier) ⇒ TrueClass, FalseClass
Validates given ‘identifier` as a combination of rules for `term` and `schema`.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/occi/core/helpers/identifier_validator.rb', line 95 def valid_identifier?(identifier) begin valid_identifier! identifier rescue URI::InvalidURIError, Occi::Core::Errors::CategoryValidationError => ex logger.warn "Identifier validation for #{self} failed with #{ex.}" if respond_to?(:logger) return false end true end |
#valid_schema!(schema) ⇒ Object
Similar to ‘::valid_schema?`, raises an `Occi::Core::Errors::CategoryValidationError` error in case of failure.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/occi/core/helpers/identifier_validator.rb', line 72 def valid_schema!(schema) if schema.blank? raise Occi::Core::Errors::CategoryValidationError, "Schema #{schema.inspect} cannot be blank" end unless schema.end_with?('#') raise Occi::Core::Errors::CategoryValidationError, "Schema #{schema.inspect} must be terminated with '#'" end valid_uri! schema prohibited_chars! schema end |
#valid_schema?(schema) ⇒ TrueClass, FalseClass
Validates given ‘schema` against the restrictions imposed by the URI specification. See Ruby’s ‘URI` class for details. On top of that, every schema must be terminated with ’#‘.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/occi/core/helpers/identifier_validator.rb', line 57 def valid_schema?(schema) begin valid_schema! schema rescue URI::InvalidURIError, Occi::Core::Errors::CategoryValidationError => ex logger.warn "Schema validation for #{self} failed with #{ex.}" if respond_to?(:logger) return false end true end |
#valid_term!(term) ⇒ Object
Similar to ‘::valid_term?`, raises an `Occi::Core::Errors::CategoryValidationError` error in case of failure.
39 40 41 42 43 44 45 |
# File 'lib/occi/core/helpers/identifier_validator.rb', line 39 def valid_term!(term) validation_result = REGEXP_TERM.match(term) return if validation_result raise Occi::Core::Errors::CategoryValidationError, "Term #{term.inspect} does not match #{REGEXP_TERM.inspect}" end |
#valid_term?(term) ⇒ TrueClass, FalseClass
Validates given ‘term` against the restrictions imposed by the OCCI specification. See `REGEXP_TERM` in this class for details.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/occi/core/helpers/identifier_validator.rb', line 24 def valid_term?(term) begin valid_term! term rescue Occi::Core::Errors::CategoryValidationError => ex logger.warn "Term validation for #{self} failed with #{ex.}" if respond_to?(:logger) return false end true end |
#valid_uri!(uri) ⇒ Object
:nodoc:
122 123 124 |
# File 'lib/occi/core/helpers/identifier_validator.rb', line 122 def valid_uri!(uri) URI.split uri end |