Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/226921

Change subject: wmflib: validate_array_re( array $items, string $re )
......................................................................

wmflib: validate_array_re( array $items, string $re )

Throw an error if any member of $items does not match the regular
expression $re. Examples:

  # OK -- each array item is a four-digit number.
  validate_array_re([8123, 8124, 8125], '^\d{4}$')

  # Fail -- last array item is not a four-digit number.
  validate_array_re([8123, 8124, 812], '^\d{4}$')

Change-Id: I9feca0df99ae42eeb852f84f4eacf6f6e6901f72
---
M modules/wmflib/README.md
A modules/wmflib/lib/puppet/parser/functions/validate_array_re.rb
2 files changed, 38 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/21/226921/1

diff --git a/modules/wmflib/README.md b/modules/wmflib/README.md
index 7b1d9ab..3ffd781 100644
--- a/modules/wmflib/README.md
+++ b/modules/wmflib/README.md
@@ -298,8 +298,22 @@
     to_seconds('2 days')  # 172800
 
 
-## validate_ensure
+## validate_array_re
+`validate_array_re( array $items, string $re )`
 
+Throw an error if any member of $items does not match the regular
+expression $re.
+
+### Examples
+
+    # OK -- each array item is a four-digit number.
+    validate_array_re([8123, 8124, 8125], '^\d{4}$')
+
+    # Fail -- last array item is not a four-digit number.
+    validate_array_re([8123, 8124, 812], '^\d{4}$')
+
+
+## validate_ensure
 `validate_ensure( string $ensure )`
 
 Throw an error if the $ensure argument is not 'present' or 'absent'.
diff --git a/modules/wmflib/lib/puppet/parser/functions/validate_array_re.rb 
b/modules/wmflib/lib/puppet/parser/functions/validate_array_re.rb
new file mode 100644
index 0000000..22d1e19
--- /dev/null
+++ b/modules/wmflib/lib/puppet/parser/functions/validate_array_re.rb
@@ -0,0 +1,23 @@
+# == Function: validate_array_re( array $items, string $re )
+#
+# Throw an error if any member of $items does not match the regular
+# expression $re.
+#
+# === Examples
+#
+#  # OK -- each array item is a four-digit number.
+#  validate_array_re([8123, 8124, 8125], '^\d{4}$')
+#
+#  # Fail -- last array item is not a four-digit number.
+#  validate_array_re([8123, 8124, 812], '^\d{4}$')
+#
+module Puppet::Parser::Functions
+  newfunction(:validate_array_re, :arity => 2) do |args|
+    items, re = args
+    re = Regexp.new(re)
+    invalid = args.first.find { |item| item.to_s !~ re }
+    unless invalid.nil?
+      fail(Puppet::ParseError, "Array element \"#{invalid}\" does not match 
regular expression \"#{re.source}\".")
+    end
+  end
+end

-- 
To view, visit https://gerrit.wikimedia.org/r/226921
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9feca0df99ae42eeb852f84f4eacf6f6e6901f72
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to