jpmckinney / validictory

🎓 deprecated general purpose python data validator
Other
240 stars 57 forks source link

How to test an array with N items that validate against M schemas #12

Closed JasonSanford closed 12 years ago

JasonSanford commented 13 years ago

I'm trying to validate an array that can have any number of items in it. Each of these items can be one of 5 or 6 schemas. So, for example:

schemaPoint = {
    "type": "object",
    "properties": {
        "type": {
            "pattern": "Point"
        },
        "coordinates": {
            "type": "array"
            "items": {
                "type": "array",
                "minItems": 2,
                "maxItems": 3
            }
        }
    }
}

schemaLineString = {
    "type": "object",
    "properties": {
        "type": {
            "pattern": "LineString"
        },
        "coordinates": {
            "type": "array",
            "minItems": 2,
            "items": {
                "type": "array",
                "minItems": 2,
                "maxItems": 3
            }
        }
    }
}

There are a number of other schemas, but I'm omitting them for brevity.

So, what I need to do is create a GeometryCollection schema that has a geometries property that is an array. This array should contain items like schemaPoint, schemaLineString and quite a few others I've not shown here.

My first attempt is something like the following:

schemaGeometryCollection = {
    "type": "object",
    "properties": {
        "type": {
            "pattern": "GeometryCollection"
        },
        "geometries": {
            "type": "array",
            "items": [schemaPoint, schemaLineString]
        }
    }
}

Essentially what I want in the above is for the geometries array above to have any number of items in it, so long as each of those items matches either schemaPoint or schemaLineString.

Am I making any sense?

jamesturk commented 12 years ago

the "items" property can indeed be a list of schemas instead of a single schema, so your approach should work just fine from what i can tell, is it not working?

JasonSanford commented 12 years ago

It doesn't seem to be working. I get a Length of list [myTestObject1, myTestObject2] for field 'geometries' is not equal to length of schema list error. It's almost like it expects that the geometries array contain the exact number of objects found in the schema. I don't care how many object are in that array. They just need to be one of a few different schemas.

JasonSanford commented 12 years ago

Here's a python script with my schemas and test objects. I keep this in the root of the validictory repo and run python validate.py to test them.

jamesturk commented 12 years ago

thanks for the example script, I'll play with this and see if I can get it working

JasonSanford commented 12 years ago

Awesome, Thanks!

If it's of any interest, I'm essentially trying to create a validator for the GeoJSON Spec.

jamesturk commented 12 years ago

ah I think I have it figured out (it was a weird corner of the spec, which we do support but I hadn't done myself before)

https://gist.github.com/1689852

is an updated version of your file with a slight change to the schema and a test of a failing GeoJSON example too

Pretty neat that you got this working with GeoJSON, probably a good test case for the guys writing the schema

jamesturk commented 12 years ago

going to close this question, if you encounter a bug in the implementation please do reopen

jamesturk commented 12 years ago

actually, mind if I borrow part of your example for the documentation to show people how to do this?

JasonSanford commented 12 years ago

Awesome, Thanks a lot! Please share with others. I'm sure someone else will have the same issue as myself.

JasonSanford commented 12 years ago

I thought I'd let you know that I finally got around to writing the GeoJSON validator. See it in action or check out the blog post. Thanks again for your help.