Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between data and validated data?

What transformations does Django Rest Framework make to data after validation? Does it only parse strings to int when possible?

like image 713
user2080105 Avatar asked Feb 02 '17 11:02

user2080105


People also ask

What is the main difference between data verification and data validation?

Data verification: to make sure that the data is accurate. Data validation: to make sure that the data is correct.

What are the difference between data validation and data verification explain with suitable example?

Verification may also happen at any time. In other words, verification may take place as part of a recurring data quality process, whereas validation typically occurs when a record is initially created or updated. Verification plays an especially critical role when data is migrated or merged from outside data sources.

What do you mean by validating data?

Data validation means checking the accuracy and quality of source data before using, importing or otherwise processing data. Different types of validation can be performed depending on destination constraints or objectives. Data validation is a form of data cleansing.

What is the difference between data validation and data integrity?

Description Data Integrity is the assurance that information is unchanged from its source, and has not been accidentally or maliciously modified, altered or destroyed. Validation is the tests and evaluations used to determine compliance with security specifications and requirements.


2 Answers

You can customize data in your CommentSerializer.validate_content method:

This is your serializer:

class CommentSerializer(serializers.Serializer):
    email=serializers.EmailField()
    content=serializers.CharField()
    def validate_content(self,value):
        #If content is 'baz' returns 'foo'
        if value and value == "baz":
            return "foo"
        return value

Let's try with wrong values (for email):

>>> serializer = CommentSerializer(data={'email': 'foobar', 'content': 'baz'})

>>> serializer.data
Traceback (most recent call last):
    raise AssertionError(msg)
AssertionError: When a serializer is passed a `data` keyword  argument 
you must call `.is_valid()` before attempting to access the 
serialized `.data` representation.

You should either call .is_valid() first, or access .initial_data instead.

>>> serializer.initial_data
{'content': 'baz', 'email': 'foobar'}

>>> serializer.validated_data
Traceback (most recent call last):
   raise AssertionError(msg) 
AssertionError: You must call `.is_valid()` before accessing `.validated_data`.

>>> serializer.is_valid()
False

>>> serializer.data
{'content': 'baz', 'email': 'foobar'}

>>> serializer.validated_data
{}

>>> serializer.errors
{'email': [u'Enter a valid email address.']}

Now Let's try with correct values

>>> serializer2 = CommentSerializer(data={'email': '[email protected]', 
'content': 'baz'})  
>>> serializer2.is_valid()
True

>>> serializer2.initial_data
{'content': 'baz', 'email': '[email protected]'}
>>> serializer2.errors
{}

>>> serializer2.data
{'content': u'foo', 'email': u'[email protected]'}

>>> serializer2.validated_data
OrderedDict([(u'email', u'[email protected]'), (u'content', u'foo')])

So:

  • data : is a dict and you can see it only after is_valid() (you can see only not validated values)
  • validated_data is an OrderedDict and you can see it only after is_valid() and is_valid() == True
like image 178
madeng84 Avatar answered Oct 21 '22 23:10

madeng84


DESERIALIZING

Data is the input to a Serializer eg

serializer = CommentSerializer(data={'email': 'foobar', 'content': 'baz'})

As you can see email is invalid, so the following rule apply(from official docmentation)

When deserializing data, you always need to call is_valid() before attempting to access the validated data, or save an object instance

serializer.is_valid()

If the data your trying to deserialize is valid #True then you can access the validated data as

serializer.validated_data

ADDITION

There is no data transformation after validation, it only check if your data is valid, if any validation errors occur, the .errors property will contain a dictionary representing the resulting error messages.

A good example is when your doing field validation

class CommentSerializer(serializers.Serializer):
    title = serializers.CharField(max_length=100)

    def validate_title(self, value):
        if 'django' not in value.lower():
           raise serializers.ValidationError("Title is not about Django")
        return value

validate_title will called each time you call .is_valid(), as you can see it only check if the title is django related, if True the value which in this case title is returned else ValidationError is raised. NO CHANGE OF DATA HERE

like image 44
Emmanuel Mtali Avatar answered Oct 21 '22 23:10

Emmanuel Mtali