Validator.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 60 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package SQL::Validator;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry;
9 use routines;
10  
11 use Data::Object::Class;
12 use Data::Object::ClassHas;
13 use JSON::Validator;
14  
15 our $VERSION = '0.01'; # VERSION
16  
17 our $GITHUB_SOURCE = 'iamalnewkirk/json-sql';
18  
19 # ATTRIBUTES
20  
21 has schema => (
22 is => 'ro',
23 isa => 'Any',
24 new => 1
25 );
26  
27 fun new_schema($self) {
28 my $version = $self->version;
29 my $specification = "schemas/$version/rulesets.yaml";
30 "https://raw.githubusercontent.com/$GITHUB_SOURCE/master/$specification"
31 }
32  
33 has validator => (
34 is => 'ro',
35 isa => 'InstanceOf["JSON::Validator"]',
36 new => 1
37 );
38  
39 fun new_validator($self) {
40 JSON::Validator->new
41 }
42  
43 has version => (
44 is => 'ro',
45 isa => 'Str',
46 def => '0.0'
47 );
48  
49 # METHODS
50  
51 method error() {
52  
53 return $self->{error};
54 }
55  
56 method validate(HashRef $schema) {
57 my $validator = $self->validator;
58  
59 $validator->coerce('booleans');
60 $validator->schema($self->schema);
61  
62 my @issues = $validator->validate($schema);
63  
64 if (@issues) {
65 require SQL::Validator::Error;
66  
67 $self->{error} = SQL::Validator::Error->new(
68 context => $self,
69 issues => [@issues]
70 );
71 }
72 else {
73 delete $self->{error};
74 }
75  
76 return !@issues ? 1 : 0;
77 }
78  
79 1;
80 =encoding utf8
81  
82 =head1 NAME
83  
84 SQL::Validator - Validate JSON-SQL Schemas
85  
86 =cut
87  
88 =head1 ABSTRACT
89  
90 Validate JSON-SQL Schemas
91  
92 =cut
93  
94 =head1 SYNOPSIS
95  
96 use SQL::Validator;
97  
98 my $sql = SQL::Validator->new;
99  
100 # my $valid = $sql->validate({
101 # insert => {
102 # into => {
103 # table => 'users'
104 # },
105 # default => true
106 # }
107 # });
108  
109 # i.e. INSERT INTO users DEFAULT VALUES;
110  
111 # $sql->error->report('insert')
112  
113 =cut
114  
115 =head1 DESCRIPTION
116  
117 This package provides a
118 L<json-sql|https://github.com/iamalnewkirk/json-sql#readme> data structure
119 validation library based around L<json-schema|https://json-schema.org>.
120  
121 =cut
122  
123 =head1 ATTRIBUTES
124  
125 This package has the following attributes:
126  
127 =cut
128  
129 =head2 schema
130  
131 schema(Any)
132  
133 This attribute is read-only, accepts C<(Any)> values, and is optional.
134  
135 =cut
136  
137 =head2 validator
138  
139 validator(InstanceOf["JSON::Validator"])
140  
141 This attribute is read-only, accepts C<(InstanceOf["JSON::Validator"])> values, and is optional.
142  
143 =cut
144  
145 =head2 version
146  
147 version(Str)
148  
149 This attribute is read-only, accepts C<(Str)> values, and is optional.
150  
151 =cut
152  
153 =head1 METHODS
154  
155 This package implements the following methods:
156  
157 =cut
158  
159 =head2 error
160  
161 error() : InstanceOf["SQL::Validator::Error"]
162  
163 The error method validates the JSON-SQL schema provided.
164  
165 =over 4
166  
167 =item error example #1
168  
169 # given: synopsis
170  
171 $sql->validate({});
172  
173 my $error = $sql->error;
174  
175 =back
176  
177 =over 4
178  
179 =item error example #2
180  
181 # given: synopsis
182  
183 $sql->validate({select => {}});
184  
185 my $error = $sql->error;
186  
187 =back
188  
189 =cut
190  
191 =head2 validate
192  
193 validate(HashRef $schema) : Bool
194  
195 The validate method validates the JSON-SQL schema provided.
196  
197 =over 4
198  
199 =item validate example #1
200  
201 # given: synopsis
202  
203 my $valid = $sql->validate({
204 insert => {
205 into => {
206 table => 'users'
207 },
208 default => 'true'
209 }
210 });
211  
212 # VALID
213  
214 =back
215  
216 =over 4
217  
218 =item validate example #2
219  
220 # given: synopsis
221  
222 my $valid = $sql->validate({
223 insert => {
224 table => 'users',
225 default => 'true'
226 }
227 });
228  
229 # INVALID
230  
231 =back
232  
233 =over 4
234  
235 =item validate example #3
236  
237 # given: synopsis
238  
239 my $valid = $sql->validate({
240 insert => {
241 into => 'users',
242 values => [1, 2, 3]
243 }
244 });
245  
246 # INVALID
247  
248 =back
249  
250 =cut
251  
252 =head1 AUTHOR
253  
254 Al Newkirk, C<awncorp@cpan.org>
255  
256 =head1 LICENSE
257  
258 Copyright (C) 2011-2019, Al Newkirk, et al.
259  
260 This is free software; you can redistribute it and/or modify it under the terms
261 of the The Apache License, Version 2.0, as elucidated in the L<"license
262 file"|https://github.com/iamalnewkirk/sql-validator/blob/master/LICENSE>.
263  
264 =head1 PROJECT
265  
266 L<Wiki|https://github.com/iamalnewkirk/sql-validator/wiki>
267  
268 L<Project|https://github.com/iamalnewkirk/sql-validator>
269  
270 L<Initiatives|https://github.com/iamalnewkirk/sql-validator/projects>
271  
272 L<Milestones|https://github.com/iamalnewkirk/sql-validator/milestones>
273  
274 L<Contributing|https://github.com/iamalnewkirk/sql-validator/blob/master/CONTRIBUTE.md>
275  
276 L<Issues|https://github.com/iamalnewkirk/sql-validator/issues>
277  
278 =cut