summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/kwalify-1.pp9
-rw-r--r--examples/kwalify-2.pp24
-rw-r--r--examples/validate_resource-1.pp23
-rw-r--r--examples/validate_resource-1.schema39
-rw-r--r--examples/validate_resource-2.pp17
-rw-r--r--examples/validate_resource-2.schema26
-rw-r--r--lib/puppet/parser/functions/validate_resource.rb36
7 files changed, 174 insertions, 0 deletions
diff --git a/examples/kwalify-1.pp b/examples/kwalify-1.pp
new file mode 100644
index 0000000..852e46c
--- /dev/null
+++ b/examples/kwalify-1.pp
@@ -0,0 +1,9 @@
+$schema = {
+ 'type' => 'seq',
+ 'sequence' => [
+ { 'type' => 'str', 'enum' => ['asdf','fdsa'] }
+ ]
+}
+$document = ['a', 'b', 'c']
+
+kwalify($schema, $document)
diff --git a/examples/kwalify-2.pp b/examples/kwalify-2.pp
new file mode 100644
index 0000000..3f4ec33
--- /dev/null
+++ b/examples/kwalify-2.pp
@@ -0,0 +1,24 @@
+$schema = {
+ 'type' => 'map',
+ 'mapping' => {
+ 'name' => {
+ 'type' => 'str',
+ 'required' => true,
+ },
+ 'email' => {
+ 'type' => 'str',
+ 'pattern' => '/@/',
+ },
+ 'age' => {
+ 'type' => 'str',
+ 'pattern' => '/^\d+$/',
+ },
+ }
+}
+$document = {
+ 'name' => 'foo',
+ 'email' => 'foo@mail.com',
+ 'age' => 20,
+}
+
+kwalify($schema, $document)
diff --git a/examples/validate_resource-1.pp b/examples/validate_resource-1.pp
new file mode 100644
index 0000000..c701b8d
--- /dev/null
+++ b/examples/validate_resource-1.pp
@@ -0,0 +1,23 @@
+define fooresource(
+ $color,
+ $type,
+ $somenumber,
+ $map
+ ) {
+
+ validate_resource()
+
+ # ... do something ...
+
+}
+
+fooresource { "example1":
+ color => "blue",
+ type => "circle",
+ somenumber => 5,
+ map => {
+ a => 1,
+ b => 2,
+ c => 3,
+ }
+}
diff --git a/examples/validate_resource-1.schema b/examples/validate_resource-1.schema
new file mode 100644
index 0000000..c540db5
--- /dev/null
+++ b/examples/validate_resource-1.schema
@@ -0,0 +1,39 @@
+type: map
+mapping:
+ "title":
+ type: str
+ required: yes
+ "name":
+ type: str
+ required: yes
+ "caller_module_name":
+ type: str
+ required: yes
+ "module_name":
+ type: str
+ required: yes
+ "color":
+ type: str
+ required: yes
+ "type":
+ type: str
+ required: yes
+ "somenumber":
+ type: str
+ required: yes
+ pattern: /^\d+$/
+ "map":
+ type: map
+ mapping:
+ "a":
+ type: str
+ required: yes
+ pattern: /^\d+$/
+ "b":
+ type: str
+ required: yes
+ pattern: /^\d+$/
+ "c":
+ type: str
+ required: yes
+ pattern: /^\d+$/
diff --git a/examples/validate_resource-2.pp b/examples/validate_resource-2.pp
new file mode 100644
index 0000000..b53b109
--- /dev/null
+++ b/examples/validate_resource-2.pp
@@ -0,0 +1,17 @@
+class foo (
+ $a,
+ $b,
+ $c
+ ) {
+
+ validate_resource()
+
+ # ... do something ...
+
+}
+
+class { "foo":
+ a => "1",
+ b => "foobaz",
+ c => ['a','b','c']
+}
diff --git a/examples/validate_resource-2.schema b/examples/validate_resource-2.schema
new file mode 100644
index 0000000..b516945
--- /dev/null
+++ b/examples/validate_resource-2.schema
@@ -0,0 +1,26 @@
+type: map
+mapping:
+ "title":
+ type: str
+ required: yes
+ "name":
+ type: str
+ required: yes
+ "caller_module_name":
+ type: str
+ required: yes
+ "module_name":
+ type: str
+ required: yes
+ "a":
+ type: str
+ required: yes
+ "b":
+ type: str
+ required: yes
+ pattern: /^foo/
+ "c":
+ type: seq
+ required: yes
+ sequence:
+ - type: str
diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb
new file mode 100644
index 0000000..bbb5a54
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_resource.rb
@@ -0,0 +1,36 @@
+#
+# validate_resource
+#
+
+module Puppet::Parser::Functions
+ newfunction(:validate_resource, :type => :statement, :doc => <<-EOS
+ EOS
+ ) do |arguments|
+
+ require 'kwalify'
+
+ if (arguments.size != 0) then
+ raise(Puppet::ParseError, "validate_resource(): Wrong number of arguments "+
+ "given #{arguments.size} for 0")
+ end
+
+
+ classhash = to_hash(recursive=false)
+ sourcepath = source.file
+ schemapath = sourcepath.gsub(/\.(rb|pp)$/, ".schema")
+ schema = Kwalify::Yaml.load_file(schemapath)
+ validator = Kwalify::Validator.new(schema)
+ errors = validator.validate(classhash)
+
+ if errors && !errors.empty?
+ error_output = "Resource validation failed:\n"
+ for e in errors
+ error_output += "[#{e.path}] #{e.message}\n"
+ end
+ raise(Puppet::ParseError, error_output)
+ end
+
+ end
+end
+
+# vim: set ts=2 sw=2 et :