summaryrefslogtreecommitdiff
path: root/lib/chef/cookbook
diff options
context:
space:
mode:
authordanielsdeleo <dan@getchef.com>2014-08-07 13:44:40 -0700
committerdanielsdeleo <dan@getchef.com>2014-08-12 11:03:10 -0700
commit89427a59886b0724a5a1101dde7ea1a1def1c67a (patch)
tree502cccbf5f09aa9b359cf1e0c22867220599a98c /lib/chef/cookbook
parent13c67e0dfbde2d366f0000ef036a68f374e4cef9 (diff)
downloadchef-89427a59886b0724a5a1101dde7ea1a1def1c67a.tar.gz
Add validation to Metadata
exposes #valid? and #errors methods to check validity.
Diffstat (limited to 'lib/chef/cookbook')
-rw-r--r--lib/chef/cookbook/metadata.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb
index bd3a400e10..c1fe20e748 100644
--- a/lib/chef/cookbook/metadata.rb
+++ b/lib/chef/cookbook/metadata.rb
@@ -110,6 +110,8 @@ class Chef
@groupings = Mash.new
@recipes = Mash.new
@version = Version.new("0.0.0")
+
+ @errors = []
end
def ==(other)
@@ -118,6 +120,32 @@ class Chef
end
end
+ # Whether this metadata is valid. In order to be valid, all required
+ # fields must be set. Chef's validation implementation checks the content
+ # of a given field when setting (and raises an error if the content does
+ # not meet the criteria), so the content of the fields is not considered
+ # when checking validity.
+ #
+ # === Returns
+ # valid<Boolean>:: Whether this metadata object is valid
+ def valid?
+ run_validation
+ @errors.empty?
+ end
+
+ # A list of validation errors for this metadata object. See #valid? for
+ # comments about the validation criteria.
+ #
+ # If there are any validation errors, one or more error strings will be
+ # returned. Otherwise an empty array is returned.
+ #
+ # === Returns
+ # error messages<Array>:: Whether this metadata object is valid
+ def errors
+ run_validation
+ @errors
+ end
+
# Sets the cookbooks maintainer, or returns it.
#
# === Parameters
@@ -516,6 +544,12 @@ class Chef
private
+ def run_validation
+ if name.nil?
+ @errors = ["The `name' attribute is required in cookbook metadata"]
+ end
+ end
+
def new_args_format(caller_name, dep_name, version_constraints)
if version_constraints.empty?
">= 0.0.0"