Validation of YAML document

1,124 views
Skip to first unread message

Michael Kowalski

unread,
Sep 12, 2013, 5:54:17 PM9/12/13
to snakeya...@googlegroups.com
Does SnakeYAML provide a methodology for performing validation of a YAML document as it's parsing?  By this I mean, for example; if I have a class with a variable called 'value' is there a preferred way to enforce that the range of values for that field has to be between 1 and 10?  Is it as simple as throwing a YAMLException in the set method for the variable? I've searched far and wide on the internet but I couldn't find any document for this, although I'm sure someone must have asked this before.

Jordan Angold

unread,
Sep 13, 2013, 2:20:59 AM9/13/13
to snakeya...@googlegroups.com
SnakeYAML will validate that the YAML document is a valid YAML document. However, there is no standard for validation of field values, except that they have the correct type (and remember that Java's generics use type erasure, so List<String> == List<Integer> == List).

You could throw from your setX() methods, but you have to make sure that SnakeYAML uses the setters. This is the default, so make sure you're not passing it BeanAccess.FIELD at any point.

/Jordan

Geoff Groos

unread,
Nov 22, 2014, 6:31:59 PM11/22/14
to snakeya...@googlegroups.com
I used to be a compiler guy, and more generally this is known as "Syntax Directed Translation (SDT)". putting validation in a setter, and specifying types (or anything else) via the bang and double-bang notation in YML are both instances of things that must be done semantically to make the document make sense.

This kind of thing isn't precisely a defined behavior of the document, but more a defined behaviour of the parser (ie, a behaviour of SnakeYAML and the other yaml parser libraries, not YAML itself).

I would love to see/learn some kind of way to do general semantic document description (sometimes called a Syntax Directed Definition (SDD)) with SnakeYML. The obvious one for me is visitors, but that's only because I worked in java with LALR parsers. Any chance something like that will happen?

For now I'll content myself with throwing setters.

che-Geoff
Reply all
Reply to author
Forward
0 new messages