def test_required_together_validator(self): resource = RequiredCarTestResource(Car()) good = validate(mock_context(), 'RequiredTogether', resource, {}) self.assertEqual(good, {}) bad = validate(mock_context(), 'RequiredTogether', resource, {'make': 'Tesla'}) self.assertEqual(bad, {'RequiredTogether': [u'Make and year are required if either is provided.']}) bad = validate(mock_context(), 'RequiredTogether', resource, {'year': 2012}) self.assertEqual(bad, {'RequiredTogether': [u'Make and year are required if either is provided.']})
def test_optional_validation(self): """ Resources and Fields should not be required to have validation """ model = User() resource = self.OptionalResource(model) errors = validate(mock_context(), 'user', resource, {}) self.assertEqual({}, errors)
def test_required_together_validator(self): resource = RequiredCarTestResource(Car()) good = validate(mock_context(), 'RequiredTogether', resource, {}) self.assertEqual(good, {}) bad = validate(mock_context(), 'RequiredTogether', resource, {'make': 'Tesla'}) self.assertEqual( bad, { 'RequiredTogether': [u'Make and year are required if either is provided.'] }) bad = validate(mock_context(), 'RequiredTogether', resource, {'year': 2012}) self.assertEqual( bad, { 'RequiredTogether': [u'Make and year are required if either is provided.'] })
def validate_user_resource(name, age, start, end, systolic, car=None, stolen_car=None): source_dict = dict(name=name, age=str(age), before=start.isoformat(), after=end.isoformat(), systolicBp=int(systolic), vehicle=(car and car.to_json()), stolenVehicle=(stolen_car and stolen_car.to_json())) return validate(mock_context(), 'user', UserTestResource(User()), source_dict)
def put(self, ctx, source_dict, save=True, skip_validation=False): ''' This is where we respect the 'pre_save' flag on each field. If pre_save is true, then we set the field value, before calling save. If not, call save first, before setting the field value, this is for the many-to-many relationship. ''' if not source_dict: return if not skip_validation: errors = validate(ctx, self.__class__.__name__, self, source_dict) if errors: logger.debug(errors) raise ValidationError(self, errors) try: self._set_pre_save_fields(ctx, source_dict) except TypeError, e: import traceback for L in traceback.format_exc().splitlines(): logger.debug(L) raise ValidationError(self, {'invalidFieldData': e.message})
def validate_resource(self, ctx, key, resource, source_dict): return validate(ctx, key + '.' + self.name, self._resource_class, source_dict)