Giter Club home page Giter Club logo

Comments (5)

bjarneschroeder avatar bjarneschroeder commented on June 12, 2024 1

@mgedmin

Hey man, I resolved it the following way:
https://schematics.readthedocs.io/en/latest/usage/validation.html#model-level-validation

You can write a method and create an instance of the model there and call validate().
For the example above I did it like this:

class Product(Model):
    vendors = ListType(BaseType)

    def validate_vendors(self, data: dict, value: list[VendorItem]):
        if value:
            for vendor_item in value:
                Vendor(vendor_item).validate()

class Vendor(Model):
    products = ListType(BaseType)

    def validate_products(self, data: dict, value: list[ProductItem]):
        if value:
            for product_item in value:
                Product(product_item).validate()

Keep an eye on the naming convention and the attributes. Otherwise the method won't be called correctly.
It is definitely not an ideal solution but it works for my purposes and maybe for yours to.
I use schematics for Item-Validation in scrapy and errors are displayed correctly in the statistics etc.

Hope this helps! 👍

from schematics.

mgedmin avatar mgedmin commented on June 12, 2024

I have the same question.

from schematics.

mgedmin avatar mgedmin commented on June 12, 2024

This seems less fragile than my hack, which was

class TreeNode(Model):
    children = ListType(ModelType(Model))  # should be ListType(ModelType(TreeNode))

# schematics doesn't support recursive models, so we monkey-patch that in 
TreeNode.children.field.model_class = TreeNode
TreeNode.children.field.fields = TreeNode.fields

from schematics.

mgedmin avatar mgedmin commented on June 12, 2024

Unsurprisingly my hack broke somewhere during the upgrade to schematics 2.x (AttributeError: property 'model_class' of 'ModelType' object has no setter).

from schematics.

mgedmin avatar mgedmin commented on June 12, 2024

Actually schematics 2.0 supports self-recursive models (#335), so no workaround should be necessary.

from schematics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.