0

Consider this simplified scenario. Master-detail tables:

CREATE TABLE queue (
    id bigint PRIMARY KEY,
    num text NOT NULL UNIQUE
);

CREATE TABLE queue_device (
    id bigint PRIMARY KEY,
    queue_id bigint NOT NULL REFERENCES queue ON DELETE CASCADE,
    device text NOT NULL,
    UNIQUE (queue_id,device)
);

When adding devices, users obviously don't know the id, they enter num instead. So I tried this validation schema:

SCHEMA = {
    'queue': {
        'type': 'string',
        'empty': False,
        'required': True,
        'rename': 'queue_id',
        'coerce': 'queue_id'
    },
    'device': {
        'type': 'string',
        'empty': False,
        'required': True
    }
}

I wanted to rename the field and coerce it to proper value, but custom coercer does not get executed. I am sure there is a rationale for doing renaming before coercing, but I for one don't see it. This way, you effectively can't have both rename and coerce rules on same field.

OK, so I tried to set coercer on a renamed field, marking it readonly because users must not set it directly.

SCHEMA = {
    'queue': {
        'type': 'string',
        'empty': False,
        'required': True,
        'rename': 'queue_id'
    },
    'device': {
        'type': 'string',
        'empty': False,
        'required': True
    },
    'queue_id': {
        'readonly': True,
        'coerce': 'queue_id'
    }
}

I do the validation first, then normalization.

if not validator.validate(document, normalize=False):
    raise ValidationError('Document validation failed.', validator.errors)
document = validator.normalized(document)

This fails because of the readonly rule. Again, I wonder what is the rationale for checking readonly during normalization, as this is a validation, not normalization rule.

I keep hitting a wall. What is the proper way to write a validation schema in this case?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Nikša Baldun
  • 1,854
  • 4
  • 28
  • 39
  • Please add the code that implements the `queue_id` coercer. What is the concrete error you get? – funky-future Oct 31 '18 at 19:38
  • @funky-future The code in queue_id coercer is irrelevant because it does not get executed, in first case because the field was renamed, and in second case because readonly rule is checked during normalization. The error is simply "readonly". It doesn't matter to me anymore because I've given up on cerberus. It's not flexible enough for my needs. – Nikša Baldun Oct 31 '18 at 21:57
  • **See also:** https://stackoverflow.com/a/53693757/42223 – dreftymac Mar 21 '19 at 18:05
  • @NikšaBaldun It's unfortunate to hear you gave up on cerberus. Have you tried using composite validation approach? https://stackoverflow.com/a/55286635/42223 – dreftymac Mar 21 '19 at 18:10

0 Answers0