YAML validation

I am sorting through a heap of yaml files looking for the following
fields.

yml = YAML.load( File.open( yml_file ) )
    feed_list[ yml['name' ] ] = {
       'description' => yml[ 'description' ] || {},
       'active' => yml[ 'active' ],
       'interval' => yml[ 'interval' ],
       'source_net' => net
    }

My problem is that when I encounter a yml file that contains no info (or
worse yaml designed by someone with a limited grasp of yaml) my program
goes splat.

Any ideas or direction for scanning then skipping bad yaml files.

I have captured the ruby exceptions but I want to just skip the files
before it comes to that

Thanks

···

--
Posted via http://www.ruby-forum.com/.

Don't think there is any way to do this. I suppose it might be nice if
there were a syntax check function, but it won't be much different
than actually trying to parse it. And if you are going to use the
result if it parses why would you want to waste time doing so twice?
So catching the error is really the best way to go.

~trans

···

On May 18, 9:13 am, Kevin Austin <nitsuani...@comcast.net> wrote:

I am sorting through a heap of yaml files looking for the following
fields.

yml = YAML.load( File.open( yml_file ) )
feed_list[ yml['name' ] ] = {
'description' => yml[ 'description' ] || {},
'active' => yml[ 'active' ],
'interval' => yml[ 'interval' ],
'source_net' => net
}

My problem is that when I encounter a yml file that contains no info (or
worse yaml designed by someone with a limited grasp of yaml) my program
goes splat.

Any ideas or direction for scanning then skipping bad yaml files.

I have captured the ruby exceptions but I want to just skip the files
before it comes to that

Thomas Sawyer wrote:

Don't think there is any way to do this. I suppose it might be nice if
there were a syntax check function, but it won't be much different
than actually trying to parse it. And if you are going to use the
result if it parses why would you want to waste time doing so twice?
So catching the error is really the best way to go.

~trans

That's what I thought was the case.

Now for my next question

after I capture my error

`sort': comparison of String with nil failed (ArgumentError)

how can I return to my code with my next yaml file?

aka
rescue
do crap
end

a good site or book would be fine

Thanks

···

--
Posted via http://www.ruby-forum.com/\.

Thomas Sawyer wrote:
> Don't think there is any way to do this. I suppose it might be nice if
> there were a syntax check function, but it won't be much different
> than actually trying to parse it. And if you are going to use the
> result if it parses why would you want to waste time doing so twice?
> So catching the error is really the best way to go.

> ~trans

That's what I thought was the case.

Now for my next question

after I capture my error

`sort': comparison of String with nil failed (ArgumentError)

how can I return to my code with my next yaml file?

aka
rescue
do crap
end

probably 'next' would do if your looping over files. Also there is
'retry'.

a good site or book would be fine

http://www.ruby-doc.org/docs/ProgrammingRuby/tut_exceptions.html

···

On May 18, 10:19 am, Kevin Austin <nitsuani...@comcast.net> wrote:

Thomas Sawyer wrote:

···

On May 18, 10:19�am, Kevin Austin <nitsuani...@comcast.net> wrote:

�do crap
end

probably 'next' would do if your looping over files. Also there is
'retry'.

a good site or book would be fine

Programming Ruby: The Pragmatic Programmer's Guide

Working (kind of)

I just purged all the nil spaces for the array

      feeds.delete_if{|x| x.nil?}

So that ignores the nil spaces of the array. Now I just have to figure
out how to ignore the incorrectly formatted yamls.

Thanks again for your help.

--
Posted via http://www.ruby-forum.com/\.

feeds.compact!

Thomas Sawyer wrote:

···

On May 18, 2010 10:11 AM, "Kevin Austin" <nitsuanivek@comcast.net> wrote:

On May 18, 10:19�am, Kevin Austin <nitsuani...@comcast.net> wrote:

�do crap

end

probably 'next' would do if your looping over files. Also there is
'retry'.

a go...

Working (kind of)

I just purged all the nil spaces for the array

     feeds.delete_if{|x| x.nil?}

So that ignores the nil spaces of the array. Now I just have to figure
out how to ignore the incorrectly formatted yamls.

Thanks again for your help.

--
Posted via http://www.ruby-forum.com/\.

If you show us the code in question we may be able to help more.

~trans

Thomas Sawyer wrote:

If you show us the code in question we may be able to help more.

~trans

Actually the rest of the problem appears to be 1.8.6 on my development
box vs. 1.8.7 on the servers.

I'll be looking for guides for the conversion process.

Thanks to everyone for your help.

···

--
Posted via http://www.ruby-forum.com/\.