question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple anonymous schema validation

See original GitHub issue

Hi all,

I am looking for a schema that can validates multiples schema ;

document1 = {
   "this_field" : {"bar_field" : "value"},
   "common_field1" : 42,
   "common_field2" : "hello",
}
document2 = {
   "that_field" : "blop",
   "common_field1" : 42,
   "common_field2" : "hello",
}

(note the difference between this_field and that_field)

From what I understand from the documentation, it could be written using *-of :

schema = {
    "oneof_schema":[
        {"this_field" : {"type": "dict"}},
        {"that_field": {"type" : "string"}}
    ],
   "common_field1" : {"type" : "integer"},
   "common_field2" : {"type" : "string"}
}

When I try to validate, I get the following error

>>> Validator().validate(document1, schema) 
 File "/home/g/.virtualenvs/p/lib/python2.7/site-packages/cerberus/cerberus.py", line 231, in validate
    return self._validate(document, schema, update=update, context=context)
  File "/home/g/.virtualenvs/p/lib/python2.7/site-packages/cerberus/cerberus.py", line 249, in _validate
    self.validate_schema(schema)
  File "/home/g/.virtualenvs/p/lib/python2.7/site-packages/cerberus/cerberus.py", line 397, in validate_schema
    raise SchemaError(errors.ERROR_DEFINITION_FORMAT.format(field))

cerberus.cerberus.SchemaError: unknown rule 'oneof_schema' for field 'oneof_schema'

I need to “name” the field used by oneof to pass this error

schema = {
    "named" : {
        "type" : "dict",
        "oneof": [
            {"schema": {"this_field" : {"type": "dict"}}},
            {"schema": {"that_field": {"type" : "string"}}}
        ]
    },
   "common_field1" : {"type" : "integer"},
   "common_field2" : {"type" : "string"}
}

but this won’t validate my original document1 and document2, which now needs to contains a named key

document3 = {
   "named" : {"this_field" : {"foo":42}},
   "common_field1" : 34
}

document4 = {
   "named" : {"that_field" : "test"},
   "common_field1" : 34
}

Correctly outputs

>>> Validator().validate(document3, schema)
True
>>> Validator().validate(document4, schema)
True

But this is not as good as what I need since it changes the structures of my documents (document1 != document3 and document2 != document4)

How can I write a schema that validates both document1 and document2, without using a specific named key ?

Please, let me know if I did not made myself clear, or if you need any extra more information.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
funkyfuturecommented, Jul 21, 2015

unfortunately such convenience isn’t possible.

apparently your first schema will not work because constraints are always bound to a field, so ‘oneof_schema’ will be interpreted like this.

the second schema is a good approach to deal with it, but the ‘common’ fields are kept one level too high.

i would recommend you to write write (parts of) the schema without shortcuts and then figure out how it can be condensed.

i hope that helps you. i’m not testing your bits, so i may oversee something.

0reactions
funkyfuturecommented, Jul 29, 2015

well, in a rudimentary way you did it. feel free to open a pull request with a proper implementation that includes:

  • validation of the rule
  • rename it to the less ambigious excludes as proposed
  • tests
  • feature documentation
  • handling of multiple excluded fields in a list
  • dismissing of required-rules for fields that have been effectively excluded
Read more comments on GitHub >

github_iconTop Results From Across the Web

Json request validation using multiple schemas | Layer7 API ...
Hi,. Validate JSON request using Swagger document using multiple schema resources. Using assertion- "Validate Against Swagger Document", ...
Read more >
Structuring a complex schema — Understanding JSON ...
In this documentation, we will refer to schemas with no identifier as “anonymous schemas”.
Read more >
Is there a way to validate that one of two properties (both ...
I only want one "foo" and one "bar" property in the resource. Note that your first example will NOT validate according to this,...
Read more >
GraphQL query best practices
The first query is named GetBooks . The second query is anonymous. You should define a name for every GraphQL operation in your...
Read more >
xsd - XML Schema: converting simpletype to anonymous type
It is not possible in XSD to use purely anonymous, local definitions when declaring an element to have both restricted content and an ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found