Class: PDK::CLI::Util::OptionValidator
- Inherits:
-
Object
- Object
- PDK::CLI::Util::OptionValidator
- Defined in:
- lib/pdk/cli/util/option_validator.rb
Class Method Summary collapse
- .comma_separated_list?(list, _options = {}) ⇒ Boolean
- .enum(val, valid_entries, _options = {}) ⇒ Object
-
.valid_data_type?(string) ⇒ Boolean
Naive validation of a data type declaration.
-
.valid_module_name?(string) ⇒ Boolean
Validate the module name against the regular expression in the documentation: docs.puppet.com/puppet/4.10/modules_fundamentals.html#allowed-module-names.
-
.valid_namespace?(string) ⇒ Boolean
Validate a Puppet namespace against the regular expression in the documentation: docs.puppet.com/puppet/4.10/lang_reserved.html#classes-and-defined-resource-types.
-
.valid_param_name?(string) ⇒ Boolean
Validate that a class/defined type parameter matches the regular expression in the documentation: docs.puppet.com/puppet/4.10/lang_reserved.html#parameters The parameter should also not be a reserved word or overload a metaparameter.
Class Method Details
.comma_separated_list?(list, _options = {}) ⇒ Boolean
5 6 7 |
# File 'lib/pdk/cli/util/option_validator.rb', line 5 def self.comma_separated_list?(list, = {}) (list =~ %r{^[\w\-]+(?:,[\w\-]+)+$}) ? true : false end |
.enum(val, valid_entries, _options = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/pdk/cli/util/option_validator.rb', line 9 def self.enum(val, valid_entries, = {}) vals = val.is_a?(Array) ? val : [val] invalid_entries = vals.reject { |e| valid_entries.include?(e) } unless invalid_entries.empty? raise _('Error: the following values are invalid: %{invalid_entries}') % { invalid_entries: invalid_entries } end val end |
.valid_data_type?(string) ⇒ Boolean
This prevents the use of dynamic data types like TypeReferences but that shouldn’t be a problem for the current feature set. This should be replaced eventually by something better (or just call Puppet::Pops::Types::TypesParser)
Naive validation of a data type declaration. Extracts all the bare words and compares them against a list of known data types.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pdk/cli/util/option_validator.rb', line 56 def self.valid_data_type?(string) valid_types = %w[ String Integer Float Numeric Boolean Array Hash Regexp Undef Default Class Resource Scalar Collection Variant Data Pattern Enum Tuple Struct Optional Catalogentry Type Any Callable NotUndef ].freeze string.scan(%r{\b(([a-zA-Z0-9_]+)(,|\[|\]|\Z))}) do |result| type = result[1] return false unless string =~ %r{\A[A-Z]} unless valid_types.include?(type) PDK.logger.warn(_("Non-standard data type '%{type}', make sure the type is available in your code, or dependencies") % { type: type }) end end true end |
.valid_module_name?(string) ⇒ Boolean
Validate the module name against the regular expression in the documentation: docs.puppet.com/puppet/4.10/modules_fundamentals.html#allowed-module-names
22 23 24 |
# File 'lib/pdk/cli/util/option_validator.rb', line 22 def self.valid_module_name?(string) !(string =~ %r{\A[a-z][a-z0-9_]*\Z}).nil? end |
.valid_namespace?(string) ⇒ Boolean
Validate a Puppet namespace against the regular expression in the documentation: docs.puppet.com/puppet/4.10/lang_reserved.html#classes-and-defined-resource-types
28 29 30 31 32 |
# File 'lib/pdk/cli/util/option_validator.rb', line 28 def self.valid_namespace?(string) return false if (string || '').split('::').last == 'init' !(string =~ %r{\A([a-z][a-z0-9_]*)(::[a-z][a-z0-9_]*)*\Z}).nil? end |
.valid_param_name?(string) ⇒ Boolean
Validate that a class/defined type parameter matches the regular expression in the documentation: docs.puppet.com/puppet/4.10/lang_reserved.html#parameters The parameter should also not be a reserved word or overload a metaparameter.
41 42 43 44 45 46 47 |
# File 'lib/pdk/cli/util/option_validator.rb', line 41 def self.valid_param_name?(string) reserved_words = %w[trusted facts server_facts title name].freeze = %w[alias audit before loglevel noop notify require schedule stage subscribe tag].freeze return false if reserved_words.include?(string) || .include?(string) !(string =~ %r{\A[a-z][a-zA-Z0-9_]*\Z}).nil? end |