Flask restx model nested wildcard dict Flask restx model nested wildcard dict flask flask

Flask restx model nested wildcard dict


First of all, Wildcard type of field accepts the definition of the dict values, not the definition of the keys, i.e fields.Wildcard(fields.String()) validates that dict values can be only of string type (in your case you need to provide definition of distribution).

The second mistake is that you are defining distribution field as Nested object instead of using Wilcard.

The following code should work for validation purpose:

DISTRIBUTION_MODEL = NAMESPACE.model("Distribution", dict(    size=fields.Integer(),    status=fields.String(),))MEMORY_MODEL = NAMESPACE.model("Memory", dict(    totalSize=fields.Integer(description='total memory size in MB',                             example=1024),    freq=fields.Integer(description='Speed of ram in mhz', example=800),    distribution=fields.Wildcard(fields.Nested(DISTRIBUTION_MODEL))))

Unfortunately, it doesn't work for marshaling.The next code should work for marshaling, but doesn't for validation input payload:

OUTPUT_MEMORY_MODEL = NAMESPACE.model("OutputMemory", dict(    totalSize=fields.Integer(description='total memory size in MB',                             example=1024),    freq=fields.Integer(description='Speed of ram in mhz', example=800),    distribution=flask_utils.fields.Nested(        NAMESPACE.model(            "distributions", {                "*": fields.Wildcard(                    # DISTRIBUTION_MODEL is taken from previous snippet                    fields.Nested(DISTRIBUTION_MODEL)                )            }        )    )))