You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For model level validators, we have to take 3 params as enforced by underneath functions
In [210]: class A(Model):
...: id=StringType()
...: names=ListType(StringType)
...: black_list=ListType(StringType)
...:
...:
...: def validate_names(self, validated, new_data):
...: print(f"New Data: {new_data}")
...: print(f"Validated: {validated}")
...: print(f"Self: {dict(self)}")
...: if any([n in validated["black_list"] for n in new_data]):
...: raise ValidationError("Blacklisted name found")
In [211]: a=A();a.id=1;a.names=["kk"];a.black_list=["jj"];a.validate()
New Data: ['kk']
Validated: {'id': '1', 'names': ['kk'], 'black_list': ['jj']}
Self: {'id': 1, 'names': ['kk'], 'black_list': ['jj']}
If those validated and new_data fields are not enforced, we could something like this
In [212]: class A(Model):
...: id=StringType()
...: names=ListType(StringType)
...: black_list=ListType(StringType)
...:
...:
...: def validate_names(self):
...: print(f"Self: {dict(self)}")
...: if any([n in self.black_list for n in self.names]):
...: raise ValidationError("Blacklisted name found")
Unfortunately, it throws up the error as data(aka validated above)/ value (aka new_data above) are enforced by schema._validator_functions
In [213]: a=A();a.id=1;a.names=["kk"];a.black_list=["jj"];a.validate()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-213-7639fd4c17da> in <module>
----> 1 a=A();a.id=1;a.names=["kk"];a.black_list=["jj"];a.validate()
~schematics/models.py in validate(self, partial, convert, app_data, **kwargs)
256 try:
257 data = self._convert(validate=True,
--> 258 partial=partial, convert=convert, app_data=app_data, **kwargs)
259 self._data.valid = data
260 except DataError as e:
~schematics/models.py in _convert(self, raw_data, context, **kwargs)
297 should_validate = getattr(context, 'validate', kwargs.get('validate', False))
298 func = validate if should_validate else convert
--> 299 return func(self._schema, self, raw_data=raw_data, oo=True, context=context, **kwargs)
300
301 def export(self, field_converter=None, role=None, app_data=None, **kwargs):
~schematics/validate.py in validate(schema, mutable, raw_data, trusted_data, partial, strict, convert, context, **kwargs)
62 data = exc.partial_data
63
---> 64 errors.update(_validate_model(schema, mutable, data, context))
65
66 if errors:
~schematics/validate.py in _validate_model(schema, mutable, data, context)
93 for field_name, field, value in atoms(schema, data, filter=has_validator):
94 try:
---> 95 schema._validator_functions[field_name](mutable, data, value, context)
96 except (FieldError, DataError) as exc:
97 serialized_field_name = field.serialized_name or field_name
~schematics/validate.py in newfunc(*args, **kwargs)
126 if not kwargs or kwargs.pop('context', 0) is 0:
127 args = args[:-1]
--> 128 return func(*args, **kwargs)
129 return newfunc
130 return func
TypeError: validate_names() takes 1 positional argument but 3 were given
Was this intentional ?
The text was updated successfully, but these errors were encountered:
For model level validators, we have to take 3 params as enforced by underneath functions
If those validated and new_data fields are not enforced, we could something like this
Unfortunately, it throws up the error as data(aka validated above)/ value (aka new_data above) are enforced by
schema._validator_functions
Was this intentional ?
The text was updated successfully, but these errors were encountered: