]>
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 ,
76 my ( $format, $code ) = @_ ;
78 die "JSON schema format ' $format ' already registered \n "
79 if $format_list ->{ $format };
81 $format_list ->{ $format } = $code ;
84 # register some common type for pve
86 register_format
( 'string' , sub {}); # allow format => 'string-list'
88 register_format
( 'pve-configid' , \
& pve_verify_configid
);
89 sub pve_verify_configid
{
90 my ( $id, $noerr ) = @_ ;
92 if ( $id !~ m/^[a-z][a-z0-9_]+$/i ) {
93 return undef if $noerr ;
94 die "invalid cofiguration ID ' $id ' \n " ;
99 register_format
( 'pve-vmid' , \
& pve_verify_vmid
);
100 sub pve_verify_vmid
{
101 my ( $vmid, $noerr ) = @_ ;
103 if ( $vmid !~ m/^[1-9][0-9]+$/ ) {
104 return undef if $noerr ;
105 die "value does not look like a valid VM ID \n " ;
110 register_format
( 'pve-node' , \
& pve_verify_node_name
);
111 sub pve_verify_node_name
{
112 my ( $node, $noerr ) = @_ ;
114 # todo: use better regex ?
115 if ( $node !~ m/^[A-Za-z][[:alnum:]\-]*[[:alnum:]]+$/ ) {
116 return undef if $noerr ;
117 die "value does not look like a valid node name \n " ;
122 register_format
( 'ipv4' , \
& pve_verify_ipv4
);
123 sub pve_verify_ipv4
{
124 my ( $ipv4, $noerr ) = @_ ;
126 if ( $ipv4 !~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ ||
127 !(( $1 > 0 ) && ( $1 < 255 ) &&
128 ( $2 <= 255 ) && ( $3 <= 255 ) &&
129 ( $4 > 0 ) && ( $4 < 255 ))) {
130 return undef if $noerr ;
131 die "value does not look like a valid IP address \n " ;
135 register_format
( 'ipv4mask' , \
& pve_verify_ipv4mask
);
136 sub pve_verify_ipv4mask
{
137 my ( $mask, $noerr ) = @_ ;
139 if ( $mask !~ m/^255\.255\.(\d{1,3})\.(\d{1,3})$/ ||
140 !(( $1 <= 255 ) && ( $2 <= 255 ))) {
141 return undef if $noerr ;
142 die "value does not look like a valid IP netmask \n " ;
147 register_format
( 'email' , \
& pve_verify_email
);
148 sub pve_verify_email
{
149 my ( $email, $noerr ) = @_ ;
151 # we use same regex as extjs Ext.form.VTypes.email
152 if ( $email !~ /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/ ) {
153 return undef if $noerr ;
154 die "value does not look like a valid email address \n " ;
159 # network interface name
160 register_format
( 'pve-iface' , \
& pve_verify_iface
);
161 sub pve_verify_iface
{
162 my ( $id, $noerr ) = @_ ;
164 if ( $id !~ m/^[a-z][a-z0-9_]{1,20}([:\.]\d+)?$/i ) {
165 return undef if $noerr ;
166 die "invalid network interface name ' $id ' \n " ;
172 my ( $format, $value ) = @_ ;
174 return if $format eq 'regex' ;
176 if ( $format =~ m/^(.*)-a?list$/ ) {
178 my $code = $format_list ->{ $1 };
180 die "undefined format ' $format ' \n " if ! $code ;
182 # Note: we allow empty lists
183 foreach my $v ( split_list
( $value )) {
187 } elsif ( $format =~ m/^(.*)-opt$/ ) {
189 my $code = $format_list ->{ $1 };
191 die "undefined format ' $format ' \n " if ! $code ;
193 return if ! $value ; # allow empty string
199 my $code = $format_list ->{ $format };
201 die "undefined format ' $format ' \n " if ! $code ;
208 my ( $errors, $path, $msg ) = @_ ;
210 $path = '_root' if ! $path ;
212 if ( $errors ->{ $path }) {
213 $errors ->{ $path } = join ( ' \n ' , $errors ->{ $path }, $msg );
215 $errors ->{ $path } = $msg ;
222 # see 'man perlretut'
223 return $value =~ /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/ ;
229 return $value =~ m/^[+-]?\d+$/ ;
233 my ( $path, $type, $value, $errors ) = @_ ;
237 if (! defined ( $value )) {
238 return 1 if $type eq 'null' ;
242 if ( my $tt = ref ( $type )) {
243 if ( $tt eq 'ARRAY' ) {
244 foreach my $t ( @$type ) {
246 check_type
( $path, $t, $value, $tmperr );
247 return 1 if ! scalar ( %$tmperr );
249 my $ttext = join ( '|' , @$type );
250 add_error
( $errors, $path, "type check (' $ttext ') failed" );
252 } elsif ( $tt eq 'HASH' ) {
254 check_prop
( $value, $type, $path, $tmperr );
255 return 1 if ! scalar ( %$tmperr );
256 add_error
( $errors, $path, "type check failed" );
259 die "internal error - got reference type ' $tt '" ;
264 return 1 if $type eq 'any' ;
266 if ( $type eq 'null' ) {
267 if ( defined ( $value )) {
268 add_error
( $errors, $path, "type check (' $type ') failed - value is not null" );
274 my $vt = ref ( $value );
276 if ( $type eq 'array' ) {
277 if (! $vt || $vt ne 'ARRAY' ) {
278 add_error
( $errors, $path, "type check (' $type ') failed" );
282 } elsif ( $type eq 'object' ) {
283 if (! $vt || $vt ne 'HASH' ) {
284 add_error
( $errors, $path, "type check (' $type ') failed" );
288 } elsif ( $type eq 'coderef' ) {
289 if (! $vt || $vt ne 'CODE' ) {
290 add_error
( $errors, $path, "type check (' $type ') failed" );
296 add_error
( $errors, $path, "type check (' $type ') failed - got $vt " );
299 if ( $type eq 'string' ) {
300 return 1 ; # nothing to check ?
301 } elsif ( $type eq 'boolean' ) {
302 #if ($value =~ m/^(1|true|yes|on)$/i) {
305 #} elsif ($value =~ m/^(0|false|no|off)$/i) {
306 } elsif ( $value eq '0' ) {
309 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
312 } elsif ( $type eq 'integer' ) {
313 if (! is_integer
( $value )) {
314 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
318 } elsif ( $type eq 'number' ) {
319 if (! is_number
( $value )) {
320 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
325 return 1 ; # no need to verify unknown types
335 my ( $path, $schema, $value, $additional_properties, $errors ) = @_ ;
337 # print "Check Object " . Dumper($value) . "\nSchema: " . Dumper($schema);
339 my $st = ref ( $schema );
340 if (! $st || $st ne 'HASH' ) {
341 add_error
( $errors, $path, "Invalid schema definition." );
345 my $vt = ref ( $value );
346 if (! $vt || $vt ne 'HASH' ) {
347 add_error
( $errors, $path, "an object is required" );
351 foreach my $k ( keys %$schema ) {
352 check_prop
( $value ->{ $k }, $schema ->{ $k }, $path ?
" $path . $k " : $k, $errors );
355 foreach my $k ( keys %$value ) {
357 my $newpath = $path ?
" $path . $k " : $k ;
359 if ( my $subschema = $schema ->{ $k }) {
360 if ( my $requires = $subschema ->{ requires
}) {
361 if ( ref ( $requires )) {
362 #print "TEST: " . Dumper($value) . "\n", Dumper($requires) ;
363 check_prop
( $value, $requires, $path, $errors );
364 } elsif (! defined ( $value ->{ $requires })) {
365 add_error
( $errors, $path ?
" $path . $requires " : $requires,
366 "missing property - ' $newpath ' requiers this property" );
370 next ; # value is already checked above
373 if ( defined ( $additional_properties ) && ! $additional_properties ) {
374 add_error
( $errors, $newpath, "property is not defined in schema " .
375 "and the schema does not allow additional properties" );
378 check_prop
( $value ->{ $k }, $additional_properties, $newpath, $errors )
379 if ref ( $additional_properties );
384 my ( $value, $schema, $path, $errors ) = @_ ;
386 die "internal error - no schema" if ! $schema ;
387 die "internal error" if ! $errors ;
389 #print "check_prop $path\n" if $value;
391 my $st = ref ( $schema );
392 if (! $st || $st ne 'HASH' ) {
393 add_error
( $errors, $path, "Invalid schema definition." );
397 # if it extends another schema, it must pass that schema as well
398 if ( $schema ->{ extends
}) {
399 check_prop
( $value, $schema ->{ extends
}, $path, $errors );
402 if (! defined ( $value )) {
403 return if $schema ->{ type
} && $schema ->{ type
} eq 'null' ;
404 if (! $schema ->{ optional
}) {
405 add_error
( $errors, $path, "property is missing and it is not optional" );
410 return if ! check_type
( $path, $schema ->{ type
}, $value, $errors );
412 if ( $schema ->{ disallow
}) {
414 if ( check_type
( $path, $schema ->{ disallow
}, $value, $tmperr )) {
415 add_error
( $errors, $path, "disallowed value was matched" );
420 if ( my $vt = ref ( $value )) {
422 if ( $vt eq 'ARRAY' ) {
423 if ( $schema ->{ items
}) {
424 my $it = ref ( $schema ->{ items
});
425 if ( $it && $it eq 'ARRAY' ) {
426 #die "implement me $path: $vt " . Dumper($schema) ."\n". Dumper($value);
427 die "not implemented" ;
430 foreach my $el ( @$value ) {
431 check_prop
( $el, $schema ->{ items
}, "${path}[ $ind ]" , $errors );
437 } elsif ( $schema ->{ properties
} || $schema ->{ additionalProperties
}) {
438 check_object
( $path, defined ( $schema ->{ properties
}) ?
$schema ->{ properties
} : {},
439 $value, $schema ->{ additionalProperties
}, $errors );
445 if ( my $format = $schema ->{ format
}) {
446 eval { check_format
( $format, $value ); };
448 add_error
( $errors, $path, "invalid format - $@ " );
453 if ( my $pattern = $schema ->{ pattern
}) {
454 if ( $value !~ m/^$pattern$/ ) {
455 add_error
( $errors, $path, "value does not match the regex pattern" );
460 if ( defined ( my $max = $schema ->{ maxLength
})) {
461 if ( length ( $value ) > $max ) {
462 add_error
( $errors, $path, "value may only be $max characters long" );
467 if ( defined ( my $min = $schema ->{ minLength
})) {
468 if ( length ( $value ) < $min ) {
469 add_error
( $errors, $path, "value must be at least $min characters long" );
474 if ( is_number
( $value )) {
475 if ( defined ( my $max = $schema ->{ maximum
})) {
477 add_error
( $errors, $path, "value must have a maximum value of $max " );
482 if ( defined ( my $min = $schema ->{ minimum
})) {
484 add_error
( $errors, $path, "value must have a minimum value of $min " );
490 if ( my $ea = $schema ->{ enum
}) {
493 foreach my $ev ( @$ea ) {
500 add_error
( $errors, $path, "value ' $value ' does not have a value in the enumeration '" .
501 join ( ", " , @$ea ) . "'" );
508 my ( $instance, $schema, $errmsg ) = @_ ;
511 $errmsg = "Parameter verification failed. \n " if ! $errmsg ;
513 # todo: cycle detection is only needed for debugging, I guess
514 # we can disable that in the final release
515 # todo: is there a better/faster way to detect cycles?
517 find_cycle
( $instance, sub { $cycles = 1 });
519 add_error
( $errors, undef , "data structure contains recursive cycles" );
521 check_prop
( $instance, $schema, '' , $errors );
524 if ( scalar ( %$errors )) {
525 raise
$errmsg, code
=> HTTP_BAD_REQUEST
, errors
=> $errors ;
531 my $schema_valid_types = [ "string" , "object" , "coderef" , "array" , "boolean" , "number" , "integer" , "null" , "any" ];
532 my $default_schema_noref = {
533 description
=> "This is the JSON Schema for JSON Schemas." ,
534 type
=> [ "object" ],
535 additionalProperties
=> 0 ,
538 type
=> [ "string" , "array" ],
539 description
=> "This is a type definition value. This can be a simple type, or a union type" ,
544 enum
=> $schema_valid_types,
546 enum
=> $schema_valid_types,
550 description
=> "This indicates that the instance property in the instance object is not required." ,
556 description
=> "This is a definition for the properties of an object value" ,
562 description
=> "When the value is an array, this indicates the schema to use to validate each item in an array" ,
566 additionalProperties
=> {
567 type
=> [ "boolean" , "object" ],
568 description
=> "This provides a default property definition for all properties that are not explicitly defined in an object type definition." ,
575 description
=> "This indicates the minimum value for the instance property when the type of the instance value is a number." ,
580 description
=> "This indicates the maximum value for the instance property when the type of the instance value is a number." ,
584 description
=> "When the instance value is a string, this indicates minimum length of the string" ,
591 description
=> "When the instance value is a string, this indicates maximum length of the string." ,
597 description
=> "A text representation of the type (used to generate documentation)." ,
602 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." ,
610 description
=> "This provides an enumeration of possible values that are valid for the instance property." ,
615 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)." ,
620 description
=> "This provides the title of the property" ,
623 type
=> [ "string" , "object" ],
625 description
=> "indicates a required property or a schema that must be validated if this property is present" ,
630 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" ,
635 description
=> "This indicates the default for the instance property."
640 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." ,
645 description
=> "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also." ,
648 # this is from hyper schema
651 description
=> "This defines the link relations of the instance objects" ,
658 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" ,
662 description
=> "This is the name of the link relation" ,
668 description
=> "For submission links, this defines the method that should be used to access the target resource" ,
678 my $default_schema = Storable
:: dclone
( $default_schema_noref );
680 $default_schema ->{ properties
}->{ properties
}->{ additionalProperties
} = $default_schema ;
681 $default_schema ->{ properties
}->{ additionalProperties
}->{ properties
} = $default_schema ->{ properties
};
683 $default_schema ->{ properties
}->{ items
}->{ properties
} = $default_schema ->{ properties
};
684 $default_schema ->{ properties
}->{ items
}->{ additionalProperties
} = 0 ;
686 $default_schema ->{ properties
}->{ disallow
}->{ properties
} = $default_schema ->{ properties
};
687 $default_schema ->{ properties
}->{ disallow
}->{ additionalProperties
} = 0 ;
689 $default_schema ->{ properties
}->{ requires
}->{ properties
} = $default_schema ->{ properties
};
690 $default_schema ->{ properties
}->{ requires
}->{ additionalProperties
} = 0 ;
692 $default_schema ->{ properties
}->{ extends
}->{ properties
} = $default_schema ->{ properties
};
693 $default_schema ->{ properties
}->{ extends
}->{ additionalProperties
} = 0 ;
695 my $method_schema = {
697 additionalProperties
=> 0 ,
700 description
=> "This a description of the method" ,
705 description
=> "This indicates the name of the function to call." ,
708 additionalProperties
=> 1 ,
723 description
=> "The HTTP method name." ,
724 enum
=> [ 'GET' , 'POST' , 'PUT' , 'DELETE' ],
729 description
=> "Method needs special privileges - only pvedaemon can execute it" ,
734 description
=> "A parameter name. If specified, all calls to this method are proxied to the host contained in that parameter." ,
739 description
=> "Required access permissions. By default only 'root' is allowed to access this method." ,
741 additionalProperties
=> 0 ,
744 description
=> "Describe access permissions." ,
748 description
=> "A simply way to allow access for 'all' authenticated users. Value 'world' is used to allow access without credentials." ,
750 enum
=> [ 'all' , 'world' ],
754 description
=> "Array of permission checks (prefix notation)." ,
761 description
=> "Used internally" ,
765 description
=> "Used internally" ,
770 description
=> "path for URL matching (uri template)" ,
772 fragmentDelimiter
=> {
774 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." ,
779 description
=> "JSON Schema for parameters." ,
784 description
=> "JSON Schema for return value." ,
789 description
=> "method implementaion (code reference)" ,
794 description
=> "Delegate call to this class (perl class string)." ,
797 additionalProperties
=> 0 ,
803 fragmentDelimiter
=> { optional
=> 1 }
811 sub validate_schema
{
814 my $errmsg = "internal error - unable to verify schema \n " ;
815 validate
( $schema, $default_schema, $errmsg );
818 sub validate_method_info
{
821 my $errmsg = "internal error - unable to verify method info \n " ;
822 validate
( $info, $method_schema, $errmsg );
824 validate_schema
( $info ->{ parameters
}) if $info ->{ parameters
};
825 validate_schema
( $info ->{ returns
}) if $info ->{ returns
};
828 # run a self test on load
829 # make sure we can verify the default schema
830 validate_schema
( $default_schema_noref );
831 validate_schema
( $method_schema );
833 # and now some utility methods (used by pve api)
834 sub method_get_child_link
{
837 return undef if ! $info ;
839 my $schema = $info ->{ returns
};
840 return undef if ! $schema || ! $schema ->{ type
} || $schema ->{ type
} ne 'array' ;
842 my $links = $schema ->{ links
};
843 return undef if ! $links ;
846 foreach my $lnk ( @$links ) {
847 if ( $lnk ->{ href
} && $lnk ->{ rel
} && ( $lnk ->{ rel
} eq 'child' )) {
856 # a way to parse command line parameters, using a
857 # schema to configure Getopt::Long
859 my ( $schema, $args, $arg_param, $fixed_param, $pwcallback ) = @_ ;
861 if (! $schema || ! $schema ->{ properties
}) {
862 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
863 if scalar ( @$args ) != 0 ;
868 if ( $arg_param && ! ref ( $arg_param )) {
869 my $pd = $schema ->{ properties
}->{ $arg_param };
870 die "expected list format $pd ->{format}"
871 if !( $pd && $pd ->{ format
} && $pd ->{ format
} =~ m/-list/ );
872 $list_param = $arg_param ;
876 foreach my $prop ( keys %{ $schema ->{ properties
}}) {
877 my $pd = $schema ->{ properties
}->{ $prop };
878 next if $list_param && $prop eq $list_param ;
879 next if defined ( $fixed_param ->{ $prop });
881 if ( $prop eq 'password' && $pwcallback ) {
882 # we do not accept plain password on input line, instead
883 # we turn this into a boolean option and ask for password below
884 # using $pwcallback() (for security reasons).
885 push @getopt, " $prop " ;
886 } elsif ( $pd ->{ type
} eq 'boolean' ) {
887 push @getopt, " $prop :s" ;
889 if ( $pd ->{ format
} && $pd ->{ format
} =~ m/-a?list/ ) {
890 push @getopt, " $prop =s@" ;
892 push @getopt, " $prop =s" ;
898 raise
( "unable to parse option \n " , code
=> HTTP_BAD_REQUEST
)
899 if ! Getopt
:: Long
:: GetOptionsFromArray
( $args, $opts, @getopt );
901 if ( my $acount = scalar ( @$args )) {
903 $opts ->{ $list_param } = $args ;
905 } elsif ( ref ( $arg_param )) {
906 raise
( "wrong number of arguments \n " , code
=> HTTP_BAD_REQUEST
)
907 if scalar ( @$arg_param ) != $acount ;
908 foreach my $p ( @$arg_param ) {
909 $opts ->{ $p } = shift @$args ;
912 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
913 if scalar ( @$args ) != 0 ;
917 if ( my $pd = $schema ->{ properties
}->{ password
}) {
918 if ( $pd ->{ type
} ne 'boolean' && $pwcallback ) {
919 if ( $opts ->{ password
} || ! $pd ->{ optional
}) {
920 $opts ->{ password
} = & $pwcallback ();
925 $opts = PVE
:: Tools
:: decode_utf8_parameters
( $opts );
926 if ( $opts ->{ description
}) {
927 print "TEST: " . PVE
:: Tools
:: encode_text
( $opts ->{ description
}) . " \n " ;
930 foreach my $p ( keys %$opts ) {
931 if ( my $pd = $schema ->{ properties
}->{ $p }) {
932 if ( $pd ->{ type
} eq 'boolean' ) {
933 if ( $opts ->{ $p } eq '' ) {
935 } elsif ( $opts ->{ $p } =~ m/^(1|true|yes|on)$/i ) {
937 } elsif ( $opts ->{ $p } =~ m/^(0|false|no|off)$/i ) {
940 raise
( "unable to parse boolean option \n " , code
=> HTTP_BAD_REQUEST
);
942 } elsif ( $pd ->{ format
}) {
944 if ( $pd ->{ format
} =~ m/-list/ ) {
945 # allow --vmid 100 --vmid 101 and --vmid 100,101
946 # allow --dow mon --dow fri and --dow mon,fri
947 $opts ->{ $p } = join ( "," , @{ $opts ->{ $p }});
948 } elsif ( $pd ->{ format
} =~ m/-alist/ ) {
949 # we encode array as \0 separated strings
950 # Note: CGI.pm also use this encoding
951 if ( scalar (@{ $opts ->{ $p }}) != 1 ) {
952 $opts ->{ $p } = join ( "\0" , @{ $opts ->{ $p }});
954 # st that split_list knows it is \0 terminated
955 my $v = $opts ->{ $p }->[ 0 ];
956 $opts ->{ $p } = " $v\0 " ;
963 foreach my $p ( keys %$fixed_param ) {
964 $opts ->{ $p } = $fixed_param ->{ $p };
970 # A way to parse configuration data by giving a json schema
972 my ( $schema, $filename, $raw ) = @_ ;
974 # do fast check (avoid validate_schema($schema))
975 die "got strange schema" if ! $schema ->{ type
} ||
976 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
980 while ( $raw && $raw =~ s/^(.*?)(\n|$)// ) {
983 next if $line =~ m/^\#/ ; # skip comment lines
984 next if $line =~ m/^\s*$/ ; # skip empty lines
986 if ( $line =~ m/^(\S+):\s*(\S+)\s*$/ ) {
989 if ( $schema ->{ properties
}->{ $key } &&
990 $schema ->{ properties
}->{ $key }->{ type
} eq 'boolean' ) {
992 $value = 1 if $value =~ m/^(1|on|yes|true)$/i ;
993 $value = 0 if $value =~ m/^(0|off|no|false)$/i ;
995 $cfg ->{ $key } = $value ;
997 warn "ignore config line: $line\n "
1002 check_prop
( $cfg, $schema, '' , $errors );
1004 foreach my $k ( keys %$errors ) {
1005 warn "parse error in ' $filename ' - ' $k ': $errors ->{ $k } \n " ;
1012 # generate simple key/value file
1014 my ( $schema, $filename, $cfg ) = @_ ;
1016 # do fast check (avoid validate_schema($schema))
1017 die "got strange schema" if ! $schema ->{ type
} ||
1018 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1020 validate
( $cfg, $schema, "validation error in ' $filename ' \n " );
1024 foreach my $k ( keys %$cfg ) {
1025 $data .= " $k : $cfg ->{ $k } \n " ;