X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FJSONSchema.pm;h=15e2c3c5bda6d9c34e6288684dad421628b2e59a;hp=e84c661cbd01e19182dd604d33c5b6024d2b62c9;hb=50ae94c97b77ad6352957b9be32686572f205031;hpb=2d468b1aa2de0a6d4029cb072b1c1fec36adefed diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm index e84c661..15e2c3c 100644 --- a/src/PVE/JSONSchema.pm +++ b/src/PVE/JSONSchema.pm @@ -137,7 +137,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"; } @@ -248,7 +248,7 @@ register_format('CIDRv4', \&pve_verify_cidrv4); sub pve_verify_cidrv4 { my ($cidr, $noerr) = @_; - if ($cidr =~ m!^(?:$IPV4RE)(?:/(\d+))$! && ($1 > 7) && ($1 < 32)) { + if ($cidr =~ m!^(?:$IPV4RE)(?:/(\d+))$! && ($1 > 7) && ($1 <= 32)) { return $cidr; } @@ -488,7 +488,10 @@ sub format_size { }; sub parse_property_string { - my ($format, $data, $path) = @_; + my ($format, $data, $path, $additional_properties) = @_; + + # In property strings we default to not allowing additional properties + $additional_properties = 0 if !defined($additional_properties); my $default_key; @@ -500,6 +503,10 @@ 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 $alias = $schema->{alias}) { + $k = $alias; + $schema = $format->{$k}; + } die "invalid key in comma-separated list property: $k\n" if !$schema; if ($schema->{type} && $schema->{type} eq 'boolean') { $v = 1 if $v =~ m/^(1|on|yes|true)$/i; @@ -524,7 +531,7 @@ sub parse_property_string { } my $errors = {}; - check_object($path, $format, $res, undef, $errors); + check_object($path, $format, $res, $additional_properties, $errors); if (scalar(%$errors)) { raise "format error\n", errors => $errors; } @@ -553,7 +560,7 @@ sub print_property_string { my %required; # this is a set, all present keys are required regardless of value foreach my $key (keys %$format) { $allowed{$key} = 1; - if (!$format->{$key}->{optional} && !$skipped{$key}) { + if (!$format->{$key}->{optional} && !$format->{$key}->{alias} && !$skipped{$key}) { $required{$key} = 1; } @@ -578,15 +585,16 @@ sub print_property_string { } foreach my $key (sort keys %$data) { - die "invalid key: $key" if !$allowed{$key}; delete $required{$key}; next if $skipped{$key}; + die "invalid key: $key" if !$allowed{$key}; - my $type = $format->{$key}->{type}; + my $typeformat = $format->{$key}->{format}; my $value = $data->{$key}; + next if !defined($value); $text .= $comma; $comma = ','; - if ($type eq 'disk-size') { + if ($typeformat && $typeformat eq 'disk-size') { $text .= "$key=" . format_size($value); } else { $text .= "$key=$value"; @@ -810,7 +818,7 @@ sub check_prop { if (!defined ($value)) { return if $schema->{type} && $schema->{type} eq 'null'; - if (!$schema->{optional}) { + if (!$schema->{optional} && !$schema->{alias}) { add_error($errors, $path, "property is missing and it is not optional"); } return; @@ -1048,6 +1056,11 @@ my $default_schema_noref = { optional => 1, description => "Whether this is the default key in a comma separated list property string.", }, + alias => { + type => 'string', + 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.", + }, default => { type => "any", optional => 1,