line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Sah::JS; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2016-09-14'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.87'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
5935
|
use 5.010001; |
|
4
|
|
|
|
|
8
|
|
7
|
4
|
|
|
4
|
|
12
|
use strict; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
55
|
|
8
|
4
|
|
|
4
|
|
10
|
use warnings; |
|
4
|
|
|
|
|
2
|
|
|
4
|
|
|
|
|
84
|
|
9
|
|
|
|
|
|
|
#use Log::Any qw($log); |
10
|
|
|
|
|
|
|
|
11
|
4
|
|
|
4
|
|
1714
|
use IPC::System::Options; |
|
4
|
|
|
|
|
11171
|
|
|
4
|
|
|
|
|
20
|
|
12
|
4
|
|
|
4
|
|
1678
|
use Nodejs::Util qw(get_nodejs_path); |
|
4
|
|
|
|
|
2760
|
|
|
4
|
|
|
|
|
1129
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $Log_Validator_Code = $ENV{LOG_SAH_VALIDATOR_CODE} // 0; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
require Exporter; |
17
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
18
|
|
|
|
|
|
|
our @EXPORT_OK = qw(gen_validator); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub gen_validator { |
21
|
0
|
|
|
0
|
1
|
|
require Data::Sah; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
my ($schema, $opts) = @_; |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
state $jsc = Data::Sah->new->get_compiler("js"); |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
0
|
|
|
|
my %args = (schema => $schema, %{$opts // {}}); |
|
0
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
my $opt_source = delete $args{source}; |
29
|
|
|
|
|
|
|
|
30
|
0
|
0
|
|
|
|
|
$args{log_result} = 1 if $Log_Validator_Code; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $v_src = $jsc->expr_validator_sub(%args); |
33
|
0
|
0
|
|
|
|
|
return $v_src if $opt_source; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
state $nodejs_path = get_nodejs_path(); |
36
|
0
|
0
|
|
|
|
|
die "Can't find node.js in PATH" unless $nodejs_path; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub { |
40
|
0
|
|
|
0
|
|
|
require File::Temp; |
41
|
0
|
|
|
|
|
|
require JSON::MaybeXS; |
42
|
|
|
|
|
|
|
#require String::ShellQuote; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $data = shift; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
state $json = JSON::MaybeXS->new->allow_nonref; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# code to be sent to nodejs |
49
|
0
|
|
|
|
|
|
my $src = "var validator = $v_src;\n\n". |
50
|
|
|
|
|
|
|
"console.log(JSON.stringify(validator(". |
51
|
|
|
|
|
|
|
$json->encode($data).")))"; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
my ($jsh, $jsfn) = File::Temp::tempfile(); |
54
|
0
|
|
|
|
|
|
print $jsh $src; |
55
|
0
|
0
|
|
|
|
|
close($jsh) or die "Can't write JS code to file $jsfn: $!"; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
my $out = IPC::System::Options::readpipe($nodejs_path, $jsfn); |
58
|
0
|
|
|
|
|
|
$json->decode($out); |
59
|
0
|
|
|
|
|
|
}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
63
|
|
|
|
|
|
|
# ABSTRACT: Some functions to use JavaScript Sah validator code from Perl |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
__END__ |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=pod |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=encoding UTF-8 |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 NAME |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Data::Sah::JS - Some functions to use JavaScript Sah validator code from Perl |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 VERSION |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
This document describes version 0.87 of Data::Sah::JS (from Perl distribution Data-Sah-JS), released on 2016-09-14. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 SYNOPSIS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
use Data::Sah::JS qw(gen_validator); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $v = gen_validator(["int*", min=>1, max=>10]); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# validate your data using the generated validator |
86
|
|
|
|
|
|
|
say "valid" if $v->(5); # valid |
87
|
|
|
|
|
|
|
say "valid" if $v->(11); # invalid |
88
|
|
|
|
|
|
|
say "valid" if $v->(undef); # invalid |
89
|
|
|
|
|
|
|
say "valid" if $v->("x"); # invalid |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# generate validator which reports error message string, in Indonesian |
92
|
|
|
|
|
|
|
my $v = gen_validator(["int*", min=>1, max=>10], |
93
|
|
|
|
|
|
|
{return_type=>'str', lang=>'id_ID'}); |
94
|
|
|
|
|
|
|
say $v->(5); # '' |
95
|
|
|
|
|
|
|
say $v->(12); # 'Data tidak boleh lebih besar dari 10' |
96
|
|
|
|
|
|
|
# (in English: 'Data must not be larger than 10') |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 FUNCTIONS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
None exported by default. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 gen_validator($schema, \%opts) => CODE (or STR) |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Generate validator code for C<$schema>. This is currently used for testing |
107
|
|
|
|
|
|
|
purposes only, as this will first generate JavaScript validator code, then |
108
|
|
|
|
|
|
|
generate a Perl coderef that will feed generated JavaScript validator code to a |
109
|
|
|
|
|
|
|
JavaScript engine (currently node.js) via command-line. Not exactly efficient. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Known options (unknown options will be passed to JS schema compiler): |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=over |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item * source => BOOL (default: 0) |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
If set to 1, return JavaScript source code string instead of Perl coderef. |
118
|
|
|
|
|
|
|
Usually only needed for debugging (but see also |
119
|
|
|
|
|
|
|
C<$Data::Sah::Log_Validator_Code> and C<LOG_SAH_VALIDATOR_CODE> if you want to |
120
|
|
|
|
|
|
|
log validator source code). |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=back |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 ENVIRONMENT |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
L<LOG_SAH_VALIDATOR_CODE> |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 HOMEPAGE |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-JS>. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 SOURCE |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-Data-Sah-JS>. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 BUGS |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-JS> |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
141
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
142
|
|
|
|
|
|
|
feature. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 SEE ALSO |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
L<Data::Sah>, L<Data::Sah::Compiler::js>. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 AUTHOR |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
This software is copyright (c) 2016 by perlancar@cpan.org. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
157
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |