Apologies for the imprecise question here. I am trying to approach something I've been programming against from a higher level. I have an API written (in Dancer2) which accepts parameters in JSON and does some CRUD operations. I have written a few different data verifiers that have so far been inadequate for covering different incoming JSON objects and they don't really hold up across all of the different data structures that come in to the API.

Is there a module or method for writing an object template (whether in JSON or in plain Perl) that I can use to verify that data coming into the API matches a flat or nested Perl data structure?

For example, let's say there is an API method which returns a list of ZIP codes matching a US state. The incoming JSON would look like this:

{ "token":"blah", "us_states":["NJ","MI"] }

What I would like to do is write a closure that is given an "object model" or "data structure" and returns a function that can verify the incoming data matches the given model. An input to the closure might look like this:

{ 'token' => &verify_token, 'us_states' => &verify_state_array, }

I say it would be a closure because the API provides a bunch of different methods for querying information about a state and therefore there will be a multiple methods that need to verify the "us_states" array values and they can use the verify_state_hash function.

For nested incoming data it would be great to be able to traverse the structure and match input keys to ready-made verification functions that are reusable (switched back to Perl format for data structures for some reason):

{ 'token' => 'blah', 'new_exchanges' => [ {'NJ' => [{'201' => ['200','202']},{'202' => ['200','201']}]}, {'MA' => [{'617' => ['200','202']},{'949' => ['200','201']}]}, ] }

(I accept that the data is formatted in a very dumb way in this example, I was just trying to show a well-nested data structure).

Again, apologies for the meandering nature of this post I think this is something I could write myself but I am concerned with the edge cases when traversing a data structure where I cannot control what is received by the API and it seems there might be a module already created to do something like this. Or perhaps I'm going down a terrible path anyways and someone could gently nudge me back on course with a suggestion. To be clear I'm not looking for anyone to write any code for me, just trying to figure out a strategy for resolving what must be a common problem for munging incoming data.

Thanks!


In reply to Data Object Verification Modules? by mwb613

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.