mirror of
https://git.proxmox.com/git/pve-common
synced 2025-08-08 20:16:02 +00:00
JSONSchema: add parse_boolean helper
This commit is contained in:
parent
9a42d8a201
commit
1b71e56485
@ -508,6 +508,13 @@ sub format_size {
|
|||||||
return "${tb}T";
|
return "${tb}T";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
sub parse_boolean {
|
||||||
|
my ($bool) = @_;
|
||||||
|
return 1 if $bool =~ m/^(1|on|yes|true)$/i;
|
||||||
|
return 0 if $bool =~ m/^(0|off|no|false)$/i;
|
||||||
|
return undef;
|
||||||
|
}
|
||||||
|
|
||||||
sub parse_property_string {
|
sub parse_property_string {
|
||||||
my ($format, $data, $path, $additional_properties) = @_;
|
my ($format, $data, $path, $additional_properties) = @_;
|
||||||
|
|
||||||
@ -546,8 +553,7 @@ sub parse_property_string {
|
|||||||
|
|
||||||
die "invalid key in comma-separated list property: $k\n" if !$schema;
|
die "invalid key in comma-separated list property: $k\n" if !$schema;
|
||||||
if ($schema->{type} && $schema->{type} eq 'boolean') {
|
if ($schema->{type} && $schema->{type} eq 'boolean') {
|
||||||
$v = 1 if $v =~ m/^(1|on|yes|true)$/i;
|
$v = parse_boolean($v) // $v;
|
||||||
$v = 0 if $v =~ m/^(0|off|no|false)$/i;
|
|
||||||
}
|
}
|
||||||
$res->{$k} = $v;
|
$res->{$k} = $v;
|
||||||
} elsif ($part !~ /=/) {
|
} elsif ($part !~ /=/) {
|
||||||
@ -1378,10 +1384,8 @@ sub get_options {
|
|||||||
if ($pd->{type} eq 'boolean') {
|
if ($pd->{type} eq 'boolean') {
|
||||||
if ($opts->{$p} eq '') {
|
if ($opts->{$p} eq '') {
|
||||||
$opts->{$p} = 1;
|
$opts->{$p} = 1;
|
||||||
} elsif ($opts->{$p} =~ m/^(1|true|yes|on)$/i) {
|
} elsif (defined(my $bool = parse_boolean($opts->{$p}))) {
|
||||||
$opts->{$p} = 1;
|
$opts->{$p} = $bool;
|
||||||
} elsif ($opts->{$p} =~ m/^(0|false|no|off)$/i) {
|
|
||||||
$opts->{$p} = 0;
|
|
||||||
} else {
|
} else {
|
||||||
raise("unable to parse boolean option\n", code => HTTP_BAD_REQUEST);
|
raise("unable to parse boolean option\n", code => HTTP_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
@ -1434,8 +1438,7 @@ sub parse_config {
|
|||||||
if ($schema->{properties}->{$key} &&
|
if ($schema->{properties}->{$key} &&
|
||||||
$schema->{properties}->{$key}->{type} eq 'boolean') {
|
$schema->{properties}->{$key}->{type} eq 'boolean') {
|
||||||
|
|
||||||
$value = 1 if $value =~ m/^(1|on|yes|true)$/i;
|
$value = parse_boolean($value) // $value;
|
||||||
$value = 0 if $value =~ m/^(0|off|no|false)$/i;
|
|
||||||
}
|
}
|
||||||
$cfg->{$key} = $value;
|
$cfg->{$key} = $value;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user