jazzband / jazzband/jsonmodels

Defer validation when setting a value to an EmbeddedField

Open
#98 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
343
Forks
50
PR merge metrics
No merged PRs in 30d

Description

Consider the following model:
```python
class DatabaseConfig(Base):
base_url = fields.StringField(required=True)
db_name = fields.StringField(required=True)

class Config(Base):
db = EmbeddedField(DatabaseConfig, required=True)
```

I can create an empty instance of `Config`, that I can populate with values until validation is required:
```
conf = Config()
db = DatabaseConfig()
```

At this point I **can** assign values to all the fields of `db`, But I **cannot** assign `db` itself to the corresponding field in `config`:
```
In [12]: conf.db = db
ValidationError: ("Error for field 'db'.", ValidationError("Error for field 'base_url'.", ValidationError('Field is required!',)))
```

While I understand **why** this is happening (fields validates values on assignment in ``__set__``, and in the case of `EmbeddedField` this means calling `validate` on the embedded field), this is very annoying since I have to pre-initialize all the embedded fields beforehand.

I think it would be better to defer the validation of `EmbeddedField` until `validate` is called on the containing model.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.