| 1 |
|
| MODULE = Punk PACKAGE = Punk::Validate |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| # The boot half (punk_validate.h): called from Punk::App::compile_extras |
| 6 |
|
| # when any route carried a validate option. Compiles each schema once |
| 7 |
|
| # through the JSON::Schema::Fast C ABI, resolves on_invalid, and appends a |
| 8 |
|
| # C guard closure to the compiled record - after any auth guards. |
| 9 |
|
| void |
| 10 |
|
| _compile_routes(self) |
| 11 |
|
| SV *self |
| 12 |
|
| CODE: |
| 13 |
5 |
| pv_compile_routes(aTHX_ self); |
| 14 |
|
| |
| 15 |
|
| # How many compiled schemas $c->validate is holding. For the tests: the cache |
| 16 |
|
| # is bounded, and nothing else can see that it is. |
| 17 |
|
| IV |
| 18 |
|
| _cache_count() |
| 19 |
|
| CODE: |
| 20 |
3 |
▶ | RETVAL = PV_CACHE ? (IV)HvUSEDKEYS(PV_CACHE) : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| RETVAL = PV_CACHE ? (IV)HvUSEDKEYS(PV_CACHE) : 0; |
3 |
0 |
3 |
| RETVAL = PV_CACHE ? (IV)HvUSEDKEYS(PV_CACHE) : 0; |
0 |
3 |
3 |
| RETVAL = PV_CACHE ? (IV)HvUSEDKEYS(PV_CACHE) : 0; |
0 |
3 |
3 |
|
| 21 |
|
| OUTPUT: |
| 22 |
|
| RETVAL |
| 23 |
|
| |
| 24 |
|
| MODULE = Punk PACKAGE = Punk::Validate::Result |
| 25 |
|
| |
| 26 |
|
| PROTOTYPES: DISABLE |
| 27 |
|
| |
| 28 |
|
| # The collected outcome of one validation: { errors, data, schema? }, |
| 29 |
|
| # built in C (pv_result_new) and read through these accessors. |
| 30 |
|
| |
| 31 |
|
| # has_errors: the error count - boolean in practice. |
| 32 |
|
| SV * |
| 33 |
|
| has_errors(self) |
| 34 |
|
| SV *self |
| 35 |
|
| CODE: |
| 36 |
|
| { |
| 37 |
710 |
| AV *errs = pv_result_errors(aTHX_ pv_result_hv(aTHX_ self)); |
| 38 |
710 |
▶ | RETVAL = newSViv(errs ? av_len(errs) + 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = newSViv(errs ? av_len(errs) + 1 : 0); |
710 |
0 |
710 |
|
| 39 |
|
| } |
| 40 |
|
| OUTPUT: |
| 41 |
|
| RETVAL |
| 42 |
|
| |
| 43 |
|
| # errors: the arrayref, in the Open::API shape plus name. |
| 44 |
|
| SV * |
| 45 |
|
| errors(self) |
| 46 |
|
| SV *self |
| 47 |
|
| CODE: |
| 48 |
|
| { |
| 49 |
4 |
| HV *h = pv_result_hv(aTHX_ self); |
| 50 |
4 |
| SV **e = hv_fetchs(h, "errors", 0); |
| 51 |
4 |
▶ | RETVAL = (e && *e) ? newSVsv(*e) : newRV_noinc((SV *)newAV()); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (e && *e) ? newSVsv(*e) : newRV_noinc((SV *)newAV()); |
4 |
0 |
4 |
| RETVAL = (e && *e) ? newSVsv(*e) : newRV_noinc((SV *)newAV()); |
4 |
0 |
4 |
|
| 52 |
|
| } |
| 53 |
|
| OUTPUT: |
| 54 |
|
| RETVAL |
| 55 |
|
| |
| 56 |
|
| # The first message for one field - error($name) - or undef. |
| 57 |
|
| SV * |
| 58 |
|
| error(self, name) |
| 59 |
|
| SV *self |
| 60 |
|
| SV *name |
| 61 |
|
| CODE: |
| 62 |
|
| { |
| 63 |
2 |
| AV *errs = pv_result_errors(aTHX_ pv_result_hv(aTHX_ self)); |
| 64 |
2 |
▶ | SSize_t i, n = errs ? av_len(errs) + 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t i, n = errs ? av_len(errs) + 1 : 0; |
2 |
0 |
2 |
|
| 65 |
2 |
| STRLEN wl; const char *want = SvPV_const(name, wl); |
| 66 |
2 |
| RETVAL = NULL; |
| 67 |
5 |
▶ | for (i = 0; i < n && !RETVAL; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n && !RETVAL; i++) { |
4 |
1 |
5 |
| for (i = 0; i < n && !RETVAL; i++) { |
3 |
1 |
4 |
|
| 68 |
3 |
| SV **ep = av_fetch(errs, i, 0); |
| 69 |
|
| HV *e; SV **nm; |
| 70 |
3 |
▶ | if (!(ep && *ep && SvROK(*ep) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(ep && *ep && SvROK(*ep) |
3 |
0 |
3 |
| if (!(ep && *ep && SvROK(*ep) |
3 |
0 |
3 |
| if (!(ep && *ep && SvROK(*ep) |
3 |
0 |
3 |
|
| 71 |
3 |
▶ | && SvTYPE(SvRV(*ep)) == SVt_PVHV)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(*ep)) == SVt_PVHV)) continue; |
0 |
3 |
3 |
|
| 72 |
3 |
| e = (HV *)SvRV(*ep); |
| 73 |
3 |
| nm = hv_fetchs(e, "name", 0); |
| 74 |
3 |
▶ | if (nm && *nm && SvOK(*nm)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (nm && *nm && SvOK(*nm)) { |
3 |
0 |
3 |
| if (nm && *nm && SvOK(*nm)) { |
3 |
0 |
3 |
| if (nm && *nm && SvOK(*nm)) { |
3 |
0 |
3 |
|
| 75 |
3 |
| STRLEN nl; const char *ns = SvPV_const(*nm, nl); |
| 76 |
3 |
▶ | if (nl == wl && memEQ(ns, want, wl)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nl == wl && memEQ(ns, want, wl)) { |
2 |
1 |
3 |
| if (nl == wl && memEQ(ns, want, wl)) { |
2 |
0 |
2 |
|
| 77 |
2 |
| SV **msg = hv_fetchs(e, "message", 0); |
| 78 |
2 |
▶ | RETVAL = (msg && *msg) ? newSVsv(*msg) : newSV(0); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (msg && *msg) ? newSVsv(*msg) : newSV(0); |
2 |
0 |
2 |
| RETVAL = (msg && *msg) ? newSVsv(*msg) : newSV(0); |
2 |
0 |
2 |
|
| 79 |
|
| } |
| 80 |
|
| } |
| 81 |
|
| } |
| 82 |
2 |
▶ | if (!RETVAL) RETVAL = newSV(0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) RETVAL = newSV(0); |
0 |
2 |
2 |
|
| 83 |
|
| } |
| 84 |
|
| OUTPUT: |
| 85 |
|
| RETVAL |
| 86 |
|
| |
| 87 |
|
| # valid / valid($name): typed, filtered params (punk_validate.h). |
| 88 |
|
| SV * |
| 89 |
|
| valid(self, name = &PL_sv_undef) |
| 90 |
|
| SV *self |
| 91 |
|
| SV *name |
| 92 |
|
| CODE: |
| 93 |
5 |
| RETVAL = pv_result_valid(aTHX_ pv_result_hv(aTHX_ self), name); |
| 94 |
|
| OUTPUT: |
| 95 |
|
| RETVAL |
| 96 |
|
| |
| 97 |
|
| # TO_JSON: { errors => [...] } - the Result encodes as the 400 body. |
| 98 |
|
| SV * |
| 99 |
|
| TO_JSON(self) |
| 100 |
|
| SV *self |
| 101 |
|
| CODE: |
| 102 |
|
| { |
| 103 |
0 |
| HV *h = pv_result_hv(aTHX_ self); |
| 104 |
0 |
| SV **e = hv_fetchs(h, "errors", 0); |
| 105 |
0 |
| HV *out = newHV(); |
| 106 |
0 |
▶ | (void)hv_stores(out, "errors", |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_stores(out, "errors", |
0 |
0 |
0 |
| (void)hv_stores(out, "errors", |
0 |
0 |
0 |
|
| 107 |
|
| (e && *e) ? newSVsv(*e) : newRV_noinc((SV *)newAV())); |
| 108 |
0 |
| RETVAL = newRV_noinc((SV *)out); |
| 109 |
|
| } |
| 110 |
|
| OUTPUT: |
| 111 |
|
| RETVAL |