iridakos/duckrails

Validating incoming data

Closed this issue · 3 comments

Hi, this is a cool tool, but a little outside what I was hoping to use it for.

I'm building an XML API which is going to spend most of it's time receiving data rather than serving it - as such, the most important bit is the format in which the data is received - that is, does it match what I'll be expecting?

If something which allows for this type of validation could be added, that would make my life much easier.

Thank you.

Hi,

Actually, you do have access to the original request itself thus in the advanced configuration of the mock you may proceed to any kind of validation of the sent data and decide the response status based on the outcome.

Though, since XML format is pretty common, let me check if the required ruby gems are included in the application so that you can process it and get back to you with both a possible fix and an example dealing with the case you described. It's something that definitely fits in the application's purpose.

Thank you for the feedback.

I have added the libxml-ruby gem to the application's Gemfile and now you can use it to do XML stuff (including validation against dtd & xsd). Find below the code I used to see that it's working.

Example: (in the mock's advanced tab)

<%
  valid = begin
    dtd = LibXML::XML::Dtd.new '<!ELEMENT root (item*) > <!ELEMENT item (#PCDATA) >'
    xml = LibXML::XML::Document.string @request.body.read
    xml.validate dtd
  rescue => e
    body = e.message
    false
  end

 status = valid ? 204 : 400
%>

<%= 
{
  body:  body,
  status_code: status
}.to_json %>

This is excellent, thank you. I'll see what I can do about having a play with this next week.