0

I defined a class

class Person(colander.MappingSchema):
    name = colander.SchemaNode(colander.String())
    age = colander.SchemaNode(colander.Int()

and I have a coming json string that may be like:

{
     'name': 'keith',
     'age': '20',
     'friends': [('1', 'jim'), ('2', 'bob'), ('3', 'joe'), ('4', 'fred')],
     'phones': [{'location': 'home', 'number': '555-1212'},
            {'location': 'work', 'number': '555-8989'}],
     ...
}

I only want to validate name and age, and I do not care about other fields (and I don't know all the other possible fields). Is there any way to only validate the two fields while ignoring others?

Justin Li
  • 1,035
  • 2
  • 12
  • 19

1 Answers1

0

When you say ignore the other fields, do you mean just drop them from the resulting dictionary after deserialisation?

If so then yes that is the default behaviour. Deserialisation will automatically drop any keys which don't map to the defined schema. There if you do

result = Person().deserialise(<your sample dictionary>)

The result vairable will just be a dictionary containing the keys name and age.

Mr-F
  • 871
  • 7
  • 12