0

I have a data structure that looks like this with name being an arbitrary string that cannot be certain values (src)

 {
  'name' : 'stringvalue',
  'src'  : 'who cares this is wrong'
 }

I'd like cerberus to check that the keys are anything but src or alsoBad, but all the schema examples I see seem to require that I specify the format for a given name.

I've tried this:

 def check_data(type_data):
    val = cerberus.Validator()
    val.allow_unknown={'forbidden' : ['src','alsoBad']}

    val.validate(type_data,{})

With the thought that the empty schema would cause the allow_unknown to go to work. But this does not find the problem.

Then I tried:

 def check_data(type_data):
    val = cerberus.Validator()
    val.allow_unknown=

    val.validate(type_data,{'any_field':{'forbidden' : ['src','alsoBad']}})

But this also failed to catch the problem.

How do I check the first level keys in a dict when they could be anything?

Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
  • Clearly the second one is checking that the data in the unknown fields are not `src` or `alsoBad`, but still don't see how to check the field names themselves. – Ray Salemi Apr 04 '18 at 18:57
  • You should give at least one valid and one invalid example of a document as well as the schema you got so far. i have the impression that you're overcomplicating your task. – funky-future Apr 09 '18 at 13:45

1 Answers1

0

I have punted and just done it with code:

def check_data(type_data):
     bad_field=set(['src','alsoBad']) & set(type_data.keys())
     if bad_field:
          raise ValueError(f'Cannot have field {bad_field}'
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63