我具有与下一个相似的架构:
my = ngfw_validator()
class network_interface(my.Schema):
type = my.fields.String(required = True, validate = [my.validate.OneOf(['ethernet','bridge','range','bonding','gre'])])
### I want like this if condition
### but, 'type' is <fields.String(default=<marshmallow.missing>, attribute=None, validate=[<OneOf(choices=['ethernet', 'bridge', 'range', 'bonding', 'gre'], labels=[], error=u'Not a valid choice.')>], required=True, load_only=False, dump_only=False, missing=<marshmallow.missing>, allow_none=False, error_messages={u'validator_failed': u'Invalid value.', u'required': u'Missing data for required field.', u'null': u'Field may not be null.', u'invalid_utf8': u'Not a valid utf-8 string.', u'invalid': u'Not a valid string.'})>
### so,, I can't check if condition ...
if type == 'ethernet' :
mode = my.fields.String(required = True, validate = [my.validate.OneOf(['static','dhcp','pppoe'])], allow_none=True)
if mode is None :
zone = my.fields.String(required = True, validate = [my.validate.OneOf(['ANY','Trust','Untrust','DMZ','PAN'])])
....
您看到了吗?这就是我想做的。 我无论如何都喜欢。 但是,我认为必须使用if条件。
所以我尝试了..但对我没用。
1)使用@pre_load
class network_interface(my.Schema):
...
@pre_load
def pre_check (self, data):
data['type'] #print 'type' real values, ex) 'ethernet'
# but i can't validate in here
我这样打
user_data = [{
"ipv4" : "192.168.0.1/255.255.255.0",
"ipv6" : "fc00::0090:0b66:2011/64",
"type" : "Bridge",
"mode" : 'dhcp'
}]
try:
network_interface(many=True).load(user_data)
except my.ValidationError as err:
print err.valid_data
如果您可以教我该怎么做, 非常感谢。