def validate(self, data): if data['normal_pregnancy'] is False: self.run_validators_on_field(data, 'abnormal_pregnancy_text', [not_empty()]) if data['neurological_problems']: self.run_validators_on_field(data, 'seizures', [required()]) self.run_validators_on_field(data, 'abnormal_gait', [required()]) self.run_validators_on_field(data, 'deafness', [required()]) self.run_validators_on_field(data, 'other_neurological_problem', [required()]) if data['other_neurological_problem']: self.run_validators_on_field(data, 'other_neurological_problem_text', [not_empty()]) if data['joint_problems']: self.run_validators_on_field(data, 'joint_problems_age', [required()]) self.run_validators_on_field(data, 'x_ray_abnormalities', [required()]) if data['x_ray_abnormalities']: self.run_validators_on_field(data, 'chondrocalcinosis', [required()]) self.run_validators_on_field(data, 'other_x_ray_abnormality', [required()]) if data['other_x_ray_abnormality']: self.run_validators_on_field(data, 'other_x_ray_abnormality_text', [not_empty()]) return data
def validate(self, data): data = super(UserSerializer, self).validate(data) current_user = self.context['user'] instance = self.instance if not data['is_bot']: # Humans need a name and email self.run_validators_on_field(data, 'first_name', [not_empty()]) self.run_validators_on_field(data, 'last_name', [not_empty()]) self.run_validators_on_field(data, 'email', [required()]) # New user if instance is None: # Password is required when creating a new user if not data['is_bot']: self.run_validators_on_field(data, 'password', [required()]) else: # Editing yourself if current_user == instance: password = data['password'] current_password = data['current_password'] # Current password is required to change email or password if ( ( instance.email != data['email'] or (password is not None and not instance.check_password(password)) ) and ( current_password is None or not instance.check_password(current_password) ) ): # Incorrect password raise ValidationError({'current_password': '******'}) # Trying to disable force password change flag if instance.force_password_change and not data['force_password_change']: if password is None: raise ValidationError({'password': '******'}) elif instance.check_password(password): # New password must be different to old password if force password change was set raise ValidationError({'password': '******'}) return data
def test_str(): value = not_empty()('hello') assert value == 'hello'
def test_none(): with pytest.raises(ValidationError): not_empty()(None)
def test_empty_list(): with pytest.raises(ValidationError): not_empty()([])
def test_empty_str(): with pytest.raises(ValidationError): not_empty()('')
def test_list(): value = not_empty()(['hello', 'world']) assert value == ['hello', 'world']