From 736c64dff3e984a66cb9466a67bf4de22cf00815 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Mon, 21 Jul 2014 18:55:39 +0200 Subject: [PATCH] Bug #3004: Role names, only word characters (letter, number, underscore) are allowed --- src/flow/lib/models/service_template.rb | 3 +- src/flow/lib/validator.rb | 37 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/flow/lib/models/service_template.rb b/src/flow/lib/models/service_template.rb index c4fafde8e2..ff544322c6 100644 --- a/src/flow/lib/models/service_template.rb +++ b/src/flow/lib/models/service_template.rb @@ -24,7 +24,8 @@ module OpenNebula :properties => { 'name' => { :type => :string, - :required => true + :required => true, + :regex => /^\w+$/ }, 'cardinality' => { :type => :integer, diff --git a/src/flow/lib/validator.rb b/src/flow/lib/validator.rb index 4ca94da426..6815b7a51f 100644 --- a/src/flow/lib/validator.rb +++ b/src/flow/lib/validator.rb @@ -377,6 +377,7 @@ class Validator # @note Schema options supported # :format # :enum + # :regex # def validate_string(body, schema_string, schema_key) if body.instance_of?(String) @@ -384,6 +385,8 @@ class Validator check_format(body, schema_string, schema_key) elsif schema_string[:enum] check_enum(body, schema_string, schema_key) + elsif schema_string[:regex] + check_regex(body, schema_string, schema_key) else body end @@ -468,6 +471,40 @@ class Validator " #{schema_string[:enum].join(', ')}; SCHEMA: #{schema_string}" end end + + # Validate an string regex + # + # @param [String] body_value to be validated + # @param [Hash] schema_string of the object to validate the body + # @param [String] schema_key of the body that will be validated in this step + # + # @return [String] The modified body + # + # @raise [ParseException] if the body does not meet the schema definition + # + # @example Validate array + # schema = { + # :type => :string, + # :regex => /^\w+$/ + # } + # + # body = "juan" + # + # Validator.check_regex(body, schema) + # #=> "juan" + # + # @note The parameter body will be modified + # @note Schema options supported + # :enum + # + def check_regex(body_value, schema_string, schema_key) + if schema_string[:regex] =~ body_value + body_value + else + raise ParseException, "KEY: '#{schema_key}' malformed; "\ + "SCHEMA: #{schema_string}" + end + end end end