Martin,

As I mentioned, there are about 20 data validation modules on CPAN, of which a 
few are generic enough to be comparable to yours (others are specialized on 
specific data types). Probably the most similar ones are Config::Validate, 
Data::Validate::Struct, 
which can validate data structure, not just config files, but naturally 
don't use the concepts and terminology of XSD as your module does.

I looked at the XSD spec, and it's one of those OMG WTF! things. In any case, 
before you start implementing the parsing and validation of XSD files, take a 
look at this: while digging around w3.org, I also saw that there is a Perl 
module that parses the XML schema document format 
(http://search.cpan.org/~samtregar/XML-Validator-Schema-1.10/). That should 
fulfill Barbara's wish.

Furthermore, I don't think you need to do anything whatsoever for YAML. You 
already have all the functionality required. Both the data to validate and 
the validation schema are data structures, right? YAML represents data 
structures. The user can load YAML into a Perl data structure simply by 
calling YAML's Load() or LoadFile(), and then can use your methods, I'd 
think. OK, so instead of speculating, I'm putting some example code below.

I wanted a list-variety simple type, and specified it as "variety: list, 
itemType: whatever" (I made it up following XSD loosely). It would be 
interesting to have that implemeted in Data::Validate::XSD.

Another interesting feature of XSD is the provision for default values, which 
other validation modules, like Config::Validate or Params::Validate do.


Bernardo

=====================================
#!/usr/bin/perl 

use warnings;
use Data::Validate::XSD;
use YAML qw(Load Dump);

# The two YAML strings below could equally well be in a file, in which case 
we'd
# use LoadFile(<filename>) instead of Load(<string>);

# Validation schema. It expresses the following specification: our data should 
contain:
#   - an optional 'a' element, with an integer value;
#   - a 'b' element that contains one or more 2- or 3-letter words;
#   - two 'circuit' elements of type 'polygon', which is a list of 3 or more 
2D points.
#
$schema_yaml = <<'SCHEMA';
---
root:
  - { name: a,       type: integer, minOccurs: 0, maxOccurs: 1 }
  - { name: b,       type: listOfShortStrings }
  - { name: circuit, type: polygon, minOccurs: 2, maxOccurs: 2 }
simpleTypes:
  shortString: { base: string, minLength: 2, maxLength: 3 }
  listOfShortStrings:
    variety: list
    itemType: shortString
    minLength: 1
    maxLength: 3
  xcoord: { base: float }
  ycoord: { base: float }
complexTypes:
  point2D:
    - { name: x, type: xcoord }
    - { name: y, type: ycoord }
  polygon:
    - { type: point2D, minOccurs: 3, maxOccurs: unbounded }
SCHEMA

%schema = %{ Load($schema_yaml) };
# Same as:
#
#%schema = (
#    root => [
#        { name => 'a',       type => 'integer', minOccurs => 0, maxOccurs => 
1, },
#        { name => 'b',       type => 'listOfShortStrings', },
#        { name => 'circuit', type => 'polygon', minOccurs => 2, maxOccurs => 
2, },
#    ],
#    simpleTypes => {
#        shortString => { base => 'string', minLength => 2, maxLength => 3, },
#        listOfShortStrings => {
#            variety   => 'list',
#            itemType  => 'shortString',
#            minLength => 1,
#            maxLength => 3,
#        },
#        xcoord => { base => 'float', },
#        ycoord => { base => 'float', },
#    },
#    complexTypes => {
#        point2D => [
#            { name => 'x', type => 'xcoord', },
#            { name => 'y', type => 'ycoord', }
#        ],
#        polygon => [
#            { type => 'point2D', minOccurs => 3, maxOccurs => 'unbounded', }
#        ],
#    },
#);

# Example of data to validate against schema
$data_yaml = <<'DATA';
---
a: 2
b:
  - foo
  - bar
  - baz
circuit:
  -
    - 2Dpoint:
        x: 1
        y: 3.14
    - 2Dpoint:
        x: 5.67
        y: -84.7
    - 2Dpoint:
        x: 7
        y: 6.8
  - 
    - 2Dpoint:
        x: 31
        y: 4
    - 2Dpoint:
        x: -67
        y: 4.7
    - 2Dpoint:
        x: 17
        y: 8.6
DATA

%data = %{ Load($data_yaml) };
# Same as:
#%data = {
#    a => 2,
#    b => ['foo', 'bar', 'baz'],
#    circuit => [
#        [   { 2Dpoint => { x => 1,    y => 3.14 } },
#            { 2Dpoint => { x => 5.67, y => -84.7 } },
#            { 2Dpoint => { x => 7,    y => 6.8 } },
#        ],
#        [   { 2Dpoint => { x => 31,   y => 4 } },
#            { 2Dpoint => { x => -67,  y => 4.7 } },
#            { 2Dpoint => { x => 17,   y => 8.6 } },
#        ]
#    ]
#};


$validator = Data::Validate::XSD->new( \%schema );
$errors = $validator->validate( \%data );
warn Dump($errors) if $errors;
# Output (what do the numbers mean?!)
#---
#b: 15
#circuit:
#  - 12
#  - 12
=======================================================
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to