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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |