开发者

Should you provide validatation for the results of a Web Service Call

开发者 https://www.devze.com 2023-04-05 01:57 出处:网络
We\'re building a web service that will return many different types of items. Similar to a web service request on ebay that requests all there inventory.

We're building a web service that will return many different types of items. Similar to a web service request on ebay that requests all there inventory.

Some of the items are well known in structure to us, like trucks and diggers. Others are not, like toasters and audio equipment.

I'd like to return all the information an item has even if that type information is not generic across all other items. In the example below make and Pop Speed are examples of this.

So i can return information like this

<inventory>
 <truck>
  <title>Massive Truck</title>
  <make>CAT</make>
  <weight>1200</weight>
 </truck>

 <toaster>
  <title>Quick Toaster</title>
  <Popspeed>20</Popspeed>
 </toaster>
</inventory>

But the issue is that i can't (I don't know of a way) to build a xsd/schema for 开发者_Python百科this. We'll also return this information in JSON so no schema there.

But does it matter?

I would have thought it would be good to know the structure and properties being returned to you but perhaps there is just a verbal/doco agreement and that's enough.


Look at Hal (http://stateless.co/hal_specification.html) it will do exactly what you are looking for. In hal you use link relations to identify the structure of the embedded resource. You won't be able to do XSD on it, but you could use something like schematron or relax-ng to validation the doc.

An xml-based hal version of your doc would look something like:

<resource rel="self" href="http://example.org/inventory" >
   <resource rel="truck" href="http://example.org/inventory/Massive_Truck"> 
      <title>Massive Truck</title>
      <make>CAT</make>
      <weight>1200</weight>
   </resource>
   <resource rel="toaster" href="http://example.org/inventory/Quick_toaster"> 
      <title>Quick Toaster</title>
      <Popspeed>20</Popspeed>
   </resource>
</resource>

As you will see from the hal spec, there is an equivalent JSON representation that you can use.

0

精彩评论

暂无评论...
验证码 换一张
取 消