[pve-devel] r4995 - pve-common/trunk

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Aug 13 10:17:58 CEST 2010


Author: dietmar
Date: 2010-08-13 08:17:57 +0000 (Fri, 13 Aug 2010)
New Revision: 4995

Modified:
   pve-common/trunk/ChangeLog
   pve-common/trunk/JSONSchema.pm
Log:
* JSONSchema.pm (register_format): implement a way to register
	'format' verification methods.



Modified: pve-common/trunk/ChangeLog
===================================================================
--- pve-common/trunk/ChangeLog	2010-08-13 07:43:36 UTC (rev 4994)
+++ pve-common/trunk/ChangeLog	2010-08-13 08:17:57 UTC (rev 4995)
@@ -1,3 +1,8 @@
+2010-08-13  Proxmox Support Team  <support at proxmox.com>
+
+	* JSONSchema.pm (register_format): implement a way to register
+	'format' verification methods.
+
 2010-08-12  Proxmox Support Team  <support at proxmox.com>
 
 	* RESTHandler.pm (AUTOLOAD): cache autoload

Modified: pve-common/trunk/JSONSchema.pm
===================================================================
--- pve-common/trunk/JSONSchema.pm	2010-08-13 07:43:36 UTC (rev 4994)
+++ pve-common/trunk/JSONSchema.pm	2010-08-13 08:17:57 UTC (rev 4995)
@@ -13,8 +13,26 @@
 
 # the code is similar to the javascript parser from http://code.google.com/p/jsonschema/
 
-# fixme: implement 'format' property 
+my $format_list = {};
 
+sub register_format {
+    my ($format, $code) = @_;
+
+    die "JSON schema format '$format' already registered\n" if $format_list->{$format};
+
+    $format_list->{$format} = $code;
+}
+
+sub check_format {
+    my ($format, $value) = @_;
+
+    my $code = $format_list->{$format};
+
+    die "undefined format '$format'\n" if !$code;
+
+    &$code($value);
+} 
+
 sub add_error {
     my ($errors, $path, $msg) = @_;
 
@@ -250,6 +268,14 @@
 
     } else {
 
+	if (my $format = $schema->{format}) {
+	    eval { check_format($format, $value); };
+	    if ($@) {
+		add_error($errors, $path, "invalid format - $@");
+		return;
+	    }
+	}
+
 	if (my $pattern = $schema->{pattern}) {
 	    if ($value !~ m/^$pattern$/) {
 		add_error($errors, $path, "value does not match the regex pattern");




More information about the pve-devel mailing list