summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown29
-rw-r--r--lib/puppet/parser/functions/kwalify.rb35
-rwxr-xr-xspec/unit/parser/functions/kwalify_spec.rb61
3 files changed, 125 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index ec4267c..68e559e 100644
--- a/README.markdown
+++ b/README.markdown
@@ -17,3 +17,32 @@ From github, download the module into your modulepath on your Puppetmaster. If y
puppet --configprint modulepath
Depending on the version of Puppet, you may need to restart the puppetmasterd (or Apache) process before the functions will work.
+
+## Functions
+
+### kwalify
+
+This function allows you to validate Puppet data structures using Kwalify
+schemas as documented here:
+
+http://www.kuwata-lab.com/kwalify/ruby/users-guide.01.html
+
+To validate, create a schema in Puppet:
+
+ $schema = {
+ 'type' => 'seq',
+ 'sequence' => [
+ { 'type' => 'str' }
+ ]
+ }
+
+And create some content that you want validated:
+
+ $document = ['a', 'b', 'c']
+
+And then use the function to validate:
+
+ kwalify($schema, $document)
+
+The function will throw an error and list all validation errors if there is a
+problem otherwise it succeeds silently.
diff --git a/lib/puppet/parser/functions/kwalify.rb b/lib/puppet/parser/functions/kwalify.rb
new file mode 100644
index 0000000..7238f84
--- /dev/null
+++ b/lib/puppet/parser/functions/kwalify.rb
@@ -0,0 +1,35 @@
+#
+# kwalify.rb
+#
+
+require 'kwalify'
+
+module Puppet::Parser::Functions
+ newfunction(:kwalify, :type => :statement, :doc => <<-EOS
+This function uses kwalify to validate Puppet data structures against Kwalify
+schemas.
+ EOS
+ ) do |args|
+
+ raise(Puppet::ParseError, "kwalify(): Wrong number of arguments " +
+ "given (#{args.size} for 2)") if args.size != 2
+
+ schema = args[0]
+ document = args[1]
+
+ validator = Kwalify::Validator.new(schema)
+
+ errors = validator.validate(document)
+
+ if errors && !errors.empty?
+ error_out = []
+ for e in errors
+ error_out << "[#{e.path}] #{e.message}"
+ end
+ raise(Puppet::ParseError, "Failed kwalify schema validation:\n" + error_out.join("\n"))
+ end
+
+ end
+end
+
+# vim: set ts=2 sw=2 et :
diff --git a/spec/unit/parser/functions/kwalify_spec.rb b/spec/unit/parser/functions/kwalify_spec.rb
new file mode 100755
index 0000000..b2afa12
--- /dev/null
+++ b/spec/unit/parser/functions/kwalify_spec.rb
@@ -0,0 +1,61 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+
+describe "the kwalify function" do
+ before :all do
+ Puppet::Parser::Functions.autoloader.loadall
+ end
+
+ before :each do
+ @scope = Puppet::Parser::Scope.new
+ end
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("kwalify").should == "function_kwalify"
+ end
+
+ it "should raise a ParseError if there is less than 2 arguments" do
+ lambda { @scope.function_kwalify([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should validate a simple array schema" do
+ schema = {
+ 'type' => 'seq',
+ 'sequence' => [
+ { 'type' => 'str' }
+ ]
+ }
+ document = ['a','b','c']
+ @scope.function_kwalify([schema, document])
+ end
+
+ it "should not validate a simple array schema when invalid" do
+ schema = {
+ 'type' => 'seq',
+ 'sequence' => [
+ { 'type' => 'str' }
+ ]
+ }
+ document = ['a','b',{'a' => 'b'}]
+ lambda { @scope.function_kwalify([schema, document]) }.should(raise_error(Puppet::ParseError))
+ end
+
+ it "should validate a hash schema" do
+ schema = {
+ 'type' => 'map',
+ 'mapping' => {
+ 'key1' => {
+ 'type' => 'str',
+ },
+ 'key2' => {
+ 'type' => 'str',
+ },
+ }
+ }
+ document = {
+ 'key1' => 'b',
+ 'key2' => 'c',
+ }
+ end
+
+end