question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

readonly works properly just in the first validation

See original GitHub issue

Given the next code:

from cerberus import Validator


v = Validator({
    'id': {'type': 'integer', 'readonly': True},
})


v.validate({'id': 1}) # False
v.validate({'id': 1}) # True
v.validate({'id': 1}) # True
v.validate({'id': 1}) # True

Just the first call validates correctly and return False. The subsequent calls to validate(...) return True. This just happen to me with property readonly. It looks like the validation works just on time.

Is this an intentional behavior of this library?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
funkyfuturecommented, May 2, 2017

@nicolaiarocci can you please add a bug label and assign this issue to the 1.2 milestone? i’m in fear it may be forgotten otherwise as there has been no feedback so far.

0reactions
dkellnercommented, May 7, 2017

It seems to be enough to reset is_normalized when a new document is coming in - all tests are passing now. The fact that this actually was not done before makes me wonder if there exist more bugs because of this. I will investigate further.

@funkyfuture What do you think about this?

diff --git a/cerberus/tests/test_validation.py b/cerberus/tests/test_validation.py
index ec97de5..2c31a74 100644
--- a/cerberus/tests/test_validation.py
+++ b/cerberus/tests/test_validation.py
@@ -85,6 +85,13 @@ def test_readonly_field_first_rule():
     assert 'read-only' in v.errors['a_readonly_number'][0]
 
 
+def test_repeated_readonly(validator):
+    # https://github.com/pyeve/cerberus/issues/311
+    validator.schema = {'id': {'readonly': True}}
+    assert_fail({'id': 0}, validator=validator)
+    assert_fail({'id': 0}, validator=validator)
+
+
 def test_readonly_field_with_default_value():
     schema = {
         'created': {
diff --git a/cerberus/validator.py b/cerberus/validator.py
index f28f74c..fd6b6eb 100644
--- a/cerberus/validator.py
+++ b/cerberus/validator.py
@@ -492,6 +492,8 @@ class Validator(object):
         self.document_error_tree = errors.DocumentErrorTree()
         self.schema_error_tree = errors.SchemaErrorTree()
         self.document = copy(document)
+        if not self.is_child:
+            self.is_normalized = False
 
         if schema is not None:
             self.schema = DefinitionSchema(self, schema)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation error with readonly input attribute and [Required ...
I'm just making it readonly on the Reset Password UI so user does not mistype it etc. Oh, better then, change the form...
Read more >
Validation Rule Problem - making record read only
your validation rule is preventing Record to be saved with those values, but you need to prevent it from moving away from them....
Read more >
Read only Field through validation rule - Salesforce Developers
Hi,. How can i make a field as read only using either validation rule / trigger code? If the value in the field...
Read more >
useForm - register - React Hook Form
Validation rules are all based on the HTML standard and also allow for ... dot syntax only for typescript usage consistency, so bracket...
Read more >
Forms in HTML documents - W3C
Disabled and read-only controls. Disabled controls; Read-only controls. Form submission. Form submission method; Successful controls; Processing form data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found