]>
git.proxmox.com Git - pve-common.git/blob - data/PVE/JSONSchema.pm
1 package PVE
:: JSONSchema
;
5 use Storable
; # for dclone
7 use Devel
:: Cycle
- quiet
; # todo: remove?
8 use PVE
:: Tools
qw(split_list) ;
9 use PVE
:: Exception
qw(raise) ;
10 use HTTP
:: Status
qw(:constants) ;
15 register_standard_option
19 # Note: This class implements something similar to JSON schema, but it is not 100% complete.
20 # see: http://tools.ietf.org/html/draft-zyp-json-schema-02
21 # see: http://json-schema.org/
23 # the code is similar to the javascript parser from http://code.google.com/p/jsonschema/
25 my $standard_options = {};
26 sub register_standard_option
{
27 my ( $name, $schema ) = @_ ;
29 die "standard option ' $name ' already registered \n "
30 if $standard_options ->{ $name };
32 $standard_options ->{ $name } = $schema ;
35 sub get_standard_option
{
36 my ( $name, $base ) = @_ ;
38 my $std = $standard_options ->{ $name };
39 die "no such standard option \n " if ! $std ;
41 my $res = $base || {};
43 foreach my $opt ( keys %$std ) {
45 $res ->{ $opt } = $std ->{ $opt };
51 register_standard_option
( 'pve-vmid' , {
52 description
=> "The (unique) ID of the VM." ,
53 type
=> 'integer' , format
=> 'pve-vmid' ,
57 register_standard_option
( 'pve-node' , {
58 description
=> "The cluster node name." ,
59 type
=> 'string' , format
=> 'pve-node' ,
62 register_standard_option
( 'pve-node-list' , {
63 description
=> "List of cluster node names." ,
64 type
=> 'string' , format
=> 'pve-node-list' ,
67 register_standard_option
( 'pve-iface' , {
68 description
=> "Network interface name." ,
69 type
=> 'string' , format
=> 'pve-iface' ,
70 minLength
=> 2 , maxLength
=> 20 ,
73 PVE
:: JSONSchema
:: register_standard_option
( 'pve-storage-id' , {
74 description
=> "The storage identifier." ,
75 type
=> 'string' , format
=> 'pve-storage-id' ,
82 my ( $format, $code ) = @_ ;
84 die "JSON schema format ' $format ' already registered \n "
85 if $format_list ->{ $format };
87 $format_list ->{ $format } = $code ;
90 # register some common type for pve
92 register_format
( 'string' , sub {}); # allow format => 'string-list'
94 register_format
( 'pve-configid' , \
& pve_verify_configid
);
95 sub pve_verify_configid
{
96 my ( $id, $noerr ) = @_ ;
98 if ( $id !~ m/^[a-z][a-z0-9_]+$/i ) {
99 return undef if $noerr ;
100 die "invalid cofiguration ID ' $id ' \n " ;
105 PVE
:: JSONSchema
:: register_format
( 'pve-storage-id' , \
& parse_storage_id
);
106 sub parse_storage_id
{
107 my ( $storeid, $noerr ) = @_ ;
109 if ( $storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i ) {
110 return undef if $noerr ;
111 die "storage ID ' $storeid ' contains illegal characters \n " ;
117 register_format
( 'pve-vmid' , \
& pve_verify_vmid
);
118 sub pve_verify_vmid
{
119 my ( $vmid, $noerr ) = @_ ;
121 if ( $vmid !~ m/^[1-9][0-9]+$/ ) {
122 return undef if $noerr ;
123 die "value does not look like a valid VM ID \n " ;
128 register_format
( 'pve-node' , \
& pve_verify_node_name
);
129 sub pve_verify_node_name
{
130 my ( $node, $noerr ) = @_ ;
132 if ( $node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)$/ ) {
133 return undef if $noerr ;
134 die "value does not look like a valid node name \n " ;
139 register_format
( 'ipv4' , \
& pve_verify_ipv4
);
140 sub pve_verify_ipv4
{
141 my ( $ipv4, $noerr ) = @_ ;
143 if ( $ipv4 !~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ ||
144 !(( $1 > 0 ) && ( $1 < 255 ) &&
145 ( $2 <= 255 ) && ( $3 <= 255 ) &&
146 ( $4 > 0 ) && ( $4 < 255 ))) {
147 return undef if $noerr ;
148 die "value does not look like a valid IP address \n " ;
152 register_format
( 'ipv4mask' , \
& pve_verify_ipv4mask
);
153 sub pve_verify_ipv4mask
{
154 my ( $mask, $noerr ) = @_ ;
156 if ( $mask !~ m/^255\.255\.(\d{1,3})\.(\d{1,3})$/ ||
157 !(( $1 <= 255 ) && ( $2 <= 255 ))) {
158 return undef if $noerr ;
159 die "value does not look like a valid IP netmask \n " ;
164 register_format
( 'email' , \
& pve_verify_email
);
165 sub pve_verify_email
{
166 my ( $email, $noerr ) = @_ ;
168 # we use same regex as extjs Ext.form.VTypes.email
169 if ( $email !~ /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/ ) {
170 return undef if $noerr ;
171 die "value does not look like a valid email address \n " ;
176 register_format
( 'dns-name' , \
& pve_verify_dns_name
);
177 sub pve_verify_dns_name
{
178 my ( $name, $noerr ) = @_ ;
180 my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)" ;
182 if ( $name !~ /^(${namere}\.)*${namere}$/ ) {
183 return undef if $noerr ;
184 die "value does not look like a valid DNS name \n " ;
189 # network interface name
190 register_format
( 'pve-iface' , \
& pve_verify_iface
);
191 sub pve_verify_iface
{
192 my ( $id, $noerr ) = @_ ;
194 if ( $id !~ m/^[a-z][a-z0-9_]{1,20}([:\.]\d+)?$/i ) {
195 return undef if $noerr ;
196 die "invalid network interface name ' $id ' \n " ;
202 my ( $format, $value ) = @_ ;
204 return if $format eq 'regex' ;
206 if ( $format =~ m/^(.*)-a?list$/ ) {
208 my $code = $format_list ->{ $1 };
210 die "undefined format ' $format ' \n " if ! $code ;
212 # Note: we allow empty lists
213 foreach my $v ( split_list
( $value )) {
217 } elsif ( $format =~ m/^(.*)-opt$/ ) {
219 my $code = $format_list ->{ $1 };
221 die "undefined format ' $format ' \n " if ! $code ;
223 return if ! $value ; # allow empty string
229 my $code = $format_list ->{ $format };
231 die "undefined format ' $format ' \n " if ! $code ;
238 my ( $errors, $path, $msg ) = @_ ;
240 $path = '_root' if ! $path ;
242 if ( $errors ->{ $path }) {
243 $errors ->{ $path } = join ( ' \n ' , $errors ->{ $path }, $msg );
245 $errors ->{ $path } = $msg ;
252 # see 'man perlretut'
253 return $value =~ /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/ ;
259 return $value =~ m/^[+-]?\d+$/ ;
263 my ( $path, $type, $value, $errors ) = @_ ;
267 if (! defined ( $value )) {
268 return 1 if $type eq 'null' ;
272 if ( my $tt = ref ( $type )) {
273 if ( $tt eq 'ARRAY' ) {
274 foreach my $t ( @$type ) {
276 check_type
( $path, $t, $value, $tmperr );
277 return 1 if ! scalar ( %$tmperr );
279 my $ttext = join ( '|' , @$type );
280 add_error
( $errors, $path, "type check (' $ttext ') failed" );
282 } elsif ( $tt eq 'HASH' ) {
284 check_prop
( $value, $type, $path, $tmperr );
285 return 1 if ! scalar ( %$tmperr );
286 add_error
( $errors, $path, "type check failed" );
289 die "internal error - got reference type ' $tt '" ;
294 return 1 if $type eq 'any' ;
296 if ( $type eq 'null' ) {
297 if ( defined ( $value )) {
298 add_error
( $errors, $path, "type check (' $type ') failed - value is not null" );
304 my $vt = ref ( $value );
306 if ( $type eq 'array' ) {
307 if (! $vt || $vt ne 'ARRAY' ) {
308 add_error
( $errors, $path, "type check (' $type ') failed" );
312 } elsif ( $type eq 'object' ) {
313 if (! $vt || $vt ne 'HASH' ) {
314 add_error
( $errors, $path, "type check (' $type ') failed" );
318 } elsif ( $type eq 'coderef' ) {
319 if (! $vt || $vt ne 'CODE' ) {
320 add_error
( $errors, $path, "type check (' $type ') failed" );
326 add_error
( $errors, $path, "type check (' $type ') failed - got $vt " );
329 if ( $type eq 'string' ) {
330 return 1 ; # nothing to check ?
331 } elsif ( $type eq 'boolean' ) {
332 #if ($value =~ m/^(1|true|yes|on)$/i) {
335 #} elsif ($value =~ m/^(0|false|no|off)$/i) {
336 } elsif ( $value eq '0' ) {
339 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
342 } elsif ( $type eq 'integer' ) {
343 if (! is_integer
( $value )) {
344 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
348 } elsif ( $type eq 'number' ) {
349 if (! is_number
( $value )) {
350 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
355 return 1 ; # no need to verify unknown types
365 my ( $path, $schema, $value, $additional_properties, $errors ) = @_ ;
367 # print "Check Object " . Dumper($value) . "\nSchema: " . Dumper($schema);
369 my $st = ref ( $schema );
370 if (! $st || $st ne 'HASH' ) {
371 add_error
( $errors, $path, "Invalid schema definition." );
375 my $vt = ref ( $value );
376 if (! $vt || $vt ne 'HASH' ) {
377 add_error
( $errors, $path, "an object is required" );
381 foreach my $k ( keys %$schema ) {
382 check_prop
( $value ->{ $k }, $schema ->{ $k }, $path ?
" $path . $k " : $k, $errors );
385 foreach my $k ( keys %$value ) {
387 my $newpath = $path ?
" $path . $k " : $k ;
389 if ( my $subschema = $schema ->{ $k }) {
390 if ( my $requires = $subschema ->{ requires
}) {
391 if ( ref ( $requires )) {
392 #print "TEST: " . Dumper($value) . "\n", Dumper($requires) ;
393 check_prop
( $value, $requires, $path, $errors );
394 } elsif (! defined ( $value ->{ $requires })) {
395 add_error
( $errors, $path ?
" $path . $requires " : $requires,
396 "missing property - ' $newpath ' requiers this property" );
400 next ; # value is already checked above
403 if ( defined ( $additional_properties ) && ! $additional_properties ) {
404 add_error
( $errors, $newpath, "property is not defined in schema " .
405 "and the schema does not allow additional properties" );
408 check_prop
( $value ->{ $k }, $additional_properties, $newpath, $errors )
409 if ref ( $additional_properties );
414 my ( $value, $schema, $path, $errors ) = @_ ;
416 die "internal error - no schema" if ! $schema ;
417 die "internal error" if ! $errors ;
419 #print "check_prop $path\n" if $value;
421 my $st = ref ( $schema );
422 if (! $st || $st ne 'HASH' ) {
423 add_error
( $errors, $path, "Invalid schema definition." );
427 # if it extends another schema, it must pass that schema as well
428 if ( $schema ->{ extends
}) {
429 check_prop
( $value, $schema ->{ extends
}, $path, $errors );
432 if (! defined ( $value )) {
433 return if $schema ->{ type
} && $schema ->{ type
} eq 'null' ;
434 if (! $schema ->{ optional
}) {
435 add_error
( $errors, $path, "property is missing and it is not optional" );
440 return if ! check_type
( $path, $schema ->{ type
}, $value, $errors );
442 if ( $schema ->{ disallow
}) {
444 if ( check_type
( $path, $schema ->{ disallow
}, $value, $tmperr )) {
445 add_error
( $errors, $path, "disallowed value was matched" );
450 if ( my $vt = ref ( $value )) {
452 if ( $vt eq 'ARRAY' ) {
453 if ( $schema ->{ items
}) {
454 my $it = ref ( $schema ->{ items
});
455 if ( $it && $it eq 'ARRAY' ) {
456 #die "implement me $path: $vt " . Dumper($schema) ."\n". Dumper($value);
457 die "not implemented" ;
460 foreach my $el ( @$value ) {
461 check_prop
( $el, $schema ->{ items
}, "${path}[ $ind ]" , $errors );
467 } elsif ( $schema ->{ properties
} || $schema ->{ additionalProperties
}) {
468 check_object
( $path, defined ( $schema ->{ properties
}) ?
$schema ->{ properties
} : {},
469 $value, $schema ->{ additionalProperties
}, $errors );
475 if ( my $format = $schema ->{ format
}) {
476 eval { check_format
( $format, $value ); };
478 add_error
( $errors, $path, "invalid format - $@ " );
483 if ( my $pattern = $schema ->{ pattern
}) {
484 if ( $value !~ m/^$pattern$/ ) {
485 add_error
( $errors, $path, "value does not match the regex pattern" );
490 if ( defined ( my $max = $schema ->{ maxLength
})) {
491 if ( length ( $value ) > $max ) {
492 add_error
( $errors, $path, "value may only be $max characters long" );
497 if ( defined ( my $min = $schema ->{ minLength
})) {
498 if ( length ( $value ) < $min ) {
499 add_error
( $errors, $path, "value must be at least $min characters long" );
504 if ( is_number
( $value )) {
505 if ( defined ( my $max = $schema ->{ maximum
})) {
507 add_error
( $errors, $path, "value must have a maximum value of $max " );
512 if ( defined ( my $min = $schema ->{ minimum
})) {
514 add_error
( $errors, $path, "value must have a minimum value of $min " );
520 if ( my $ea = $schema ->{ enum
}) {
523 foreach my $ev ( @$ea ) {
530 add_error
( $errors, $path, "value ' $value ' does not have a value in the enumeration '" .
531 join ( ", " , @$ea ) . "'" );
538 my ( $instance, $schema, $errmsg ) = @_ ;
541 $errmsg = "Parameter verification failed. \n " if ! $errmsg ;
543 # todo: cycle detection is only needed for debugging, I guess
544 # we can disable that in the final release
545 # todo: is there a better/faster way to detect cycles?
547 find_cycle
( $instance, sub { $cycles = 1 });
549 add_error
( $errors, undef , "data structure contains recursive cycles" );
551 check_prop
( $instance, $schema, '' , $errors );
554 if ( scalar ( %$errors )) {
555 raise
$errmsg, code
=> HTTP_BAD_REQUEST
, errors
=> $errors ;
561 my $schema_valid_types = [ "string" , "object" , "coderef" , "array" , "boolean" , "number" , "integer" , "null" , "any" ];
562 my $default_schema_noref = {
563 description
=> "This is the JSON Schema for JSON Schemas." ,
564 type
=> [ "object" ],
565 additionalProperties
=> 0 ,
568 type
=> [ "string" , "array" ],
569 description
=> "This is a type definition value. This can be a simple type, or a union type" ,
574 enum
=> $schema_valid_types,
576 enum
=> $schema_valid_types,
580 description
=> "This indicates that the instance property in the instance object is not required." ,
586 description
=> "This is a definition for the properties of an object value" ,
592 description
=> "When the value is an array, this indicates the schema to use to validate each item in an array" ,
596 additionalProperties
=> {
597 type
=> [ "boolean" , "object" ],
598 description
=> "This provides a default property definition for all properties that are not explicitly defined in an object type definition." ,
605 description
=> "This indicates the minimum value for the instance property when the type of the instance value is a number." ,
610 description
=> "This indicates the maximum value for the instance property when the type of the instance value is a number." ,
614 description
=> "When the instance value is a string, this indicates minimum length of the string" ,
621 description
=> "When the instance value is a string, this indicates maximum length of the string." ,
627 description
=> "A text representation of the type (used to generate documentation)." ,
632 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." ,
640 description
=> "This provides an enumeration of possible values that are valid for the instance property." ,
645 description
=> "This provides a description of the purpose the instance property. The value can be a string or it can be an object with properties corresponding to various different instance languages (with an optional default property indicating the default description)." ,
650 description
=> "This provides the title of the property" ,
653 type
=> [ "string" , "object" ],
655 description
=> "indicates a required property or a schema that must be validated if this property is present" ,
660 description
=> "This indicates what format the data is among some predefined formats which may include: \n\n date - a string following the ISO format \n address \n schema - a schema definition object \n person \n page \n html - a string representing HTML" ,
665 description
=> "This indicates the default for the instance property."
670 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." ,
675 description
=> "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also." ,
678 # this is from hyper schema
681 description
=> "This defines the link relations of the instance objects" ,
688 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" ,
692 description
=> "This is the name of the link relation" ,
698 description
=> "For submission links, this defines the method that should be used to access the target resource" ,
708 my $default_schema = Storable
:: dclone
( $default_schema_noref );
710 $default_schema ->{ properties
}->{ properties
}->{ additionalProperties
} = $default_schema ;
711 $default_schema ->{ properties
}->{ additionalProperties
}->{ properties
} = $default_schema ->{ properties
};
713 $default_schema ->{ properties
}->{ items
}->{ properties
} = $default_schema ->{ properties
};
714 $default_schema ->{ properties
}->{ items
}->{ additionalProperties
} = 0 ;
716 $default_schema ->{ properties
}->{ disallow
}->{ properties
} = $default_schema ->{ properties
};
717 $default_schema ->{ properties
}->{ disallow
}->{ additionalProperties
} = 0 ;
719 $default_schema ->{ properties
}->{ requires
}->{ properties
} = $default_schema ->{ properties
};
720 $default_schema ->{ properties
}->{ requires
}->{ additionalProperties
} = 0 ;
722 $default_schema ->{ properties
}->{ extends
}->{ properties
} = $default_schema ->{ properties
};
723 $default_schema ->{ properties
}->{ extends
}->{ additionalProperties
} = 0 ;
725 my $method_schema = {
727 additionalProperties
=> 0 ,
730 description
=> "This a description of the method" ,
735 description
=> "This indicates the name of the function to call." ,
738 additionalProperties
=> 1 ,
753 description
=> "The HTTP method name." ,
754 enum
=> [ 'GET' , 'POST' , 'PUT' , 'DELETE' ],
759 description
=> "Method needs special privileges - only pvedaemon can execute it" ,
764 description
=> "A parameter name. If specified, all calls to this method are proxied to the host contained in that parameter." ,
769 description
=> "Required access permissions. By default only 'root' is allowed to access this method." ,
771 additionalProperties
=> 0 ,
774 description
=> "Describe access permissions." ,
778 description
=> "A simply way to allow access for 'all' authenticated users. Value 'world' is used to allow access without credentials." ,
780 enum
=> [ 'all' , 'world' ],
784 description
=> "Array of permission checks (prefix notation)." ,
791 description
=> "Used internally" ,
795 description
=> "Used internally" ,
800 description
=> "path for URL matching (uri template)" ,
802 fragmentDelimiter
=> {
804 description
=> "A ways to override the default fragment delimiter '/'. This onyl works on a whole sub-class. You can set this to the empty string to match the whole rest of the URI." ,
809 description
=> "JSON Schema for parameters." ,
814 description
=> "JSON Schema for return value." ,
819 description
=> "method implementaion (code reference)" ,
824 description
=> "Delegate call to this class (perl class string)." ,
827 additionalProperties
=> 0 ,
833 fragmentDelimiter
=> { optional
=> 1 }
841 sub validate_schema
{
844 my $errmsg = "internal error - unable to verify schema \n " ;
845 validate
( $schema, $default_schema, $errmsg );
848 sub validate_method_info
{
851 my $errmsg = "internal error - unable to verify method info \n " ;
852 validate
( $info, $method_schema, $errmsg );
854 validate_schema
( $info ->{ parameters
}) if $info ->{ parameters
};
855 validate_schema
( $info ->{ returns
}) if $info ->{ returns
};
858 # run a self test on load
859 # make sure we can verify the default schema
860 validate_schema
( $default_schema_noref );
861 validate_schema
( $method_schema );
863 # and now some utility methods (used by pve api)
864 sub method_get_child_link
{
867 return undef if ! $info ;
869 my $schema = $info ->{ returns
};
870 return undef if ! $schema || ! $schema ->{ type
} || $schema ->{ type
} ne 'array' ;
872 my $links = $schema ->{ links
};
873 return undef if ! $links ;
876 foreach my $lnk ( @$links ) {
877 if ( $lnk ->{ href
} && $lnk ->{ rel
} && ( $lnk ->{ rel
} eq 'child' )) {
886 # a way to parse command line parameters, using a
887 # schema to configure Getopt::Long
889 my ( $schema, $args, $arg_param, $fixed_param, $pwcallback ) = @_ ;
891 if (! $schema || ! $schema ->{ properties
}) {
892 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
893 if scalar ( @$args ) != 0 ;
898 if ( $arg_param && ! ref ( $arg_param )) {
899 my $pd = $schema ->{ properties
}->{ $arg_param };
900 die "expected list format $pd ->{format}"
901 if !( $pd && $pd ->{ format
} && $pd ->{ format
} =~ m/-list/ );
902 $list_param = $arg_param ;
906 foreach my $prop ( keys %{ $schema ->{ properties
}}) {
907 my $pd = $schema ->{ properties
}->{ $prop };
908 next if $list_param && $prop eq $list_param ;
909 next if defined ( $fixed_param ->{ $prop });
911 if ( $prop eq 'password' && $pwcallback ) {
912 # we do not accept plain password on input line, instead
913 # we turn this into a boolean option and ask for password below
914 # using $pwcallback() (for security reasons).
915 push @getopt, " $prop " ;
916 } elsif ( $pd ->{ type
} eq 'boolean' ) {
917 push @getopt, " $prop :s" ;
919 if ( $pd ->{ format
} && $pd ->{ format
} =~ m/-a?list/ ) {
920 push @getopt, " $prop =s@" ;
922 push @getopt, " $prop =s" ;
928 raise
( "unable to parse option \n " , code
=> HTTP_BAD_REQUEST
)
929 if ! Getopt
:: Long
:: GetOptionsFromArray
( $args, $opts, @getopt );
931 if ( my $acount = scalar ( @$args )) {
933 $opts ->{ $list_param } = $args ;
935 } elsif ( ref ( $arg_param )) {
936 raise
( "wrong number of arguments \n " , code
=> HTTP_BAD_REQUEST
)
937 if scalar ( @$arg_param ) != $acount ;
938 foreach my $p ( @$arg_param ) {
939 $opts ->{ $p } = shift @$args ;
942 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
943 if scalar ( @$args ) != 0 ;
947 if ( my $pd = $schema ->{ properties
}->{ password
}) {
948 if ( $pd ->{ type
} ne 'boolean' && $pwcallback ) {
949 if ( $opts ->{ password
} || ! $pd ->{ optional
}) {
950 $opts ->{ password
} = & $pwcallback ();
955 $opts = PVE
:: Tools
:: decode_utf8_parameters
( $opts );
956 if ( $opts ->{ description
}) {
957 print "TEST: " . PVE
:: Tools
:: encode_text
( $opts ->{ description
}) . " \n " ;
960 foreach my $p ( keys %$opts ) {
961 if ( my $pd = $schema ->{ properties
}->{ $p }) {
962 if ( $pd ->{ type
} eq 'boolean' ) {
963 if ( $opts ->{ $p } eq '' ) {
965 } elsif ( $opts ->{ $p } =~ m/^(1|true|yes|on)$/i ) {
967 } elsif ( $opts ->{ $p } =~ m/^(0|false|no|off)$/i ) {
970 raise
( "unable to parse boolean option \n " , code
=> HTTP_BAD_REQUEST
);
972 } elsif ( $pd ->{ format
}) {
974 if ( $pd ->{ format
} =~ m/-list/ ) {
975 # allow --vmid 100 --vmid 101 and --vmid 100,101
976 # allow --dow mon --dow fri and --dow mon,fri
977 $opts ->{ $p } = join ( "," , @{ $opts ->{ $p }});
978 } elsif ( $pd ->{ format
} =~ m/-alist/ ) {
979 # we encode array as \0 separated strings
980 # Note: CGI.pm also use this encoding
981 if ( scalar (@{ $opts ->{ $p }}) != 1 ) {
982 $opts ->{ $p } = join ( "\0" , @{ $opts ->{ $p }});
984 # st that split_list knows it is \0 terminated
985 my $v = $opts ->{ $p }->[ 0 ];
986 $opts ->{ $p } = " $v\0 " ;
993 foreach my $p ( keys %$fixed_param ) {
994 $opts ->{ $p } = $fixed_param ->{ $p };
1000 # A way to parse configuration data by giving a json schema
1002 my ( $schema, $filename, $raw ) = @_ ;
1004 # do fast check (avoid validate_schema($schema))
1005 die "got strange schema" if ! $schema ->{ type
} ||
1006 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1010 while ( $raw && $raw =~ s/^(.*?)(\n|$)// ) {
1013 next if $line =~ m/^\#/ ; # skip comment lines
1014 next if $line =~ m/^\s*$/ ; # skip empty lines
1016 if ( $line =~ m/^(\S+):\s*(\S+)\s*$/ ) {
1019 if ( $schema ->{ properties
}->{ $key } &&
1020 $schema ->{ properties
}->{ $key }->{ type
} eq 'boolean' ) {
1022 $value = 1 if $value =~ m/^(1|on|yes|true)$/i ;
1023 $value = 0 if $value =~ m/^(0|off|no|false)$/i ;
1025 $cfg ->{ $key } = $value ;
1027 warn "ignore config line: $line\n "
1032 check_prop
( $cfg, $schema, '' , $errors );
1034 foreach my $k ( keys %$errors ) {
1035 warn "parse error in ' $filename ' - ' $k ': $errors ->{ $k } \n " ;
1042 # generate simple key/value file
1044 my ( $schema, $filename, $cfg ) = @_ ;
1046 # do fast check (avoid validate_schema($schema))
1047 die "got strange schema" if ! $schema ->{ type
} ||
1048 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1050 validate
( $cfg, $schema, "validation error in ' $filename ' \n " );
1054 foreach my $k ( keys %$cfg ) {
1055 $data .= " $k : $cfg ->{ $k } \n " ;