Examples

JSONClasses can quickly build a backstage in five minutes.
Code Examples
Product User
User management and session authorization are needed in most of the products.
example.py
1@authorized2@api3@pymongo4@jsonclass5class User:6 id: str = types.readonly.str.primary.mongoid.required7 email: Optional[str] = types.str.maxlength(36).trim.unique 8 .presentwithout('phone_number').writenonnull 9 .validate(validate_auth_code_for_email) 10 calling_code: Optional[str] = types.str.maxlength(5).trim 11 .presentwith('phone_number').writenonnull 12 .validate(check_user_calling_code) 13 phone_number: Optional[str] = types.str.maxlength(20).trim.unique 14 .presentwithout('email').writenonnull 15 .validate(check_user_calling_code) 16 .validate(validate_auth_code) 17 password: Optional[str] = types.str.writeonly.writenonnull 18 .minlength(8).maxlength(16) 19 .transform(salt).compare(compare_password) 20 authentication_code: Optional[str] = types.str.temp 21 old_password: Optional[str] = types.str.temp 22 name: Optional[str] = types.str.maxlength(30).trim 23 gender: Optional[Gender] = types.enum(Gender).writeonce 24 created_at: datetime = types.readonly.datetime.tscreated.required25 updated_at: datetime = types.readonly.datetime.tsupdated.required
introduction.txt
1"email": A user can have an email or a phone number, or both. 2When email is set, it can not be set back to null. 3When it's changed, an authentication code verification is triggered in case of changing it without permission.4 5"calling_code": The calling code is validated against the phone number prefix. 6If phone number is present, it must be present.7 8"phone_number": Phone number should be presented if email is not present.9A user should sign up with either email or phone number.10 11"password": The password is not output to the response. 12However, though, it can be accepted. 13It's validated and transformed on receiving. 14It's encrypted stored in the database. 15When it's changed, a password comparing is performed.16 17"authentication_code": The authentication code is used for authenticates the email updation and phone number updation. 18It will not be saved into the database. It's "temp".19 20"old_password ": The old password is used for comparing with the old encrypted value. 21Only with correct old password, password can be updated.22 23"name": A user's name is automatically trimed. 24The leading and trailing whitespaces are removed.25 26"gender": A user's gender can be null at the beginning. 27A user cannot change his/her gender after it's first time set.28
Song & Singer Relationship
This example demostrates the many to many relationship through local list keys.
example.py
1@api2@pymongo3@jsonclass4class Song:5 id: str = types.readonly.str.primary.mongoid.required6 name: str7 singers: list[Singer] = types.listof('Singer').linkto 8 created_at: datetime = types.readonly.datetime.tscreated.required9 updated_at: datetime = types.readonly.datetime.tsupdated.required101112@api13@pymongo14@jsonclass15class Singer:16 id: str = types.readonly.str.primary.mongoid.required17 name: str18 songs: list[Song] = types.listof('Song').linkedby('singers') 19 created_at: datetime = types.readonly.datetime.tscreated.required20 updated_at: datetime = types.readonly.datetime.tsupdated.required
introduction.txt
1A song has multiple singers, 2but not much, usually one or two.3 4A singer has a lot of songs.5 6Setup graph relationships are extremely simple in JSONClasses7 8JSONClasses supports local keys relationship, 9also supports join table.10 11Queries can be performed with relationships.12
Blog Comments
Comments in blog systems have a tree structure.
example.py
1@api2@pymongo3@jsonclass4class Comment:5 id: str = types.readonly.str.primary.mongoid.required6 name: str7 parent: Comment | None = types.objof('Comment').linkto 8 children: list[Comment] = types.listof('Comment').linkedby('parent') 9 created_at: datetime = types.readonly.datetime.tscreated.required10 updated_at: datetime = types.readonly.datetime.tsupdated.required
introduction.txt
1Root comments don't have a parent. 2While other comments do have a parent.3 4A comment has 0 or more child comments.5 6Setup 1 to many relationship is extremely simple in JSONClasses.
JSONClasses
Find Us
JSONClasses is under MIT License.
Made with ❤️ and passion, from Teochew.
Presented by Fillmula Inc. © 2021-2022.