include/punk/punk_validate.h

filter: Statementstmt 97.1% 302 / 316 (5 uncoverable) Branchbran 63.7% 250 / 422 (43 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.0% 552 / 738 (48 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/model.xs ↑ Summary include/punk/punk_compile.h »
1 /* punk_validate.h - collecting request validation, in C.
2 *
3 * The whole tier runs on the JSON::Schema::Fast C ABI (jsf_abi.h): plain
4 * hashref schemas compile once through the table (coercion on) and cache by
5 * their canonical JSON via the File::Raw::JSON ABI - a hashref literal in a
6 * handler is a fresh ref every call, so refaddr keying would recompile
7 * forever; a prebuilt JSON::Schema::Fast::Compiled passes straight through.
8 * Per request nothing is Perl but the (optional) req->json / params calls
9 * that fetch the data.
10 *
11 * Invalid data never croaks - it collects into a Punk::Validate::Result the
12 * handler (or the generated route guard, a punk_closure appended to the
13 * record's guards AV at boot) turns into a 400 or a form re-render. Errors
14 * carry the Open::API shape: the JSON::Schema::Fast fields plus `name`,
15 * with a root-level `required` failure expanded to one error per missing
16 * property so a form can put the message beside its field.
17 *
18 * Needs (include after): punk_context.h (pcx_*), punk_session.h (ps_stash),
19 * punk_dispatch.h (pd_call), punk_static.h (punk_closure), punk_names.h,
20 * and the root's punk_frj. */
21  
22 #ifndef PUNK_VALIDATE_H
23 #define PUNK_VALIDATE_H
24  
25 #include "jsf_abi.h"
26  
27 #define PV_RESULT_CLASS "Punk::Validate::Result"
28  
29 /* ---- the JSON::Schema::Fast ABI, resolved once at runtime ----------------- */
30  
31 static const jsf_abi *PUNK_JSF_TAB = NULL;
32 static int PUNK_JSF_TAB_TRIED = 0;
33  
34 1431 static const jsf_abi *punk_jsf_try(pTHX) {
35 1431 ▶ if (!PUNK_JSF_TAB_TRIED) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_JSF_TAB_TRIED) { 4 1427 1431
36 4 dSP; int count, ok; IV p = 0;
37 4 PUNK_JSF_TAB_TRIED = 1;
38 4 ok = pk_require_once(aTHX_ "JSON::Schema::Fast", FALSE);
39 4 SPAGAIN; /* the require may have reallocated the value stack */
40 /*
41 # uncoverable branch false
42 # uncoverable condition right count:1 class:ignore_covered_err */
43 4 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 4 0 4
44 4 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 4 4
45 4 count = call_pv("JSON::Schema::Fast::_abi_ptr", G_SCALAR | G_EVAL);
46 4 SPAGAIN;
47 /*
48 # uncoverable branch false
49 # uncoverable condition right count:1 class:ignore_covered_err
50 # uncoverable condition right count:2 class:ignore_covered_err */
51 4 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
52 /*
53 # uncoverable statement
54 # uncoverable branch true
55 # uncoverable branch false
56 # uncoverable condition left count:1
57 # uncoverable condition right count:1 */
58 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
59 4 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 4 0 4
60 /*
61 # uncoverable branch false
62 # uncoverable condition right count:1 class:ignore_covered_err */
63 4 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 4 0 4
64 4 const jsf_abi *a = INT2PTR(const jsf_abi *, p);
65 /*
66 # uncoverable branch false
67 # uncoverable condition right count:1 class:ignore_covered_err
68 # uncoverable condition right count:2 class:ignore_covered_err */
69 4 ▶ if (a && a->abi_version >= JSF_ABI_VERSION)
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= JSF_ABI_VERSION) 4 0 4
if (a && a->abi_version >= JSF_ABI_VERSION) 4 0 4
70 4 PUNK_JSF_TAB = a;
71 }
72 }
73 }
74 1431 return PUNK_JSF_TAB;
75 }
76  
77 1431 static const jsf_abi *punk_jsf(pTHX) {
78 1431 const jsf_abi *a = punk_jsf_try(aTHX);
79 /*
80 # uncoverable branch true
81 # uncoverable condition left count:1 class:ignore_covered_err */
82 1431 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 0 1431 1431
83 0 croak("Punk: validation needs JSON::Schema::Fast with a compatible " /* # uncoverable statement */
84 "C ABI (JSF_ABI_VERSION %d)", JSF_ABI_VERSION);
85 1431 return a;
86 }
87  
88 /* ---- small helpers -------------------------------------------------------- */
89  
90 /* $obj->$meth(@argv) under G_EVAL: NULL (and a cleared error) on a die -
91 * the data-fetching calls mirror the defensive evals the Perl tier had. */
92 2870 static SV *pv_meth_ev(pTHX_ SV *obj, const char *meth, SV **argv, int nargs) {
93 2870 dSP; SV *r = NULL; int count, i;
94 2870 ENTER; SAVETMPS;
95 2870 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2870 2870
96 2870 ▶ EXTEND(SP, nargs + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs + 1); 2870 0 2870
EXTEND(SP, nargs + 1); 0 2870 2870
97 2870 PUSHs(obj);
98 3588 ▶ for (i = 0; i < nargs; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) PUSHs(argv[i]); 718 2870 3588
99 2870 PUTBACK;
100 2870 count = call_method(meth, G_SCALAR | G_EVAL);
101 2870 SPAGAIN;
102 /*
103 # uncoverable branch false
104 # uncoverable condition right count:1 class:ignore_covered_err
105 # uncoverable condition right count:2 class:ignore_covered_err
106 # uncoverable condition right count:3 class:ignore_covered_err */
107 2870 ▶ if (count > 0) { SV *t = POPs; if (!SvTRUE(ERRSV)) r = newSVsv(t); }
Branch: 3 branches
BranchTrueFalseTotal
if (count > 0) { SV *t = POPs; if (!SvTRUE(ERRSV)) r = newSVsv(t); } 2870 0 2870
if (count > 0) { SV *t = POPs; if (!SvTRUE(ERRSV)) r = newSVsv(t); } 2870 0 2870
if (count > 0) { SV *t = POPs; if (!SvTRUE(ERRSV)) r = newSVsv(t); } 2870 0 2870
108 2870 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2870 0 2870
109 /*
110 # uncoverable branch true
111 # uncoverable condition right count:1 class:ignore_covered_err
112 # uncoverable condition left count:2 class:ignore_covered_err
113 # uncoverable condition left count:3 class:ignore_covered_err
114 # uncoverable condition right count:3 class:ignore_covered_err */
115 2870 ▶ if (SvTRUE(ERRSV)) sv_setsv(ERRSV, &PL_sv_no);
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) sv_setsv(ERRSV, &PL_sv_no); 2870 0 2870
if (SvTRUE(ERRSV)) sv_setsv(ERRSV, &PL_sv_no); 0 2870 2870
if (SvTRUE(ERRSV)) sv_setsv(ERRSV, &PL_sv_no); 0 0 0
116 2870 return r;
117 }
118  
119 /* case-insensitive "does hay contain needle" */
120 713 static int pv_ci_has(const char *hay, STRLEN hl, const char *needle) {
121 713 STRLEN nl = strlen(needle), i, j;
122 /*
123 # uncoverable branch true
124 # uncoverable condition left count:1 class:ignore_covered_err
125 # uncoverable condition left count:2 class:ignore_covered_err */
126 713 ▶ if (nl == 0 || hl < nl) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 0 || hl < nl) return 0; 713 0 713
if (nl == 0 || hl < nl) return 0; 0 713 713
127 731 ▶ for (i = 0; i + nl <= hl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + nl <= hl; i++) { 730 1 731
128 12135 ▶ for (j = 0; j < nl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nl; j++) { 11423 712 12135
129 11423 char a = hay[i + j], b = needle[j];
130 /*
131 # uncoverable branch true
132 # uncoverable condition left count:1 class:ignore_covered_err
133 # uncoverable condition left count:2 class:ignore_covered_err */
134 11423 ▶ if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a'); 10707 716 11423
if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a'); 0 10707 10707
135 /*
136 # uncoverable branch true
137 # uncoverable condition left count:1 class:ignore_covered_err
138 # uncoverable condition left count:2 class:ignore_covered_err */
139 11423 ▶ if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a'); 10710 713 11423
if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a'); 0 10710 10710
140 11423 ▶ if (a != b) break;
Branch: 1 branch
BranchTrueFalseTotal
if (a != b) break; 18 11405 11423
141 }
142 730 ▶ if (j == nl) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == nl) return 1; 712 18 730
143 }
144 1 return 0;
145 }
146  
147 /* canonical JSON of an SV (+1) - the schema cache key */
148 718 static SV *pv_canon(pTHX_ SV *data) {
149 frj_opts o;
150 718 Zero(&o, 1, frj_opts);
151 718 o.canonical = 1;
152 718 return punk_frj(aTHX)->encode(aTHX_ data, &o);
153 }
154  
155 /* ---- the schema cache ----------------------------------------------------- */
156  
157 static HV *PV_CACHE = NULL;
158  
159 /* How many compiled schemas the cache may hold. A schema written once in a
160 * route is one entry for the life of the worker, and an application has tens
161 * of those. One BUILT per request - an enum from a query, say - is a new
162 * entry per distinct shape, without end; past this many the cache starts
163 * over rather than grow with the data. */
164 #define PV_CACHE_MAX 256
165  
166 /* A compiled validator (+1): a blessed Compiled passes through, a hashref
167 * compiles once (coerce on) and caches by canonical JSON. Croaks on
168 * anything else, and on a malformed schema (from the ABI). */
169 720 static SV *pv_compile(pTHX_ SV *schema) {
170 720 ▶ if (SvROK(schema) && SvOBJECT(SvRV(schema)))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(schema) && SvOBJECT(SvRV(schema))) 719 1 720
if (SvROK(schema) && SvOBJECT(SvRV(schema))) 1 718 719
171 1 return newSVsv(schema);
172 719 ▶ if (!(SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 718 1 719
if (!(SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 718 718
173 1 croak("Punk: the validate schema must be a hashref or a "
174 "JSON::Schema::Fast::Compiled");
175 {
176 718 SV *key = sv_2mortal(pv_canon(aTHX_ schema));
177 HE *he;
178 718 ▶ if (!PV_CACHE) PV_CACHE = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PV_CACHE) PV_CACHE = newHV(); 4 714 718
179 718 he = hv_fetch_ent(PV_CACHE, key, 0, 0);
180 718 ▶ if (he) return newSVsv(HeVAL(he));
Branch: 1 branch
BranchTrueFalseTotal
if (he) return newSVsv(HeVAL(he)); 9 709 718
181 {
182 709 SV *compiled = punk_jsf(aTHX)->compile(aTHX_ schema,
183 &PL_sv_undef, 1, 0);
184 709 ▶ if (HvUSEDKEYS(PV_CACHE) >= PV_CACHE_MAX) hv_clear(PV_CACHE); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(PV_CACHE) >= PV_CACHE_MAX) hv_clear(PV_CACHE); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 709 709
if (HvUSEDKEYS(PV_CACHE) >= PV_CACHE_MAX) hv_clear(PV_CACHE); /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 707 709
185 709 (void)hv_store_ent(PV_CACHE, key, compiled, 0); /* cache owns */
186 709 return newSVsv(compiled);
187 }
188 }
189 }
190  
191 /* ---- errors: the Open::API shape plus name -------------------------------- */
192  
193 /* instanceLocation -> the field name a form wants: '/year' -> 'year',
194 * '/a/b' -> 'a.b', '~1'/'~0' unescaped, the root -> '' (+1) */
195 8 static SV *pv_name_of(pTHX_ const char *s, STRLEN l) {
196 8 SV *out = newSVpvs("");
197 /*
198 # uncoverable condition right count:1 class:ignore_covered_err
199 # uncoverable condition right count:2 class:ignore_covered_err */
200 8 ▶ STRLEN i = (l && s[0] == '/') ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
STRLEN i = (l && s[0] == '/') ? 1 : 0; 8 0 8
STRLEN i = (l && s[0] == '/') ? 1 : 0; 8 0 8
201 42 ▶ for (; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < l; i++) { 34 8 42
202 /*
203 # uncoverable branch true
204 # uncoverable condition left count:1 class:ignore_covered_err */
205 34 ▶ if (s[i] == '/') sv_catpvs(out, ".");
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '/') sv_catpvs(out, "."); 0 34 34
206 /*
207 # uncoverable branch true
208 # uncoverable condition left count:1 class:ignore_covered_err
209 # uncoverable condition left count:2 class:ignore_covered_err
210 # uncoverable condition right count:2 class:ignore_covered_err
211 # uncoverable condition left count:3 class:ignore_covered_err
212 # uncoverable condition right count:3 class:ignore_covered_err */
213 34 ▶ else if (s[i] == '~' && i + 1 < l && s[i + 1] == '1') {
Branch: 3 branches
BranchTrueFalseTotal
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '1') { 0 34 34
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '1') { 0 0 0
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '1') { 0 0 0
214 0 sv_catpvs(out, "/"); i++; /* # uncoverable statement */
215 }
216 /*
217 # uncoverable branch true
218 # uncoverable condition left count:1 class:ignore_covered_err
219 # uncoverable condition left count:2 class:ignore_covered_err
220 # uncoverable condition right count:2 class:ignore_covered_err
221 # uncoverable condition left count:3 class:ignore_covered_err
222 # uncoverable condition right count:3 class:ignore_covered_err */
223 34 ▶ else if (s[i] == '~' && i + 1 < l && s[i + 1] == '0') {
Branch: 3 branches
BranchTrueFalseTotal
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '0') { 0 34 34
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '0') { 0 0 0
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '0') { 0 0 0
224 0 sv_catpvs(out, "~"); i++; /* # uncoverable statement */
225 }
226 34 else sv_catpvn(out, s + i, 1);
227 }
228 8 return out;
229 }
230  
231 /* Augment raw JSON::Schema::Fast errors (+1 AV): every error gains `name`;
232 * a root-level `required` failure is expanded to one error per missing
233 * property (when the plain schema and a hash of data are known), so
234 * error($name) lands beside the right field. */
235 10 static AV *pv_augment(pTHX_ AV *errors, HV *schema, SV *data) {
236 10 AV *out = newAV();
237 10 ▶ HV *datah = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *datah = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 10 0 10
HV *datah = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 10 0 10
238 20 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 10 0 10
239 10 SSize_t i, n = av_len(errors) + 1;
240 25 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 10 25
241 15 SV **ep = av_fetch(errors, i, 0);
242 HV *e; SV **kw, **loc;
243 15 const char *locs = ""; STRLEN locl = 0;
244 /*
245 # uncoverable branch true
246 # uncoverable condition left count:1 class:ignore_covered_err
247 # uncoverable condition left count:2 class:ignore_covered_err
248 # uncoverable condition left count:3 class:ignore_covered_err
249 # uncoverable condition left count:4 class:ignore_covered_err */
250 15 ▶ if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV)) 15 0 15
if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV)) 15 0 15
if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV)) 15 0 15
if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV)) 0 15 15
251 7 continue; /* # uncoverable statement class:ignore_covered_err */
252 15 e = (HV *)SvRV(*ep);
253 15 kw = hv_fetchs(e, "keyword", 0);
254 15 loc = hv_fetchs(e, "instanceLocation", 0);
255 /*
256 # uncoverable branch false
257 # uncoverable condition right count:1 class:ignore_covered_err
258 # uncoverable condition right count:2 class:ignore_covered_err
259 # uncoverable condition right count:3 class:ignore_covered_err */
260 30 ▶ if (loc && *loc && SvOK(*loc)) locs = SvPV_const(*loc, locl);
Branch: 3 branches
BranchTrueFalseTotal
if (loc && *loc && SvOK(*loc)) locs = SvPV_const(*loc, locl); 15 0 15
if (loc && *loc && SvOK(*loc)) locs = SvPV_const(*loc, locl); 15 0 15
if (loc && *loc && SvOK(*loc)) locs = SvPV_const(*loc, locl); 15 0 15
261 /*
262 # uncoverable condition right count:1 class:ignore_covered_err
263 # uncoverable condition right count:2 class:ignore_covered_err
264 # uncoverable condition right count:3 class:ignore_covered_err
265 # uncoverable condition right count:5 class:ignore_covered_err
266 # uncoverable condition right count:6 class:ignore_covered_err
267 # uncoverable condition right count:7 class:ignore_covered_err */
268 30 ▶ if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required")
Branch: 4 branches
BranchTrueFalseTotal
if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required") 15 0 15
if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required") 15 0 15
if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required") 15 0 15
if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required") 7 8 15
269 7 ▶ && locl == 0 && schema && datah) {
Branch: 3 branches
BranchTrueFalseTotal
&& locl == 0 && schema && datah) { 7 0 7
&& locl == 0 && schema && datah) { 7 0 7
&& locl == 0 && schema && datah) { 7 0 7
270 7 SV **rq = hv_fetchs(schema, "required", 0);
271 /*
272 # uncoverable branch false
273 # uncoverable condition right count:1 class:ignore_covered_err
274 # uncoverable condition right count:2 class:ignore_covered_err
275 # uncoverable condition right count:3 class:ignore_covered_err
276 # uncoverable condition right count:4 class:ignore_covered_err */
277 7 ▶ if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) { 7 0 7
if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) { 7 0 7
if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) { 7 0 7
if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) { 7 0 7
278 7 AV *req = (AV *)SvRV(*rq);
279 7 SSize_t j, rn = av_len(req) + 1;
280 7 int missing = 0;
281 14 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 7 7 14
282 7 SV **pp = av_fetch(req, j, 0);
283 STRLEN pl; const char *p;
284 HV *ne; SV *iloc;
285 /*
286 # uncoverable branch true
287 # uncoverable condition left count:1 class:ignore_covered_err
288 # uncoverable condition left count:2 class:ignore_covered_err
289 # uncoverable condition left count:3 class:ignore_covered_err */
290 7 ▶ if (!(pp && *pp && SvOK(*pp))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(pp && *pp && SvOK(*pp))) continue; 7 0 7
if (!(pp && *pp && SvOK(*pp))) continue; 7 0 7
if (!(pp && *pp && SvOK(*pp))) continue; 0 7 7
291 7 p = SvPV_const(*pp, pl);
292 /*
293 # uncoverable branch true
294 # uncoverable condition left count:1 class:ignore_covered_err */
295 7 ▶ if (hv_exists(datah, p, (I32)pl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(datah, p, (I32)pl)) continue; 0 7 7
296 7 ne = newHVhv(e);
297 7 iloc = newSVpvs("/");
298 7 sv_catpvn(iloc, p, pl);
299 7 (void)hv_stores(ne, "instanceLocation", iloc);
300 7 (void)hv_stores(ne, "name", newSVpvn(p, pl));
301 7 (void)hv_stores(ne, "message",
302 newSVpvf("missing required property '%.*s'",
303 (int)pl, p));
304 7 av_push(out, newRV_noinc((SV *)ne));
305 7 missing = 1;
306 }
307 /*
308 # uncoverable branch false
309 # uncoverable condition right count:1 class:ignore_covered_err */
310 7 ▶ if (missing) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (missing) continue; 7 0 7
311 }
312 }
313 {
314 8 HV *ne = newHVhv(e);
315 8 (void)hv_stores(ne, "name", pv_name_of(aTHX_ locs, locl));
316 8 av_push(out, newRV_noinc((SV *)ne));
317 }
318 }
319 10 return out;
320 }
321  
322 /* ---- the data sources ----------------------------------------------------- */
323  
324 713 static SV *pv_source_json(pTHX_ SV *c) {
325 713 SV *req = pv_meth_ev(aTHX_ c, "req", NULL, 0);
326 713 SV *body = NULL;
327 /*
328 # uncoverable branch false
329 # uncoverable condition right count:1 class:ignore_covered_err */
330 713 ▶ if (req) {
Branch: 1 branch
BranchTrueFalseTotal
if (req) { 713 0 713
331 713 body = pv_meth_ev(aTHX_ req, "json", NULL, 0);
332 713 SvREFCNT_dec(req);
333 }
334 /*
335 # uncoverable branch false
336 # uncoverable condition right count:1 class:ignore_covered_err
337 # uncoverable condition right count:2 class:ignore_covered_err */
338 713 ▶ if (body && SvROK(body)) return body;
Branch: 2 branches
BranchTrueFalseTotal
if (body && SvROK(body)) return body; 713 0 713
if (body && SvROK(body)) return body; 713 0 713
339 /*
340 # uncoverable statement
341 # uncoverable branch true
342 # uncoverable branch false
343 # uncoverable condition left count:1
344 # uncoverable condition right count:1 */
345 0 ▶ if (body) SvREFCNT_dec(body);
Branch: 1 branch
BranchTrueFalseTotal
if (body) SvREFCNT_dec(body); 0 0 0
346 0 return newRV_noinc((SV *)newHV()); /* # uncoverable statement */
347 }
348  
349 8 static SV *pv_source_params(pTHX_ SV *c) {
350 8 SV *p = pv_meth_ev(aTHX_ c, "params", NULL, 0);
351 /*
352 # uncoverable branch false
353 # uncoverable condition right count:1 class:ignore_covered_err
354 # uncoverable condition right count:2 class:ignore_covered_err
355 # uncoverable condition right count:3 class:ignore_covered_err */
356 8 ▶ if (p && SvROK(p) && SvTYPE(SvRV(p)) == SVt_PVHV) return p;
Branch: 3 branches
BranchTrueFalseTotal
if (p && SvROK(p) && SvTYPE(SvRV(p)) == SVt_PVHV) return p; 8 0 8
if (p && SvROK(p) && SvTYPE(SvRV(p)) == SVt_PVHV) return p; 8 0 8
if (p && SvROK(p) && SvTYPE(SvRV(p)) == SVt_PVHV) return p; 8 0 8
357 /*
358 # uncoverable statement
359 # uncoverable branch true
360 # uncoverable branch false
361 # uncoverable condition left count:1
362 # uncoverable condition right count:1 */
363 0 ▶ if (p) SvREFCNT_dec(p);
Branch: 1 branch
BranchTrueFalseTotal
if (p) SvREFCNT_dec(p); 0 0 0
364 0 return newRV_noinc((SV *)newHV()); /* # uncoverable statement */
365 }
366  
367 /* auto: the decoded JSON body for a JSON request, the merged params
368 * (captures, query, form) otherwise (+1) */
369 718 static SV *pv_source_auto(pTHX_ SV *c) {
370 718 SV *req = pv_meth_ev(aTHX_ c, "req", NULL, 0);
371 718 int is_json = 0;
372 /*
373 # uncoverable branch false
374 # uncoverable condition right count:1 class:ignore_covered_err */
375 718 ▶ if (req) {
Branch: 1 branch
BranchTrueFalseTotal
if (req) { 718 0 718
376 SV *argv[1], *ct;
377 718 argv[0] = sv_2mortal(newSVpvs("content-type"));
378 718 ct = pv_meth_ev(aTHX_ req, "header", argv, 1);
379 718 ▶ if (ct && SvOK(ct)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ct && SvOK(ct)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 718 0 718
if (ct && SvOK(ct)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 713 5 718
380 713 STRLEN l; const char *s = SvPV_const(ct, l);
381 713 is_json = pv_ci_has(s, l, "application/json");
382 }
383 /*
384 # uncoverable branch false
385 # uncoverable condition right count:1 class:ignore_covered_err */
386 718 ▶ if (ct) SvREFCNT_dec(ct);
Branch: 1 branch
BranchTrueFalseTotal
if (ct) SvREFCNT_dec(ct); 718 0 718
387 718 SvREFCNT_dec(req);
388 }
389 718 ▶ return is_json ? pv_source_json(aTHX_ c) : pv_source_params(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
return is_json ? pv_source_json(aTHX_ c) : pv_source_params(aTHX_ c); 712 6 718
390 }
391  
392 /* ---- the Result ----------------------------------------------------------- */
393  
394 /* bless { errors, data, schema? } (+1); owns errs */
395 722 static SV *pv_result_new(pTHX_ AV *errs, SV *data, SV *plain_schema) {
396 722 HV *h = newHV();
397 722 (void)hv_stores(h, "errors", newRV_noinc((SV *)errs));
398 722 (void)hv_stores(h, "data", newSVsv(data));
399 722 ▶ if (plain_schema)
Branch: 1 branch
BranchTrueFalseTotal
if (plain_schema) 721 1 722
400 721 (void)hv_stores(h, "schema", newSVsv(plain_schema));
401 722 return sv_bless(newRV_noinc((SV *)h),
402 gv_stashpvs(PV_RESULT_CLASS, GV_ADD));
403 }
404  
405 731 static HV *pv_result_hv(pTHX_ SV *self) {
406 /*
407 # uncoverable branch true
408 # uncoverable condition left count:1 class:ignore_covered_err
409 # uncoverable condition left count:2 class:ignore_covered_err
410 # uncoverable condition left count:3 class:ignore_covered_err */
411 731 ▶ if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 731 0 731
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 731 0 731
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 0 731 731
412 0 croak("Punk: not a " PV_RESULT_CLASS); /* # uncoverable statement */
413 731 return (HV *)SvRV(self);
414 }
415  
416 722 static AV *pv_result_errors(pTHX_ HV *h) {
417 722 SV **e = hv_fetchs(h, "errors", 0);
418 722 ▶ return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 722 0 722
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 722 0 722
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 722 0 722
419 1444 ▶ ? (AV *)SvRV(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*e) : NULL; 722 0 722
420 }
421  
422 /* valid / valid($name): only declared properties, numeric strings numified
423 * for integer/number, booleans canonical 1/0 - the shapes $c->openapi
424 * delivers. Without a plain schema (a precompiled validator) the data
425 * comes back as-is (+1). */
426 5 static SV *pv_result_valid(pTHX_ HV *h, SV *name) {
427 5 SV **d = hv_fetchs(h, "data", 0);
428 5 SV **s = hv_fetchs(h, "schema", 0);
429 /*
430 # uncoverable condition right count:1 class:ignore_covered_err
431 # uncoverable condition right count:2 class:ignore_covered_err */
432 5 ▶ SV *data = (d && *d) ? *d : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *data = (d && *d) ? *d : &PL_sv_undef; 5 0 5
SV *data = (d && *d) ? *d : &PL_sv_undef; 5 0 5
433 5 ▶ HV *datah = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *datah = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 5 0 5
434 10 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 5 0 5
435 5 HV *props = NULL;
436 /*
437 # uncoverable condition right count:2 class:ignore_covered_err
438 # uncoverable condition right count:3 class:ignore_covered_err
439 # uncoverable condition right count:4 class:ignore_covered_err */
440 5 ▶ if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) { 4 1 5
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) { 4 0 4
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) { 4 0 4
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) { 4 0 4
441 4 SV **pp = hv_fetchs((HV *)SvRV(*s), "properties", 0);
442 /*
443 # uncoverable branch false
444 # uncoverable condition right count:1 class:ignore_covered_err
445 # uncoverable condition right count:2 class:ignore_covered_err
446 # uncoverable condition right count:3 class:ignore_covered_err
447 # uncoverable condition right count:4 class:ignore_covered_err */
448 4 ▶ if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV) 4 0 4
if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV) 4 0 4
if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV) 4 0 4
if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV) 4 0 4
449 4 props = (HV *)SvRV(*pp);
450 }
451 5 ▶ if (!props || !datah) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!props || !datah) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 4 1 5
if (!props || !datah) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 4 4
452 /*
453 # uncoverable branch true
454 # uncoverable condition left count:1 class:ignore_covered_err
455 # uncoverable condition right count:1 class:ignore_covered_err
456 # uncoverable condition left count:2 class:ignore_covered_err */
457 1 ▶ if (name && SvOK(name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (name && SvOK(name)) { 1 0 1
if (name && SvOK(name)) { 0 1 1
458 /*
459 # uncoverable statement
460 # uncoverable condition left count:1
461 # uncoverable condition right count:1 */
462 0 ▶ HE *he = datah ? hv_fetch_ent(datah, name, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = datah ? hv_fetch_ent(datah, name, 0, 0) : NULL; 0 0 0
463 /*
464 # uncoverable statement
465 # uncoverable condition left count:1
466 # uncoverable condition right count:1 */
467 0 ▶ return he ? newSVsv(HeVAL(he)) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return he ? newSVsv(HeVAL(he)) : newSV(0); 0 0 0
468 }
469 1 ▶ return datah ? newRV_noinc((SV *)newHVhv(datah)) : newSVsv(data); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return datah ? newRV_noinc((SV *)newHVhv(datah)) : newSVsv(data); /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
470 }
471 {
472 4 HV *out = newHV();
473 HE *he;
474 4 hv_iterinit(props);
475 12 ▶ while ((he = hv_iternext(props))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(props))) { 8 4 12
476 8 SV *k = hv_iterkeysv(he);
477 8 HE *dhe = hv_fetch_ent(datah, k, 0, 0);
478 8 SV *v, *prop, *nv = NULL;
479 8 const char *type = NULL;
480 /*
481 # uncoverable branch true
482 # uncoverable condition left count:1 class:ignore_covered_err */
483 8 ▶ if (!dhe) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!dhe) continue; 0 8 8
484 8 v = HeVAL(dhe);
485 8 prop = HeVAL(he);
486 /*
487 # uncoverable branch false
488 # uncoverable condition right count:1 class:ignore_covered_err
489 # uncoverable condition right count:2 class:ignore_covered_err
490 # uncoverable condition right count:3 class:ignore_covered_err */
491 8 ▶ if (prop && SvROK(prop) && SvTYPE(SvRV(prop)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (prop && SvROK(prop) && SvTYPE(SvRV(prop)) == SVt_PVHV) { 8 0 8
if (prop && SvROK(prop) && SvTYPE(SvRV(prop)) == SVt_PVHV) { 8 0 8
if (prop && SvROK(prop) && SvTYPE(SvRV(prop)) == SVt_PVHV) { 8 0 8
492 8 SV **tp = hv_fetchs((HV *)SvRV(prop), "type", 0);
493 /*
494 # uncoverable branch false
495 # uncoverable condition right count:1 class:ignore_covered_err
496 # uncoverable condition right count:2 class:ignore_covered_err
497 # uncoverable condition right count:3 class:ignore_covered_err */
498 16 ▶ if (tp && *tp && SvOK(*tp)) type = SvPV_nolen(*tp);
Branch: 3 branches
BranchTrueFalseTotal
if (tp && *tp && SvOK(*tp)) type = SvPV_nolen(*tp); 8 0 8
if (tp && *tp && SvOK(*tp)) type = SvPV_nolen(*tp); 8 0 8
if (tp && *tp && SvOK(*tp)) type = SvPV_nolen(*tp); 8 0 8
499 }
500 /*
501 # uncoverable condition right count:1 class:ignore_covered_err
502 # uncoverable condition right count:2 class:ignore_covered_err
503 # uncoverable condition right count:3 class:ignore_covered_err */
504 8 ▶ if (type && v && SvOK(v) && !SvROK(v)) {
Branch: 4 branches
BranchTrueFalseTotal
if (type && v && SvOK(v) && !SvROK(v)) { 8 0 8
if (type && v && SvOK(v) && !SvROK(v)) { 8 0 8
if (type && v && SvOK(v) && !SvROK(v)) { 8 0 8
if (type && v && SvOK(v) && !SvROK(v)) { 7 1 8
505 /*
506 # uncoverable condition left count:2 class:ignore_covered_err
507 # uncoverable condition right count:3 class:ignore_covered_err */
508 7 ▶ if ((strEQ(type, "integer") || strEQ(type, "number"))
Branch: 2 branches
BranchTrueFalseTotal
if ((strEQ(type, "integer") || strEQ(type, "number")) 3 4 7
if ((strEQ(type, "integer") || strEQ(type, "number")) 0 3 3
509 4 ▶ && looks_like_number(v)) {
Branch: 1 branch
BranchTrueFalseTotal
&& looks_like_number(v)) { 4 0 4
510 4 nv = strEQ(type, "integer") ? newSViv((IV)SvNV(v))
511 4 ▶ : newSVnv(SvNV(v)); /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
: newSVnv(SvNV(v)); /* # uncoverable statement class:ignore_covered_err */ 4 0 4
512 }
513 3 ▶ else if (strEQ(type, "boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(type, "boolean")) { 1 2 3
514 1 STRLEN vl; const char *vs = SvPV_const(v, vl);
515 1 int truthy = SvTRUE(v)
516 1 ▶ && !(vl == 5 && memEQ(vs, "false", 5))
Branch: 2 branches
BranchTrueFalseTotal
&& !(vl == 5 && memEQ(vs, "false", 5)) 1 0 1
&& !(vl == 5 && memEQ(vs, "false", 5)) 0 1 1
517 2 ▶ && !(vl == 1 && vs[0] == '0');
Branch: 3 branches
BranchTrueFalseTotal
&& !(vl == 1 && vs[0] == '0'); 1 0 1
&& !(vl == 1 && vs[0] == '0'); 0 0 0
&& !(vl == 1 && vs[0] == '0'); 0 0 0
518 1 nv = newSViv(truthy ? 1 : 0);
519 }
520 }
521 8 ▶ (void)hv_store_ent(out, k, nv ? nv : newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store_ent(out, k, nv ? nv : newSVsv(v), 0); 3 5 8
522 }
523 4 ▶ if (name && SvOK(name)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (name && SvOK(name)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
if (name && SvOK(name)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 2 4
524 2 HE *ohe = hv_fetch_ent(out, name, 0, 0);
525 2 ▶ SV *r = ohe ? newSVsv(HeVAL(ohe)) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *r = ohe ? newSVsv(HeVAL(ohe)) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
526 2 SvREFCNT_dec((SV *)out);
527 2 return r;
528 }
529 2 return newRV_noinc((SV *)out);
530 }
531 }
532  
533 /* ---- run one validation --------------------------------------------------- */
534  
535 /* validate `data` against a compiled schema, stash and return the Result
536 * (+1). plain_schema is the hashref form when known (drives `required`
537 * expansion and valid()'s property filter), else NULL. */
538 722 static SV *pv_run(pTHX_ SV *c, SV *compiled, SV *plain_schema, SV *data) {
539 722 AV *raw = (AV *)sv_2mortal((SV *)newAV());
540 722 int ok = punk_jsf(aTHX)->validate(aTHX_ compiled, data, raw);
541 721 ▶ HV *sh = (plain_schema && SvROK(plain_schema)
Branch: 1 branch
BranchTrueFalseTotal
HV *sh = (plain_schema && SvROK(plain_schema) 721 0 721
542 721 ▶ && SvTYPE(SvRV(plain_schema)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(plain_schema)) == SVt_PVHV) 721 0 721
543 1443 ▶ ? (HV *)SvRV(plain_schema) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(plain_schema) : NULL; 721 1 722
544 722 ▶ AV *errs = ok ? newAV() : pv_augment(aTHX_ raw, sh, data);
Branch: 1 branch
BranchTrueFalseTotal
AV *errs = ok ? newAV() : pv_augment(aTHX_ raw, sh, data); 712 10 722
545 722 ▶ SV *r = pv_result_new(aTHX_ errs, data, sh ? plain_schema : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *r = pv_result_new(aTHX_ errs, data, sh ? plain_schema : NULL); 721 1 722
546 722 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c));
547 722 (void)hv_stores(stash, "punk.validation", newSVsv(r));
548 722 return r;
549 }
550  
551 /* $c->validate($schema, $data?) (+1 Result) */
552 713 static SV *pv_validate(pTHX_ SV *c, SV *schema, SV *source) {
553 713 SV *compiled = sv_2mortal(pv_compile(aTHX_ schema));
554 712 ▶ SV *plain = (SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV *plain = (SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV) 711 1 712
555 1424 ▶ ? schema : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? schema : NULL; 712 0 712
556 712 ▶ SV *data = (source && SvROK(source)) ? newSVsv(source)
Branch: 1 branch
BranchTrueFalseTotal
SV *data = (source && SvROK(source)) ? newSVsv(source) 1 711 712
557 1423 ▶ : pv_source_auto(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
: pv_source_auto(aTHX_ c); 712 0 712
558 SV *r;
559 712 sv_2mortal(data);
560 712 r = pv_run(aTHX_ c, compiled, plain, data);
561 712 return r;
562 }
563  
564 /* ---- the route guard ------------------------------------------------------ */
565  
566 /* capture: [ compiled, plain-schema-or-undef, source-or-undef,
567 * on_invalid-or-undef ] */
568 XS_INTERNAL(pv_guard_cb);
569 10 XS_INTERNAL(pv_guard_cb) {
570 10 dXSARGS;
571 10 AV *cap = punk_clos_cap(aTHX_ cv);
572 SV *c, *compiled, *plain, *srcv, *oi, *data, *r;
573 AV *errs;
574 /*
575 # uncoverable branch true
576 # uncoverable condition left count:1 class:ignore_covered_err
577 # uncoverable condition left count:2 class:ignore_covered_err */
578 10 ▶ if (items < 1 || !cap) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (items < 1 || !cap) XSRETURN_EMPTY; 10 0 10
if (items < 1 || !cap) XSRETURN_EMPTY; 0 10 10
579 10 c = ST(0);
580 10 compiled = *av_fetch(cap, 0, 0);
581 10 plain = *av_fetch(cap, 1, 0);
582 10 srcv = *av_fetch(cap, 2, 0);
583 10 oi = *av_fetch(cap, 3, 0);
584  
585 10 ▶ if (SvOK(srcv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(srcv)) { 3 7 10
586 3 data = strEQ(SvPV_nolen(srcv), "json") ? pv_source_json(aTHX_ c)
587 3 ▶ : pv_source_params(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
: pv_source_params(aTHX_ c); 1 2 3
588 }
589 7 else data = pv_source_auto(aTHX_ c);
590 10 sv_2mortal(data);
591  
592 10 ▶ r = sv_2mortal(pv_run(aTHX_ c, compiled,
Branch: 1 branch
BranchTrueFalseTotal
r = sv_2mortal(pv_run(aTHX_ c, compiled, 10 0 10
593 SvOK(plain) ? plain : NULL, data));
594 10 errs = pv_result_errors(aTHX_ pv_result_hv(aTHX_ r));
595 10 ▶ if (!errs || av_len(errs) < 0) XSRETURN_EMPTY; /* valid: continue */ /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!errs || av_len(errs) < 0) XSRETURN_EMPTY; /* valid: continue */ /* # uncoverable condition left count:1 class:ignore_covered_err */ 10 0 10
if (!errs || av_len(errs) < 0) XSRETURN_EMPTY; /* valid: continue */ /* # uncoverable condition left count:1 class:ignore_covered_err */ 6 4 10
596  
597 4 ▶ if (SvOK(oi)) { /* the app's own failure response */
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(oi)) { /* the app's own failure response */ 1 3 4
598 1 int died = 0;
599 1 SV *out = pd_call(aTHX_ oi, c, &died);
600 /*
601 # uncoverable branch true
602 # uncoverable condition left count:1 class:ignore_covered_err
603 # uncoverable condition left count:2 class:ignore_covered_err
604 # uncoverable condition right count:2 class:ignore_covered_err
605 # uncoverable condition left count:3 class:ignore_covered_err
606 # uncoverable condition right count:3 class:ignore_covered_err */
607 1 ▶ if (died) { if (out) SvREFCNT_dec(out); croak_sv(ERRSV); }
Branch: 3 branches
BranchTrueFalseTotal
if (died) { if (out) SvREFCNT_dec(out); croak_sv(ERRSV); } 0 1 1
if (died) { if (out) SvREFCNT_dec(out); croak_sv(ERRSV); } 0 0 0
if (died) { if (out) SvREFCNT_dec(out); croak_sv(ERRSV); } 0 0 0
608 1 ▶ ST(0) = out ? sv_2mortal(out) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = out ? sv_2mortal(out) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
609 1 XSRETURN(1);
610 }
611 { /* the OpenAPI-mount-shaped 400 */
612 3 HV *body = newHV();
613 SV *argv[2], *resp;
614 3 (void)hv_stores(body, "errors", newRV_inc((SV *)errs));
615 3 argv[0] = sv_2mortal(newRV_noinc((SV *)body));
616 3 argv[1] = sv_2mortal(newSViv(400));
617 3 resp = pcx_call_meth(aTHX_ c, "json", argv, 2, 1);
618 3 ▶ ST(0) = resp ? sv_2mortal(resp) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = resp ? sv_2mortal(resp) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
619 3 XSRETURN(1);
620 }
621 }
622  
623 /* ---- boot: compile the route-level validate options ----------------------- */
624  
625 /* Called from Punk::App::compile_extras (after the router compiled): parse
626 * each validate spec, compile its schema once, resolve on_invalid, and
627 * APPEND a guard closure to the record - after any auth guards, so a 401
628 * still costs no body parse. punk_serve walks the same guards AV with no
629 * idea anything changed. */
630 6 static void pv_compile_routes(pTHX_ SV *self) {
631 6 HV *h = (HV *)SvRV(self);
632 6 SV **vrp = hv_fetchs(h, K_VALIDATE_ROUTES, 0);
633 AV *vrs;
634 HV *by;
635 SSize_t i, n;
636 /*
637 # uncoverable branch true
638 # uncoverable condition left count:1 class:ignore_covered_err
639 # uncoverable condition left count:2 class:ignore_covered_err
640 # uncoverable condition left count:3 class:ignore_covered_err
641 # uncoverable condition left count:4 class:ignore_covered_err */
642 6 ▶ if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV)) 6 0 6
if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV)) 6 0 6
if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV)) 6 0 6
if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV)) 0 6 6
643 0 return; /* # uncoverable statement */
644 6 vrs = (AV *)SvRV(*vrp);
645 6 n = av_len(vrs) + 1;
646 /*
647 # uncoverable branch true
648 # uncoverable condition left count:1 class:ignore_covered_err */
649 6 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 6 6
650 6 by = pk_route_index(aTHX_ self, "validate");
651  
652 12 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 3 12
653 9 SV **vp = av_fetch(vrs, i, 0);
654 HV *vr, *rec;
655 SV **m, **p, **sp;
656 9 SV *where, *spec, *schema, *src = NULL, *oi = NULL;
657 9 SV *compiled, *resolved = NULL;
658 HE *he;
659 /*
660 # uncoverable branch true
661 # uncoverable condition left count:1 class:ignore_covered_err
662 # uncoverable condition left count:2 class:ignore_covered_err
663 # uncoverable condition left count:3 class:ignore_covered_err
664 # uncoverable condition left count:4 class:ignore_covered_err */
665 9 ▶ if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV)) 9 0 9
if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV)) 9 0 9
if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV)) 9 0 9
if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV)) 0 9 9
666 0 continue; /* # uncoverable statement */
667 9 vr = (HV *)SvRV(*vp);
668 9 m = hv_fetchs(vr, K_METHOD, 0);
669 9 p = hv_fetchs(vr, K_PATH, 0);
670 9 sp = hv_fetchs(vr, K_VALIDATE, 0);
671 /*
672 # uncoverable branch true
673 # uncoverable condition left count:1 class:ignore_covered_err
674 # uncoverable condition left count:2 class:ignore_covered_err
675 # uncoverable condition left count:3 class:ignore_covered_err
676 # uncoverable condition left count:4 class:ignore_covered_err
677 # uncoverable condition left count:5 class:ignore_covered_err
678 # uncoverable condition left count:6 class:ignore_covered_err */
679 9 ▶ if (!(m && *m && p && *p && sp && *sp)) continue;
Branch: 6 branches
BranchTrueFalseTotal
if (!(m && *m && p && *p && sp && *sp)) continue; 9 0 9
if (!(m && *m && p && *p && sp && *sp)) continue; 9 0 9
if (!(m && *m && p && *p && sp && *sp)) continue; 9 0 9
if (!(m && *m && p && *p && sp && *sp)) continue; 9 0 9
if (!(m && *m && p && *p && sp && *sp)) continue; 9 0 9
if (!(m && *m && p && *p && sp && *sp)) continue; 0 9 9
680 9 where = sv_2mortal(newSVsv(*m));
681 9 sv_catpvs(where, " ");
682 9 sv_catsv(where, *p);
683  
684 9 he = hv_fetch_ent(by, where, 0, 0);
685 /*
686 # uncoverable branch true
687 # uncoverable condition left count:1 class:ignore_covered_err */
688 9 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 0 9 9
689 0 croak("Punk: validate on unknown route %s", SvPV_nolen(where)); /* # uncoverable statement */
690 9 rec = (HV *)SvRV(HeVAL(he));
691  
692 9 spec = *sp;
693 9 schema = spec;
694 /*
695 # uncoverable condition right count:1 class:ignore_covered_err
696 # uncoverable condition right count:2 class:ignore_covered_err */
697 9 ▶ if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV 9 0 9
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV 9 0 9
698 9 ▶ && hv_exists((HV *)SvRV(spec), "schema", 6)) {
Branch: 1 branch
BranchTrueFalseTotal
&& hv_exists((HV *)SvRV(spec), "schema", 6)) { 5 4 9
699 5 HV *sh = (HV *)SvRV(spec);
700 HE *ke;
701 SV **x;
702 5 hv_iterinit(sh);
703 13 ▶ while ((ke = hv_iternext(sh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ke = hv_iternext(sh))) { 9 4 13
704 9 ▶ STRLEN kl; const char *k = HePV(ke, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(ke, kl); 0 9 9
705 9 ▶ if (!(strEQ(k, "schema") || strEQ(k, "source")
Branch: 2 branches
BranchTrueFalseTotal
if (!(strEQ(k, "schema") || strEQ(k, "source") 5 4 9
if (!(strEQ(k, "schema") || strEQ(k, "source") 2 3 5
706 2 ▶ || strEQ(k, "on_invalid")))
Branch: 1 branch
BranchTrueFalseTotal
|| strEQ(k, "on_invalid"))) 1 1 2
707 1 croak("Punk: unknown validate option '%s' on %s",
708 k, SvPV_nolen(where));
709 }
710 4 x = hv_fetchs(sh, "schema", 0);
711 /*
712 # uncoverable condition right count:1 class:ignore_covered_err
713 # uncoverable condition right count:2 class:ignore_covered_err */
714 4 ▶ schema = (x && *x) ? *x : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
schema = (x && *x) ? *x : &PL_sv_undef; 4 0 4
schema = (x && *x) ? *x : &PL_sv_undef; 4 0 4
715 4 x = hv_fetchs(sh, "source", 0);
716 /*
717 # uncoverable condition right count:2 class:ignore_covered_err
718 # uncoverable condition right count:3 class:ignore_covered_err */
719 4 ▶ if (x && *x && SvOK(*x)) src = *x;
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) src = *x; 3 1 4
if (x && *x && SvOK(*x)) src = *x; 3 0 3
if (x && *x && SvOK(*x)) src = *x; 3 0 3
720 4 x = hv_fetchs(sh, "on_invalid", 0);
721 /*
722 # uncoverable condition right count:2 class:ignore_covered_err
723 # uncoverable condition right count:3 class:ignore_covered_err */
724 4 ▶ if (x && *x && SvOK(*x)) oi = *x;
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) oi = *x; 1 3 4
if (x && *x && SvOK(*x)) oi = *x; 1 0 1
if (x && *x && SvOK(*x)) oi = *x; 1 0 1
725 }
726  
727 /*
728 # uncoverable condition left count:2 class:ignore_covered_err
729 # uncoverable condition right count:2 class:ignore_covered_err
730 # uncoverable condition left count:3 class:ignore_covered_err */
731 8 ▶ if (!(SvROK(schema)
Branch: 1 branch
BranchTrueFalseTotal
if (!(SvROK(schema) 7 1 8
732 7 ▶ && (SvOBJECT(SvRV(schema))
Branch: 1 branch
BranchTrueFalseTotal
&& (SvOBJECT(SvRV(schema)) 7 0 7
733 7 ▶ || SvTYPE(SvRV(schema)) == SVt_PVHV)))
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(schema)) == SVt_PVHV))) 0 7 7
734 1 croak("Punk: invalid validate schema on %s: the schema must be "
735 "a hashref or a JSON::Schema::Fast::Compiled",
736 SvPV_nolen(where));
737 7 compiled = pv_compile(aTHX_ schema); /* croaks on a bad schema */
738  
739 10 ▶ if (src && !(strEQ(SvPV_nolen(src), "params") /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (src && !(strEQ(SvPV_nolen(src), "params") /* # uncoverable condition right count:3 class:ignore_covered_err */ 3 4 7
if (src && !(strEQ(SvPV_nolen(src), "params") /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 1 3
740 2 ▶ || strEQ(SvPV_nolen(src), "json"))) {
Branch: 1 branch
BranchTrueFalseTotal
|| strEQ(SvPV_nolen(src), "json"))) { 1 1 2
741 1 SvREFCNT_dec(compiled);
742 1 croak("Punk: validate source on %s must be 'params' or 'json'",
743 SvPV_nolen(where));
744 }
745 6 ▶ if (oi) {
Branch: 1 branch
BranchTrueFalseTotal
if (oi) { 1 5 6
746 SV *argv[2];
747 1 argv[0] = oi;
748 1 argv[1] = sv_2mortal(newSVpvs("on_invalid"));
749 1 resolved = pcx_call_meth(aTHX_ self, "_resolve_target",
750 argv, 2, 1); /* croaks on a typo */
751 }
752  
753 {
754 6 AV *cap = newAV();
755 6 av_push(cap, compiled); /* owns the +1 */
756 6 ▶ av_push(cap, (SvROK(schema)
Branch: 2 branches
BranchTrueFalseTotal
av_push(cap, (SvROK(schema) 6 0 6
av_push(cap, (SvROK(schema) 6 0 6
757 && SvTYPE(SvRV(schema)) == SVt_PVHV)
758 ? newSVsv(schema) : newSV(0));
759 6 ▶ av_push(cap, src ? newSVsv(src) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, src ? newSVsv(src) : newSV(0)); 2 4 6
760 6 ▶ av_push(cap, resolved ? resolved : newSV(0)); /* owns the +1 */
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, resolved ? resolved : newSV(0)); /* owns the +1 */ 5 1 6
761 6 pk_route_guard_push(aTHX_ rec,
762 punk_closure(aTHX_ pv_guard_cb, cap));
763 }
764 }
765 }
766  
767 #endif /* PUNK_VALIDATE_H */
« xs/model.xs ↑ Summary include/punk/punk_compile.h »