## 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.
+TODO
+++ /dev/null
-$schema = {
- 'type' => 'seq',
- 'sequence' => [
- { 'type' => 'str', 'enum' => ['asdf','fdsa'] }
- ]
-}
-$document = ['a', 'b', 'c']
-
-kwalify($schema, $document)
+++ /dev/null
-$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)
+++ /dev/null
-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,
- }
-}
+++ /dev/null
-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+$/
+++ /dev/null
-class foo (
- $a,
- $b,
- $c
- ) {
-
- validate_resource()
-
- # ... do something ...
-
-}
-
-class { "foo":
- a => "1",
- b => "foobaz",
- c => ['a','b','c']
-}
+++ /dev/null
-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
+++ /dev/null
-#
-# kwalify.rb
-#
-
-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]
-
- require 'kwalify'
-
- 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 :
+++ /dev/null
-#
-# validate_resource
-#
-
-module Puppet::Parser::Functions
- newfunction(:validate_resource, :type => :statement, :doc => <<-EOS
-This function when placed at the beginning of a class, will go looking for a
-valid kwalify schema by replacing the extension of the file with '.schema'.
-
-It will then validate the arguments passed to the function using that kwalify
-schema.
- 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 :
+++ /dev/null
-#!/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',
- }
- @scope.function_kwalify([schema, document])
- end
-
-end