use base 'Exporter';
our @EXPORT_OK = qw(
-register_standard_option
get_standard_option
+parse_property_string
+register_standard_option
);
# Note: This class implements something similar to JSON schema, but it is not 100% complete.
});
my $format_list = {};
+my $format_validators = {};
sub register_format {
- my ($format, $code) = @_;
+ my ($name, $format, $validator) = @_;
- die "JSON schema format '$format' already registered\n"
- if $format_list->{$format};
+ die "JSON schema format '$name' already registered\n"
+ if $format_list->{$name};
- $format_list->{$format} = $code;
+ if ($validator) {
+ die "A \$validator function can only be specified for hash-based formats\n"
+ if ref($format) ne 'HASH';
+ $format_validators->{$name} = $validator;
+ }
+
+ $format_list->{$name} = $format;
}
sub get_format {
- my ($format) = @_;
- return $format_list->{$format};
+ my ($name) = @_;
+ return $format_list->{$name};
}
my $renderer_hash = {};
sub pve_verify_configid {
my ($id, $noerr) = @_;
- if ($id !~ m/^[a-z][a-z0-9_]+$/i) {
+ if ($id !~ m/^[a-z][a-z0-9_-]+$/i) {
return undef if $noerr;
die "invalid configuration ID '$id'\n";
}
} elsif ($entry =~ m/^([^:]+):([^:]+)$/) {
my ($source, $target) = ($1, $2);
eval {
- PVE::JSONSchema::check_format($idformat, $source, '');
- PVE::JSONSchema::check_format($idformat, $target, '');
+ check_format($idformat, $source, '');
+ check_format($idformat, $target, '');
};
- die "entry '$entry' contains invalid ID - $@\n"
- if $@;
+ die "entry '$entry' contains invalid ID - $@\n" if $@;
die "duplicate mapping for source '$source'\n"
- if $map->{entries}->{$source};
+ if exists $map->{entries}->{$source};
$map->{entries}->{$source} = $target;
} else {
eval {
- PVE::JSONSchema::check_format($idformat, $entry);
+ check_format($idformat, $entry);
};
-
- die "entry '$entry' contains invalid ID - $@\n"
- if $@;
+ die "entry '$entry' contains invalid ID - $@\n" if $@;
die "default target ID can only be provided once\n"
- if $map->{default};
+ if exists $map->{default};
$map->{default} = $entry;
}
}
die "identity mapping cannot be combined with other mappings\n"
- if $map->{identity} && ($map->{default} || $map->{entries});
+ if $map->{identity} && ($map->{default} || exists $map->{entries});
return $map;
}
return $name;
}
+register_format('timezone', \&pve_verify_timezone);
+sub pve_verify_timezone {
+ my ($timezone, $noerr) = @_;
+
+ return $timezone if $timezone eq 'UTC';
+
+ open(my $fh, "<", "/usr/share/zoneinfo/zone.tab");
+ while (my $line = <$fh>) {
+ next if $line =~ /^\s*#/;
+ chomp $line;
+ my $zone = (split /\t/, $line)[2];
+ return $timezone if $timezone eq $zone; # found
+ }
+ close $fh;
+
+ return undef if $noerr;
+ die "invalid time zone '$timezone'\n";
+}
+
# network interface name
register_format('pve-iface', \&pve_verify_iface);
sub pve_verify_iface {
sub check_format {
my ($format, $value, $path) = @_;
- return parse_property_string($format, $value, $path) if ref($format) eq 'HASH';
- return if $format eq 'regex';
+ if (ref($format) eq 'HASH') {
+ # hash ref cannot have validator/list/opt handling attached
+ return parse_property_string($format, $value, $path);
+ }
+
+ if (ref($format) eq 'CODE') {
+ # we are the (sole, old-style) validator
+ return $format->($value);
+ }
- if ($format =~ m/^(.*)-a?list$/) {
+ return if $format eq 'regex';
- my $code = $format_list->{$1};
+ my $parsed;
+ $format =~ m/^(.*?)(?:-a?(list|opt))?$/;
+ my ($format_name, $format_type) = ($1, $2 // 'none');
+ my $registered = get_format($format_name);
+ die "undefined format '$format'\n" if !$registered;
- die "undefined format '$format'\n" if !$code;
+ die "'-$format_type' format must have code ref, not hash\n"
+ if $format_type ne 'none' && ref($registered) ne 'CODE';
+ if ($format_type eq 'list') {
# Note: we allow empty lists
foreach my $v (split_list($value)) {
- &$code($v);
+ $parsed = $registered->($v);
}
-
- } elsif ($format =~ m/^(.*)-opt$/) {
-
- my $code = $format_list->{$1};
-
- die "undefined format '$format'\n" if !$code;
-
- return if !$value; # allow empty string
-
- &$code($value);
-
+ } elsif ($format_type eq 'opt') {
+ $parsed = $registered->($value) if $value;
} else {
-
- my $code = $format_list->{$format};
-
- die "undefined format '$format'\n" if !$code;
-
- return parse_property_string($code, $value, $path) if ref($code) eq 'HASH';
- &$code($value);
+ if (ref($registered) eq 'HASH') {
+ # Note: this is the only case where a validator function could be
+ # attached, hence it's safe to handle that in parse_property_string.
+ # We do however have to call it with $format_name instead of
+ # $registered, so it knows about the name (and thus any validators).
+ $parsed = parse_property_string($format, $value, $path);
+ } else {
+ $parsed = $registered->($value);
+ }
}
+
+ return $parsed;
}
sub parse_size {
$additional_properties = 0 if !defined($additional_properties);
# Support named formats here, too:
+ my $validator;
if (!ref($format)) {
- if (my $desc = $format_list->{$format}) {
- $format = $desc;
+ if (my $reg = get_format($format)) {
+ die "parse_property_string only accepts hash based named formats\n"
+ if ref($reg) ne 'HASH';
+
+ # named formats can have validators attached
+ $validator = $format_validators->{$format};
+
+ $format = $reg;
} else {
die "unknown format: $format\n";
}
raise "format error\n", errors => $errors;
}
+ return $validator->($res) if $validator;
return $res;
}
sub print_property_string {
my ($data, $format, $skip, $path) = @_;
+ my $validator;
if (ref($format) ne 'HASH') {
my $schema = get_format($format);
die "not a valid format: $format\n" if !$schema;
+ # named formats can have validators attached
+ $validator = $format_validators->{$format};
$format = $schema;
}
raise "format error", errors => $errors;
}
+ $data = $validator->($data) if $validator;
+
my ($default_key, $keyAliasProps) = &$find_schema_default_key($format);
my $res = '';