How do I validate a `schematics.Model` against a known value?

144 Views Asked by At

Say I have the following model:

import schematics

class Environment(schematics.Model):
    name = schematics.types.StringType(required=True)
    description = schematics.types.StringType()

Then I create an instance:

raw_json = some_api_call(...)
# Say raw_json is {"name": "stagging", "description": "A stagging area"}
env = Environment(raw_json)
expected = {"name": "test", "description": "Tests area"}

Now, I want to validate env against expected? I know that calling env.validate() will validate the fields, but how about validating against expected values? I have tried the shooting in the dark with:

env.validate(trusted_data=expected)

But that is not working. The documentation for this project is not that comprehensive and looking into the source is too much for me to figure out. Any help are highly appreciated.

1

There are 1 best solutions below

0
Hunor Portik On

Maybe I didn't get it fully your question, tho schematics at the end of the day is a simple dict, that holds data.

In this case I wouldn't really go further than assert dict1 == dict2. If you need more granular comparison then you can go into details by comparing the key-values 1-by-1.