Comments (5)
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.
I have the same question.
from schematics.
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.
Unsurprisingly my hack broke somewhere during the upgrade to schematics 2.x (AttributeError: property 'model_class' of 'ModelType' object has no setter).
from schematics.
Actually schematics 2.0 supports self-recursive models (#335), so no workaround should be necessary.
from schematics.
Related Issues (20)
- When using multiple inheritance, rogue field errors for any inherited models
- ListType serialize_when_none=False doesn't seem to work HOT 1
- Serializable property is not filled when import data
- ETA for a new release HOT 7
- HashType requires LENGTH HOT 1
- SyntaxWarning with a literal
- Fix simple typo: overriden -> overridden HOT 2
- BaseType's has no attribute 'model_class' HOT 2
- RecursionError when using @serializable HOT 2
- Have you considered the possibility to replace blacklist/whitelist terminology by using better terminology?
- Project is still being maintened HOT 9
- Request for joining the organisation
- I want To Join this organization.
- Question: PolyModelType that selects Model based on field value of parent HOT 1
- Question: Parsing data between JSON schemas HOT 1
- schematics.exceptions.ConversionError: [u'No default or explicit locales were given.'] HOT 1
- Python 3.10 - Schematics finally broke HOT 4
- cannot collect all model errors HOT 1
- the traceback including the line number where the validation error occurred
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from schematics.