New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
readonly
conflicts with default
#268
Comments
well, yes. they are conflicting. by definition. it's okay. nothing hinders you to run various validations and normalizations in any arbritrary order. i would even suggest that this is a good practice when schemas are getting complex. |
I've encountered this issue because I'm integrating Cerberus 1.0 in Eve: https://github.com/nicolaiarocci/eve/issues/776 Eve currently has its own I have some working code allowing
If we want to have fields with both
If you are interested, you can see the code here: https://github.com/dkellner/cerberus/tree/default-readonly. There is still one failing test, because the first validator (processing "only" So the important question is: Do we want to allow One further question to your "good practice"-suggestion: Do you mean running a validator with just read-only checks and |
My answer would be Yes. |
"Work in progress" because we still have to wait for the Cerberus issue "`readonly` conflicts with `default`" to be resolved. (see pyeve/cerberus#268) This is a rather big change. I still decided to do a single commit, as intermediate commits would be in a non-working state anyway. Breaking changes: - `keyschema` was renamed to `valueschema` and `propertyschema` to `keyschema` (following changes in Cerberus). - A PATCH on a document which misses a field having a default value will now result in setting this value, even if the field was not provided in the PATCH's payload. - Error messages for `keyschema` are now returned as dictionary. Before: {'propertyschema_dict': 'propertyschema_dict'} Now: {'keyschema_dict': {'AAA': "value does not match regex '[a-z]+'"}} - Error messages for `type` validations are different now (following changes in Cerberus). - It is no longer valid to have a field with `default` = None and `nullable` = False. (see patch.py:test_patch_nested_document_nullable_missing) In a nutshell, changes to the codebase are as follows: - Add data layer independent subclass of `cerberus.Validator` * Support new signature of `__init__` and `validate` * Use `_config`-aware properties instead of bare member attributes to pass the `resource`, `document_id` and `persisted_document` to make them available to child validators * Add schema-docstrings to all `_validate_*` methods - Adjust Mongo-specific `Validator` subclass * Adjust `_validate_type_*` methods (following changes in Cerberus) * Add schema-docstrings to all `_validate_*` methods - Add custom ErrorHandler to support `VALIDATION_ERROR_AS_LIST` - A few renames: * `ValidationError` -> `DocumentError` * `propertyschema` -> `keyschema` and `keyschema` -> `valueschema` - Adjust tests due to different validation error messages (mostly for `type`) - Remove `transparent_schema_rules` without replacement - Remove `default`-handling, as Cerberus takes care of this now
i'm not sure if I understand the use-case from the tests above. but wouldn't it suffice to update the document with the default values in the client code before it is processed by cerberus? |
"Work in progress" because we still have to wait for the Cerberus issue "`readonly` conflicts with `default`" to be resolved. (see pyeve/cerberus#268) This is a rather big change. I still decided to do a single commit, as intermediate commits would be in a non-working state anyway. Breaking changes: - `keyschema` was renamed to `valueschema` and `propertyschema` to `keyschema` (following changes in Cerberus). - A PATCH on a document which misses a field having a default value will now result in setting this value, even if the field was not provided in the PATCH's payload. - Error messages for `keyschema` are now returned as dictionary. Before: {'propertyschema_dict': 'propertyschema_dict'} Now: {'keyschema_dict': {'AAA': "value does not match regex '[a-z]+'"}} - Error messages for `type` validations are different now (following changes in Cerberus). - It is no longer valid to have a field with `default` = None and `nullable` = False. (see patch.py:test_patch_nested_document_nullable_missing) In a nutshell, changes to the codebase are as follows: - Add data layer independent subclass of `cerberus.Validator` * Support new signature of `__init__` and `validate` * Use `_config`-aware properties instead of bare member attributes to pass the `resource`, `document_id` and `persisted_document` to make them available to child validators * Add schema-docstrings to all `_validate_*` methods - Adjust Mongo-specific `Validator` subclass * Adjust `_validate_type_*` methods (following changes in Cerberus) * Add schema-docstrings to all `_validate_*` methods - Add custom ErrorHandler to support `VALIDATION_ERROR_AS_LIST` - A few renames: * `ValidationError` -> `DocumentError` * `propertyschema` -> `keyschema` and `keyschema` -> `valueschema` - Adjust tests due to different validation error messages (mostly for `type`) - Remove `transparent_schema_rules` without replacement - Remove `default`-handling, as Cerberus takes care of this now
Used Cerberus version / latest commit: 1.0.1 / ebd281b
Bug report
Documents always fail validation when
default
andreadonly
are used on the same field.This is because
default
value handling / normalization will happen before thereadonly
validator is called and the latter will therefore always fail, because the document will always contain the field. I think we should allow validators to be processed before normalization and maybe another list likepriority_validations
for those rules would be useful.I've started working on this already, but of course any comments are appreciated :).
Failing test
The text was updated successfully, but these errors were encountered: