]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/JSONSchema.pm
update changelog
[pve-common.git] / src / PVE / JSONSchema.pm
index 95529258f19c24535e09dae21dc54471da8b1d37..cde941e2f4592890e42abe170ed8d31649c879e9 100644 (file)
@@ -71,19 +71,19 @@ register_standard_option('pve-iface', {
     minLength => 2, maxLength => 20,
 });
 
-PVE::JSONSchema::register_standard_option('pve-storage-id', {
+register_standard_option('pve-storage-id', {
     description => "The storage identifier.",
     type => 'string', format => 'pve-storage-id',
 }); 
 
-PVE::JSONSchema::register_standard_option('pve-config-digest', {
+register_standard_option('pve-config-digest', {
     description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
     type => 'string',
     optional => 1,
     maxLength => 40, # sha1 hex digest lenght is 40
 });
 
-PVE::JSONSchema::register_standard_option('extra-args', {
+register_standard_option('extra-args', {
     description => "Extra arguments as array",
     type => 'array',
     items => { type => 'string' },
@@ -110,6 +110,16 @@ sub get_format {
 
 register_format('string', sub {}); # allow format => 'string-list'
 
+register_format('urlencoded', \&pve_verify_urlencoded);
+sub pve_verify_urlencoded {
+    my ($text, $noerr) = @_;
+    if ($text !~ /^[-%a-zA-Z0-9_.!~*'()]*$/) {
+       return undef if $noerr;
+       die "invalid urlencoded string: $text\n";
+    }
+    return $text;
+}
+
 register_format('pve-configid', \&pve_verify_configid);
 sub pve_verify_configid {
     my ($id, $noerr) = @_;
@@ -137,7 +147,7 @@ register_format('pve-vmid', \&pve_verify_vmid);
 sub pve_verify_vmid {
     my ($vmid, $noerr) = @_;
 
-    if ($vmid !~ m/^[1-9][0-9]+$/) {
+    if ($vmid !~ m/^[1-9][0-9]{2,8}$/) {
        return undef if $noerr;
        die "value does not look like a valid VM ID\n";
     }
@@ -503,6 +513,12 @@ sub parse_property_string {
            my ($k, $v) = ($1, $2);
            die "duplicate key in comma-separated list property: $k\n" if defined($res->{$k});
            my $schema = $format->{$k};
+           if (my $group = $schema->{group}) {
+               die "keys $res->{$group} and $k are part of the same group and cannot be used together\n"
+                   if defined($res->{$group});
+               $res->{$group} = $k;
+               $schema = $format->{$group};
+           }
            if (my $alias = $schema->{alias}) {
                $k = $alias;
                $schema = $format->{$k};
@@ -525,6 +541,7 @@ sub parse_property_string {
                    die "duplicate key in comma-separated list property: $default_key\n";
                }
            }
+           die "value without key, but schema does not define a default key\n" if !$default_key;
        } else {
            die "missing key in comma-separated list property\n";
        }
@@ -544,7 +561,7 @@ sub print_property_string {
 
     if (ref($format) ne 'HASH') {
        my $schema = $format_list->{$format};
-       die "not a valid format: $format" if !$schema;
+       die "not a valid format: $format\n" if !$schema;
        $format = $schema;
     }
 
@@ -558,16 +575,27 @@ sub print_property_string {
     my %skipped = map { $_ => 1 } @$skip;
     my %allowed;
     my %required; # this is a set, all present keys are required regardless of value
+    my %group_for_key;
     foreach my $key (keys %$format) {
        $allowed{$key} = 1;
-       if (!$format->{$key}->{optional} && !$format->{$key}->{alias} && !$skipped{$key}) {
+       my $keyfmt = $format->{$key};
+       my $group = $keyfmt->{group};
+       if (defined($group)) {
+           $skipped{$group} = 1;
+           if (defined(my $grpalias = $format->{$group}->{alias})) {
+               $group_for_key{$grpalias} = $group;
+           } else {
+               $group_for_key{$key} = $group;
+           }
+       }
+       if (!$keyfmt->{optional} && !$keyfmt->{alias} && !defined($group) && !$skipped{$key}) {
            $required{$key} = 1;
        }
 
        # Skip default keys
-       if ($format->{$key}->{default_key}) {
+       if ($keyfmt->{default_key}) {
            if ($default_key) {
-               warn "multiple default keys in schema ($default_key, $key)";
+               warn "multiple default keys in schema ($default_key, $key)\n";
            } else {
                $default_key = $key;
                $skipped{$key} = 1;
@@ -576,7 +604,7 @@ sub print_property_string {
     }
 
     my ($text, $comma);
-    if ($default_key) {
+    if ($default_key && !defined($format->{$default_key}->{alias})) {
        $text = "$data->{$default_key}";
        $comma = ',';
     } else {
@@ -587,22 +615,27 @@ sub print_property_string {
     foreach my $key (sort keys %$data) {
        delete $required{$key};
        next if $skipped{$key};
-       die "invalid key: $key" if !$allowed{$key};
+       die "invalid key: $key\n" if !$allowed{$key};
 
-       my $typeformat = $format->{$key}->{format};
+       my $keyfmt = $format->{$key};
+       my $typeformat = $keyfmt->{format};
        my $value = $data->{$key};
        next if !defined($value);
+       if (my $group = $group_for_key{$key}) {
+           $key = $data->{$group};
+       }
        $text .= $comma;
        $comma = ',';
        if ($typeformat && $typeformat eq 'disk-size') {
            $text .= "$key=" . format_size($value);
        } else {
+           die "illegal value with commas for $key\n" if $value =~ /,/;
            $text .= "$key=$value";
        }
     }
 
     if (my $missing = join(',', keys %required)) {
-       die "missing properties: $missing";
+       die "missing properties: $missing\n";
     }
 
     return $text;
@@ -752,8 +785,31 @@ sub check_object {
        return;
     }
 
+    my %groups;
+    foreach my $k (keys %$schema) {
+       if (defined(my $group = $schema->{$k}->{group})) {
+           # When a group is aliased then the key/value pair will match the
+           # schema, but if it's not then the group key contains the key-name
+           # which will not match the group key's defined schema and we have
+           # to match it against that...
+           if (!defined($schema->{$group}->{alias})) {
+               $groups{$group} = 1;
+           }
+       }
+    }
     foreach my $k (keys %$schema) {
-       check_prop($value->{$k}, $schema->{$k}, $path ? "$path.$k" : $k, $errors);
+       my $orig_key = $k;
+       my $v;
+       if ($groups{$k}) {
+           if (defined($orig_key = $value->{$k})) {
+               $v = $value->{$orig_key};
+           } else {
+               $orig_key = $k; # now only used for the 'path' parameter
+           }
+       } else {
+           $v = $value->{$k};
+       }
+       check_prop($v, $schema->{$k}, $path ? "$path.$orig_key" : $orig_key, $errors);
     }
 
     foreach my $k (keys %$value) {
@@ -818,7 +874,7 @@ sub check_prop {
 
     if (!defined ($value)) {
        return if $schema->{type} && $schema->{type} eq 'null';
-       if (!$schema->{optional} && !$schema->{alias}) {
+       if (!$schema->{optional} && !$schema->{alias} && !$schema->{group}) {
            add_error($errors, $path, "property is missing and it is not optional");
        }
        return;
@@ -1016,11 +1072,10 @@ my $default_schema_noref = {
        pattern => {
            type => "string",
            format => "regex",
-           description => "When the instance value is a string, this provides a regular expression that a instance string value should match in order to be valid.",
+           description => "When the instance value is a string, this provides a regular expression that a instance string value should match in order to be valid.",
            optional => 1,
            default => ".*",
-        },
-
+       },
        enum => {
            type => "array",
            optional => 1,
@@ -1036,21 +1091,21 @@ my $default_schema_noref = {
            optional => 1,
            description => "This provides a shorter (usually just one word) description for a property used to generate descriptions for comma separated list property strings.",
        },
-        title => {
-           type => "string",
+       title => {
+           type => "string",
            optional => 1,
-           description => "This provides the title of the property",
-        },
-        requires => {
-           type => [ "string", "object" ],
+           description => "This provides the title of the property",
+       },
+       requires => {
+           type => [ "string", "object" ],
            optional => 1,
-           description => "indicates a required property or a schema that must be validated if this property is present",
-        },
-        format => {
+           description => "indicates a required property or a schema that must be validated if this property is present",
+       },
+       format => {
            type => [ "string", "object" ],
            optional => 1,
-           description => "This indicates what format the data is among some predefined formats which may include:\n\ndate - a string following the ISO format \naddress \nschema - a schema definition object \nperson \npage \nhtml - a string representing HTML",
-        },
+           description => "This indicates what format the data is among some predefined formats which may include:\n\ndate - a string following the ISO format \naddress \nschema - a schema definition object \nperson \npage \nhtml - a string representing HTML",
+       },
        default_key => {
            type => "boolean",
            optional => 1,
@@ -1061,50 +1116,55 @@ my $default_schema_noref = {
            optional => 1,
            description => "When a key represents the same property as another it can be an alias to it, causing the parsed datastructure to use the other key to store the current value under.",
        },
+       group => {
+           type => 'string',
+           optional => 1,
+           description => "If a key is part of a group then setting it will additionally set the group name in the resulting data structure to the key used to fill the group. Only one key of a group can be assigned.",
+       },
        default => {
            type => "any",
            optional => 1,
            description => "This indicates the default for the instance property."
        },
-        completion => {
+       completion => {
            type => 'coderef',
            description => "Bash completion function. This function should return a list of possible values.",
            optional => 1,
-        },
-        disallow => {
-           type => "object",
+       },
+       disallow => {
+           type => "object",
            optional => 1,
-           description => "This attribute may take the same values as the \"type\" attribute, however if the instance matches the type or if this value is an array and the instance matches any type or schema in the array, than this instance is not valid.",
+           description => "This attribute may take the same values as the \"type\" attribute, however if the instance matches the type or if this value is an array and the instance matches any type or schema in the array, then this instance is not valid.",
        },
-        extends => {
-           type => "object",
+       extends => {
+           type => "object",
            optional => 1,
-           description => "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also.",
+           description => "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also.",
            default => {},
-        },
-        # this is from hyper schema
-        links => {
-            type => "array",
-            description => "This defines the link relations of the instance objects",
-           optional => 1,
+       },
+       # this is from hyper schema
+       links => {
+           type => "array",
+           description => "This defines the link relations of the instance objects",
+           optional => 1,
            items => {
-               type => "object",
-               properties => {
-                   href => {
-                       type => "string",
-                       description => "This defines the target URL for the relation and can be parameterized using {propertyName} notation. It should be resolved as a URI-reference relative to the URI that was used to retrieve the instance document",
-                   },
-                   rel => {
-                       type => "string",
-                       description => "This is the name of the link relation",
-                       optional => 1,
-                       default => "full",
-                   },
+               type => "object",
+               properties => {
+                   href => {
+                       type => "string",
+                       description => "This defines the target URL for the relation and can be parameterized using {propertyName} notation. It should be resolved as a URI-reference relative to the URI that was used to retrieve the instance document",
+                   },
+                   rel => {
+                       type => "string",
+                       description => "This is the name of the link relation",
+                       optional => 1,
+                       default => "full",
+                   },
                    method => {
-                       type => "string",
-                       description => "For submission links, this defines the method that should be used to access the target resource",
-                       optional => 1,
-                       default => "GET",
+                       type => "string",
+                       description => "For submission links, this defines the method that should be used to access the target resource",
+                       optional => 1,
+                       default => "GET",
                    },
                },
            },