Package: yModel
yModelSchema is aMarshmallowsubclass that saves the data and errors to private members (that you can access by using self.get_data() and self.get_errors())The point is to not need a data class since, in an API context, all we need is json in json outIt highly uses decorators to avoid (because is automatic) the need of validation and post processing the responseTree is the tree version with materialized paths and order in a object member (if needed)MongoSchema and yMongoTree are the MongoDB versions for CRUD plain schemas and tree structuresIt is part of the yRest framework (which includesySanicandyAuthby the moment and yOpenSanic in the future)ExampleclassPerson(Schema):name=fields.Str(required=True)age=fields.Int()@can_crash(Exception,ErrorSchema,404)@can_crash(ValidationError,ErrorSchema)@produces(NameOnlyOkSchema,as_="name")@consumes(NameOnlyRequestSchema)asyncdefset_name(self,request,schema):ifschema.get_data()["name"]=="Exception":raiseException("It can crash from a regular Exception")ifschema.get_data()["name"]=="MakeItCrash":raiseValidationError("Crash test")self.get_data()["name"]=schema.get_data()["name"]returnself.get_data["name"]@deprecate("Reason to deprecate it")asyncdefto_deprecate(self,request):returnTrueAs you can see, the first part of the class is a regular marshmallow class with fields declarationThe interesting part is theset_nameandto_deprecatemembers full of decoratorsset_nameconsumes aNameOnlyRequestSchema(which is another yModel use for input validation) that will be filled with the request.json (by default)With this decorator you will recieve an extra*argsitem with the model already validatedThen it produces aNameOnlyOkSchemawith the return value from the function. So in this case will be something like{"ok":true,"result":"the name of the person"}can_crash automates the error processing by returning a response like{"ok":false,"message":"the raised error"}in the example two exception has been defined:If the name passed is "Exception" will raise a regular python Exception which will be captured by the firstcan_crashreturning andErrorSchemaand a 404 status{"ok":false,"message":"It can crash from a regular Exception"}If the name passed is "MakeItCrash" it will be captured by the secondcan_crashand will return anErrorSchematoo but with the status 400{"ok":false,"message":"Crash test"}yModel defines some basic schemas for returning okOkSchemaand not okErrorSchemathat you can subclass with your own needs (or even use a schema build by you from scratch. It only needs to be a subclass of yModel)Thedeprecateddecorator will be mostly useful with the yOpenSanic to mark an API endpoint as deprecatedThis decorators will help later for introspection since the tree structures are tricky to instrospectInstallationpip install yModelTree structuresAdd apathandnamefields to build the materialized path or add a@pre_loadfunction if you need a different source for the slug (it's usingpython-slugifybut you can use any one else if you add your own@pre_loadfunction)Check out the tests for examplesMongoDBUse this class as a guide to create you own backend need (will be nice if you pull request it to this project if the backend is an open sourced one)HelpFeel free to help if you think something is weird or incomplete by submiting a pull requestI've spend an indecent amount of time dealing with tree structures to know what's essential for that matter but will be no surprise if you have a nice idea to improve this code (at the end of the day I bet you are a smart person)What is already needed[] Elasticsearch module[] Redis module[] More testing[] Continous integration[] Better help & documentationI'm not a technical person but still want to helpYou can tip the project with cryptos too:BTC: 1GtKxwZGR65ar9V8xafxhMiniZyqXej2GCETH: 0x01bd478b8C07633D2f4E58AC553f72CE4E590d56LTC: LYUzrFX6ck5uMhw5VqcD9piQHnX7oeSLdhXMR: 49stcvbfjEkWLjb6mdG21zMJ3uRrLmN3bazGQ8cHjjsVHYYyY61N6P7emCXhpsvB2Vc8Uuz2FA1Qk6hkE8e4ADmJQQ64eyTADA: DdzFFzCqrhsoUF5UjGGAYUayV5uNCJZ17PJn9V8X9MTQ26m2wDVycme42gufKufPNWMazfJLg8RKHpc1iFvn6j8BTJjaozGtLPzCDx5tNEM: NDGYO6X3NTD6CX3V7MCCYKQPBIOYGZRXEKDLCDW2