Types Modifiers
The types modifiers in JSONClasses define field types, reading/writing rules, validation rules and value transformers.
#
Data Types#
Primitive Data Types#
Usagetypes.str
, types.int
, types.float
, types.bool
, types.date
, types.datetime
.
Define and mark the field's type. This is normally put at the beginning of the types modifiers. If there is a transformer in the chain which transforms other data types into the desired data type, the data type modifier should be preceded by the transformer. A transformer in the chain triggers eager validation.
#
Examples@jsonclassclass MyClass: str_value: str = types.str.required optional_int_value: Optional[int] = types.int
#
Enum Data Types#
enumenum
marks the field as enum data type.
#
Usageenum(type[Enum] | str)
#
Parameterstype[Enum]
The enum class marked with @jsonenum
.
str
The string represents the enum class marked with @jsonenum
.
#
Examples@jsonclassclass Dog: sex: Sex = types.enum(Sex).required breed: Breed = types.enum('Breed').required
#
inputvalueThis enum field accepts enum value.
#
Examples@jsonclassclass Dog: sex: Sex = types.enum(Sex).inputvalue.required
#
inputnameThis enum field accepts enum name. This is the default behavior.
#
Examples@jsonclassclass Dog: sex: Sex = types.enum(Sex).inputname.required
#
inputlnameThis enum field accepts lowercased enum name.
#
Examples@jsonclassclass Dog: sex: Sex = types.enum(Sex).inputlname.required
#
inputallThis enum field accepts any value which is acceptable.
#
Examples@jsonclassclass Dog: sex: Sex = types.enum(Sex).inputall.required
#
outputvalueThis enum field outputs enum value.
#
Examples@jsonclassclass Dog: sex: Sex = types.enum(Sex).outputvalue.required
#
outputnameThis enum field outputs enum name.
#
Examples@jsonclassclass Dog: sex: Sex = types.enum(Sex).outputname.required
#
outputlnameThis enum field outputs lowercased enum name.
#
Examples@jsonclassclass Dog: sex: Sex = types.enum(Sex).outputlname.required
#
Collection Data Types#
listoflistof
marks the field as list
data type.
#
Usagelistof(Types | type[JObject] | type[Enum] | str)
#
ParametersTypes
: The item types definition.
type[JObject]
: The JSONClasses object class.
type[Enum]
: The enum class marked with @jsonenum.
str
: The string representation of object class, enum class or typed dict class.
#
Examples@jsonclassclass MyClass: field_one: list[str] = types.listof(str) field_two: list[int] = types.listof(types.int).required field_three: list[MyEnum] = types.listof(MyEnum).required field_four: list[MyObject] = types.listof('MyObject')
#
dictofdictof
marks the field as dict data type. Only str
key is supported.
#
Usagedictof(Types | type[JObject] | type[Enum] | str)
#
ParametersTypes
: The item types definition.
type[JObject]
: The JSONClasses object class.
type[Enum]
: The enum class marked with @jsonenum.
str
: The string representation of object class, enum class or typed dict class.
#
Examples@jsonclassclass MyClass: field_one: dict[str, str] = types.dictof(str) field_two: dict[str, int] = types.dictof(types.int).required field_three: dict[str, MyEnum] = types.dictof(MyEnum).required field_four: dict[str, MyObject] = types.dictof('MyObject')
#
Object Data Types#
objofobjof
is a type represents a JSONClasses object of specific class.
#
Usageobjof(type[JObject], str)
#
Parameterstype[JObject]
: The JSONClasses class.
str
: A string which represents a JSONClasses class.
#
Examples@jsonclassclass User: username: str = types.str.unique.required articles: list[Article] = types.listof('Article').linkedby('author') profile: Profile = types.objof('Profile').linkto.required
#
Union Data Types#
unionThe value of a union field can be one of provided types.
#
Usageunion(list[Union[Types, type[JObject], type[Enum], type[TypedDict], str]])
#
Examples@jsonclassclass MyClass: field_one: int | str = types.union([str, int]) field_two: Union[int, str] = types.union([types.str, types.int])
#
Any Data Types#
anyThe value of this field can be anything.
#
Examples@jsonclassclass MyClass: field_one: Any = types.any.required field_two: Optional[Any] = types.any
#
Field Definitions#
primaryMarks a str
or int
field as primary field.
#
Examples@pymongo@jsonclassclass Article: id: str = types.readonly.str.primary.mongoid.required title: str content: str
#
tscreatedMarks a datetime field's usage to timestamp for recording creation date. This automatically gains a default value feature.
#
Examples@pymongo@jsonclassclass Article: id: str = types.readonly.str.primary.mongoid.required title: str content: str created_at: datetime = types.readonly.datetime.tscreated.required
#
tsupdatedMarks a datetime field's usage to timestamp for recording modification date. This automatically gains a default value and a setonsave
feature.
#
Examples@pymongo@jsonclassclass Article: id: str = types.readonly.str.primary.mongoid.required title: str content: str created_at: datetime = types.readonly.datetime.tscreated.required updated_at: datetime = types.readonly.datetime.tsupdated.required
#
Field Nullability#
requiredThe value of this field should not be None.
#
Examples@pymongo@jsonclassclass Song: name: str = types.str.required
#
nullableMarks a collection item field to be nullable.
#
Examples@pymongo@jsonclassclass SchoolClass: student_names: list[str] = types.listof(str).required student_height: list[str] = types.listof(types.str.nullable).required
#
presentMarks a field as present. When validating, if no value is present in this field, validation will fail. This is useful for foreign key fields to do required validation.
#
Example@jsonclassclass User: profile: Profile = types.objof('Profile').linkedby('user').present
#
presentwithFields marks with presentwith modifier are forced presented if referring field is present. If referring field has None value, this field's value is optional. If referring field has non None value, value of this field is required.
#
Usagepresentwith(str)
#
Example@jsonclassclass UserContact: calling_code: Optional[str] = types.str.presentwith('phone_number') phone_number: Optional[str] = types.str
#
presentwithoutFields marked with presentwithout modifier are forced presented if referring field is not present. If referring field has None value, this field's value should be present. If referring field has non None value, value of this field is not forced to be present.
#
Usagepresentwithout(str | list[str])
#
Example@jsonclassclass UserAccount: email: Optional[str] = types.str.presentwithout('phone_number') phone_number: Optional[str] = types.str.presentwithout('email')
#
nonnullTransforms None into empty library.
#
Example@jsonclassclass User: motto: str = types.str.required
#
Accessibility#
writeonlyField marked with writeonly
can only be written to. Values of these fields are hidden from the JSON output.
#
Examples@pymongo@jsonclassclass User: id: str = types.readonly.str.primary.mongoid.required username: str = types.str.unique.required password: str = types.writeonly.str.required
#
writeonceFor optional fields, the value is optional. And on creating, it can be null. After assigning a nonnull value, this field doesn't accept new value anymore. For required fields, the value cannot be changed after assigning on creation.
#
Examples@jsonclassclass User: # a sex value shouldn't be changed after it's set sex: Optional[Sex] = types.writeonce.enum(Sex)
#
writenonnullThe value is optional. And on creating, it can be null. After assigning a nonnull value, this field doesn't accept null value anymore.
#
Examples@jsonclassclass User: name: Optional[str] = types.writenonnull.str
#
readonlyThe value of the field cannot be set. It can only be set internally or through special transformer and callbacks.
#
Examples@pymongo@jsonclassclass User: id: str = types.readonly.str.primary.mongoid.required
#
internalmarked with internal will not be accepted as input, and it will not be present in output. These fields are internal and hidden from users.
#
Example@jsonclassclass Message: credential: str = types.str.internal.required
#
readwriteThe value of the field can be set and can be get. This is the default field behavior.
#
Examples@jsonclassclass User: name: Optional[str] = types.readwrite.str
#
tempThis field is a temporary field. Values of temporary fields won't be serialized into database. Temp fields are optional fields.
#
Examples@jsonclassclass User: password: str = types.str.required old_password: Optional[str] = types.str.temp
#
getterMark a field as calculated field. It's not stored.
#
Examples@jsonclassclass Student: name: str upper_name: str = types.str.getter(types.this.fval("name").toupper)
#
setterSetter to a calculated field.
#
Usagesetter(Callable | Types)
#
Examples@jsonclassclass Student: name: str upper_name: str = types.str .getter(types.this.fval("name").toupper) .setter(types.this.assign("name", types.passin.tolower))
#
Transformers#
transformApply the transformer on the value.
#
Usagetransform(Callable | Types)
#
Example@jsonclassclass User: name: str = types.str.transform(upper_transformer).required
#
defaultAssign field a default value if the value of field is 'None'.
#
Usagedefault(Any | Callable | Types)
#
Examples@jsonclassclass User: name: str = types.str.default('Snoopy').required
#
String Transformers#
truncateTruncate the str or list. This should not be used on a reference field.
#
Usagetruncate(int | Callable | Types)
#
Examples@jsonclassclass Article: title: str content: str = types.str.truncate(5000).required
#
trimRemove whitespaces and newlines from the beginning and end of the string.
#
Examples@jsonclassclass Article: title: str = types.str.trim.required content: str = types.str.truncate(5000).required
#
totitleTransform the string to title case.
#
Examples@jsonclassclass Article: title: str = types.str.trim.totitle.required content: str = types.str.truncate(5000).required
#
tocapCapitalize the string value.
#
Examples@jsonclassclass Article: title: str = types.str.trim.tocap.required
#
tolowerConvert the string value to lowercase.
#
Examples@jsonclassclass User: username: str = types.str.trim.tolower.required
#
toupperConvert the string value to uppercase.
#
Examples@jsonclassclass Term: abbr: str = types.str.trim.toupper.required
#
replaceReplace old substring with new substring.
#
Usagereplace(str | Callable | Types, str | Callable | Types)
#
Examples@jsoncalssclass Paper: title: str = types.str.replace("?", ".").required
#
replacerReplace the pattern with new substring.
#
Usagereplacer(str | Callable | Types, str | Callable | Types)
#
Examples@jsoncalssclass Paper: content_with_number_hidden: str = types.str.replacer("\d", "*").required
#
splitSplit a string value into a string list.
#
Usagesplit(str | Callable | Types)
#
Example@jsonclassclass Article: keywords: list[str] = types.split(' ').listof(str).required
#
joinTake all items in an iterable and joins them into one string.
#
Usagejoin(str | Callable | Types)
#
Example@jsonclassclass Band: all_member_name: str = types.join('-').str.required
#
saltAdd a salt to a string.
#
Example@jsonclassclass User: password: str = types.str.salt.required
#
tostrTransforms value into a string.
#
Example@jsonclassclass Student: age_str: str = types.tostr.str.required
#
padstartPad the current string of the start with a given char to reache a given length.
#
Usagepadstart(str | Callable | Types, int | Callable | Types)
#
Example@jsonclassclass Entrants: two_digit_number: str = types.str.padstart('0', 2).required
#
padendPad the current string of the end with a given char to reache a given length.
#
Usagepadend(str | Callable | Types, int | Callable | Types)
#
Example@jsonclassclass User: mask_id: str = types.str.padend('*', 18).required
#
Number Transformer#
upperbondDecrease the value of the field to upperbond
if the value of the field is larger than upperbond
.
#
Usageupperbond(int | float | Callable | Types)
#
Examples@jsonclassclass User: age: int = types.int.upperbond(150).required
#
lowerbondIncrease the value of the field to lowerbond
if the value of the field is smaller than lowerbond
.
#
Usagelowerbond(int | float | Callable | Types)
#
Examples@jsonclassclass User: age: int = types.int.lowerbond(1).required
#
filterFilter a list of values.
#
Usagefilter(Callable | Types)
#
Examples@jsonclassclass Number: even: list[int] = typse.listof(int).filter(lambda i: i % 2 == 0).required odd: list[int] = types.listof(int).filter(types.mod(2).neq(0)).required
#
mapMap a list of values.
#
Usagemap(Callable | Types)
#
Examples@jsonclassclass Number: plus_one: list[int] | None = types.listof(str).map(lambda i: i + 1) plus_two: list[int] | None = types.listof(str).map(types.add(2))
#
sqrtReturn the square root of any number that can not less than 0.
#
Examples@jsonclassclass Math: num1: int = types.int.sqrt.required num2: float = types.float.sqrt.required
#
powReturn the value of the field to the power of the value of pow.
#
Usagepow(int | float | Callable | Types)
#
Examples@jsonclassclass Math: num: int = types.int.pow(3).required
#
modReturn the remainder after the division of the value of field by another value. The value of field can be either int or float.
#
Usagemod(int | float | Callable | Types)
#
Examples@jsonclassclass Math: num: int = types.int.mod(10).required
#
divDivide the value of field by another value.
#
Usagediv(int | float | Callable | Types)
#
Examples@jsonclassclass Math: num: int = types.int.div(10).required
#
mulMultiply the value of field.
#
Usagemul(int | float | Callable | Types)
#
Examples@jsonclassclass Math: num: int = types.int.mul(10).required
#
subSubtracte the value of field.
#
Usagesub(int | float | Callable | Types)
#
Examples@jsonclassclass Math: num: int = types.int.sub(5).required
#
addAdd the value of field.
#
Usageadd(int | float | Callable | Types)
#
Examples@jsonclassclass Math: num: int = types.int.add(5).required
#
tointConvert the value of field to int.
#
Examples@jsonclassclass User: item: int = types.toint.int.required
#
tofloatConvert the value of field to float.
#
Examples@jsonclassclass User: item: float = types.tofloat.float.required
#
absReturn the absolute value of the value of field.
#
Examples@jsonclassclass Math: num: int = types.int.abs.required
#
floorReturn the floor of the value of field i.e., the least integer not less than the value of field.
#
Examples@jsonclassclass Math: num: int = types.int.floor.required
#
ceilReturn the ceiling of the value of field i.e., the largest integer not greater than the value of field.
#
Examples@jsonclassclass Math: num: int = types.int.ceil.required
#
roundRound the value of field to only two decimals.
#
Examples@jsonclassclass Math: num: int = typse.int.round.required
#
inverseChange the value to false if value is true, vice versa.
#
Examples@jsonclassclass User: true_or_false: bool = types.bool.inverse.required
#
Boolean Transformers#
inverseChange the value to false if value is true, vice versa.
#
Examples@jsonclassclass User: true_or_false: bool = types.bool.inverse.required
#
toboolTransform the value of field into a bool
#
Examples@jsonclassclass User: is_admin: bool = types.tobool.bool.required
#
Datetime Transformers#
tobosecTransform datetime into the beginning of the second.
#
Examples@jsonclassclass Datetime: time: datetime = types.datetime.tobosec.required
#
tobominTransform datetime into the beginning of the minute.
#
Examples@jsonclassclass Datetime: time: datetime = types.datetime.tobomin.required
#
tobohourTransform datetime into the beginning of the hour.
#
Examples@jsonclassclass Datetime: time: datetime = types.datetime.tobohour.required
#
tobodayTransform datetime or date into the beginning of the day.
#
Examples@jsonclassclass Datetime: time: date = types.date.toboday.required
#
tobomonTransform datetime or date into the beginning of the month.
#
Examples@jsonclassclass Datetime: time: date = types.date.tobomon.required
#
toboyearTransform datetime or date into the beginning of the year.
#
Example@jsonclassclass Datetime: time: date = types.date.toboyear.required
#
tonextsecChange datetime to the next second.
#
Examples@jsonclassclass Datetime: time: datetime = types.datetime.tonextsec.required
#
tonextminChange datetime to the next minute.
#
Examples@jsonclassclass Datetime: time: datetime = types.datetime.tonextmin.required
#
tonexthourChange datetime to the next hour.
#
Examples@jsonclassclass Datetime: time: datetime = types.datetime.tonexthour.required
#
tonextdayChange datetime or date to the next day.
#
Examples@jsonclassclass Datetime: time: date = types.date.tonextday.required
#
tonextmonChange datetime or date to the next mon.
#
Examples@jsonclassclass Datetime: time: date = types.date.tonextmon.required
#
tonextyearChange datetime or date to the next year.
#
Examples@jsonclassclass Datetime: time: date = types.date.tonextyear.required
#
fmtdFormat datetime or date.
#
Usagefmtd(str | Callable | Types)
#
Examples@jsonclassclass Datetime: time: datetime = types.datetime.fmtd("%Y年%m月%d日 %H:%M:%S").required
#
Iterable Transformer#
wrapintolistWrap value into a list.
#
Example@jsonclassclass AlwaysList: something_list: list[Any] = types.wrapintolist.listof(Any).required
#
tolistTransform the value of field into a list
#
Examples@jsonclassclass Student: age: int = types.int.tolist.required
#
reverseReverse the elements of the list
#
Examples@jsonclassclass Student: name: list[str] = types.listof(str).reverse.required
#
insertatInsert an element to the list.
#
Usageinsertat(Any | Callable | Types, int | Callable | Types)
#
Example@jsonclassclass List: name_list: list[str] = types.append("Lucy", 3).listof(str).required
#
appendAdd an element to the start of list.
#
Usageappend(str | int | float | Callable | Types)
#
Example@jsonclassclass List: name_list: list[str] = types.append("Jack").listof(str).required
#
prependAdd an element to the end of list.
#
Usageprepend(str | int | float | Callable | Types)
#
Example@jsonclassclass List: name_list: list[str] = types.prepend("Ben").listof(str).required
#
lenGet the length of iterable value.
#
Example@jsonclassclass Article: content: str words: int = types.int.getter(types.this.fval('content').split(" ").len)
#
Validators#
validateOn invalid, output message as error message.
#
Usagevalidate(Callable | Types)
#
Example@jsonclassclass User: password: str = types.validate(is_str_validator)
#
vmsgMark takes a modifier callable as its sole argument. Use this to define custom field value validations.
#
Usagevmsg(Callable | Types, str)
#
Example@jsonclassclass User: password: str = types.str.vmsg(types.length(6, 16), "Password length 6 to 16 digits").required
#
invalidField marked with invalid will never be valid.
#
Example@jsonclassclass MyClass: invalid_field: str = types.str.invalid.required
#
compareValidate field value by compare old and new values.
#
Usagecompare(Callable)
#
Examples@jsonclassclass Student: height: int = types.int.compare(15).required
#
String Validators#
matchValidate string value against pattern.
#
Usagematch(str | Callable | Types)
#
Example@jsonclassclass User: phone_number: str= types.str.match('^\+86').required
#
urlValidate string value against valid url.
#
Example@jsonclassclass User: user_link: str = types.str.url.required
#
emailValidate string value aganist valid email.
#
Example@jsaonclassclass User: email: str = types.str.email.required
#
hexcolorValidate string value against valid hex color. This also removes # at the beginning of the value.
#
Example@jsonclassclass Theme: tint_color: str = types.str.hexcolor.required
#
securepwValidate string value aganist valid secure password.
#
Example@jsaonclassclass User: password: str = types.str.securepw.required
#
digitValidate a string value against digit string.
#
Example@jsonclassclass Product: digit_id: str = types.str.digit.required
#
alphaValidate a string value against alpha string.
#
Example@jsonclassclass User: frist_name: str = types.str.alpha.required
#
numericValidate a string value against numeric string.
#
Example@jsonclassclass User: age: str = types.str.numeric.required
#
alnumValidate a string value against alnum string.
#
Example@jsonclassclass User: alpha_age: str = types.str.alnum.required
#
Number Validators#
minValidate number value against min value.
#
Usagemin(int | float | Callable | Types)
#
Example@jsonclassclass Teenager: age: int = types.int.min(13).required
#
maxValidate number value against max value.
#
Usagemax(int | float | Callable | Types)
#
Example@jsonclassclass Baby: age: int = types.int.max(1).required
#
lteValidate number value against min value.
#
Usagelte(int | float | Callable | Types)
#
Example@jsonclassclass PreSchool: age: int = types.int.lte(10).required
#
gteValidate number value against max value.
#
Usagegte(int | float | Callable | Types)
#
Example@jsonclassclass TallBoy: height: float = types.float.gte(170).required
#
ltValidate number value less than min value.
#
Usagelt(int | float | Callable | Types)
#
Example@jsonclassclass Preschool: age: int = types.int.lt(5).required
#
gtValidate number value greater than max value.
#
Usagegt(int | float | Callable | Types)
#
Example@jsonclassclass Adult: age: int = types.int.gt(18).required
#
rangeValidate number value against a range.
#
Example@jsonclassclass YoungAdult: age: int = types.int.range(18, 21).required
#
nonnegativeValidate number value greater than or equal to zero.
#
Example@jsonclassclass Product: orders: int = types.int.nonnegative.required
#
nonpositiveValidate number value less than or equal to zero.
#
Example@jsonclassclass Ice: temperature: int = types.int.nonnegative.required
#
negativeValidate number value less than zero.
#
Example@jsonclassclass Product: price: int = types.int.negative.required
#
positiveValidate number value greater than zero.
#
Example@jsonclassclass Asset: debt: int = types.int.nonnegative.required
#
oddMarked with int should be odd.
#
Example@jsonclassclass OddNumber: num: int = types.int.odd.required
#
evenMarked with int should be even.
#
Example@jsonclassclass EvenNumber: num: int = types.int.even.required
#
Datetime Validators#
beforeValidate date against before date.
#
Usagebefore(date | datetime | Callable | Types)
#
Examples@jsonclassclass Date: Monday: date = types.date.before(Tuesday).required
#
afterValidate date against after date.
#
Usageafter(date | datetime | Callable | Types)
#
Examples@jsonclassclass Date: Saturday: date = types.date.after(Firday).required
#
Iterable Validators#
oneofValidate string value against a list of available values.
#
Usageoneof(list[Any] | Callable | Types)
#
Example@jsonclassclass SchoolUser: role: str = types.str.oneof(['teacher', 'student']).required
#
lengthValidate iterable value against the provided length.
#
Usagelength(int | Callable | Types, int | Callable | Types | None)
#
Example@jsonclassclass User: password: str = types.str.lenth(6, 16).required
#
minlengthValidate iterable value against min length.
#
Usageminlenth(int | Callable | Types)
#
Example@jsonclassclass User: username: str = types.str.minlength(5).required
#
maxlengthValidate iterable value against max length.
#
Usagemaxlenth(int | Callable | Types)
#
Example@jsonclassclass Article: title: str = types.str.maxlength(50).required
#
hasprefixCheck if the value of field is prefix of the string or list value
#
Usagehasprefix(str | list[Any] | Callable | Types)
#
Examples@jsonclassclass Student: phone_number: str = types.str.hasprefix("+86").required
#
hassuffixCheck if the value of field is suffix of the string or list value
#
Usagehassuffix((str | list[Any] | Callable | Types))
#
Examples@jsonclassclass Student: qq_email: str = types.str.hassuffix("@qq.com").required
#
isprefixofCheck if the string or list value is prefix of the value of field
#
Usageisprefixof((str | list[Any] | Callable | Types))
#
Examples@jsonclassclass Teacher: calling_code: str = types.str.isprefixof(types.this.fval('phone_number')).required
#
issuffixofCheck if the string or list value is suffix of the value of field
#
Usageissuffixof((str | list[Any] | Callable | Types))
#
Examples@jsonclassclass Student: email_suffix: str = types.str.issuffixof(types.this.fval('email')).required
#
Formatters#
fmtFormat a field's value to the output json.
#
Usagefmt(Callable | Types)
#
Example@jsonclassclass Profile: theme_color: str = types.str.hexcolor.fmt(types.prepend('#')).required
#
Before Saving Actions#
setonsaveSets the field's value on object save.
#
Usagesetonsave(Callale | Types)
#
Example@jsonclassclass User: updated_at: types.datetime.readonly.timestamp('updated') \ .default(datetime.now) \ .setonsave(lambda: datetime.now()).required
#
fsetonsaveForcely sets the field's value on object save regardless of the object's modified state.
#
Example@jsonclassclass AuthorizationCode: phone_no: str = types.str.digit.required value: str = types.str.fsetonsave(types.randomdigits(4)).required
#
onsaveThe action to perform when saving is triggered.
#
Usageonsave(Callable)
#
Example@jsonclassclass User: email: str = types.str.onsave(send_email).required
#
onupdateThis is triggered when the value of this field is modified and object saving is triggered.
#
Usageonupdate(Callable)
#
Example@jsonclassclass User: username: str password: str = types.str.onupdate(send_email).required
#
onwriteIt is called when field has a new value and saving is triggered.
#
Usageonwrite(Callable)
#
Example@jsonclassclass Order: order_num: int = types.int.onwrite(send_message).required
#
Graph Relationships#
embeddedThis object is embedded into it's containing object. This is the default behavior.
#
linktoThis field has a local reference key defined on the object.
#
Examples@jsonclassclass User: articles: list[Article] = types.listof('Article').linkedby('user')
@jsonclassclass Article: user: User = types.objof('User').linkto.required
#
linkedbyThis field has a foreign key reference on the referenced object.
#
Usagelinkedby(str)
#
Examples@jsonclassclass User: articles: list[Article] = types.listof('Article').linkedby('user')
@jsonclassclass Article: user: User = types.objof('User').linkto.required
#
linkedthruThis field has a foreign key mapping table with the referenced object.
#
Usagelinkedthru(str)
#
Examples@jsonclassclass User: products: list[Article] = types.listof('Product').linkedthru('users')
@jsonclassclass Product: users: list[User] = types.listof('User').linkedthru('products')
#
denyIf there are any objects are linked on this field, this object's deletion is denied.
#
Examples@jsonclassclass Profile: user: User = types.objof('User').linkto.deny.required
#
cascadeIf there are any objects are linked on this field, when this object is deleted, delete them, too.
#
Examples@jsonclassclass User: profile: Profile = types.objof('Profile').linkedby('user').cascade.required
#
nullifyIf there are any objects are linked on this field, when this object is deleted, set the reference keys (if there is any) to null. This is the default behavior.
#
ORM Instructions#
indexPerform indexing on this field.
#
Examples@jsonclassclass User: score: int = types.int.index.required
#
cindexMark with cindex have compound indexes. This modifier doesn't have any effect around transforming and validating.
#
Usagecindex(str)
#
Example@jsonclassclass Student: name: str = types.int.cindex("name_score").required score: int = types.int.cindex("name_score").required
#
uniquePerform unique indexing on this field.
#
Examples@jsonclassclass User: email: str = types.str.unique.required
#
cuniqueMark with cunique have compound indexes. This is a unique index. This modifier doesn't have any effect around transforming and validating.
#
Usagecunique(str)
#
Example@jsonclassclass Score: student_name: str = types.str.cunique("student_course").required course_name: str = types.str.cunique("student_course").required
#
queryableMarks a column should be queryable.
#
Example@jsonclassclass User: username: str = types.str.queryable.required
#
unqueryableMarks a column should be unqueryable.
#
Example@jsonclassclass User: username: str password: str = types.str.unqueryable.required
#
Permissons#
asopAssigns the transfromed operator to the current field.
#
Example@jsonclassclass User: name: str = types.str.required owned_teams: list[Team] = types.listof('Team').linkedby('owner')
@jsonclassclass Team: name: str = types.str.required owner: User = types.objof('User').linkto.asop(lambda o: o)
#
asopdAssigns the operator to the current field directly.
#
Example@jsonclassclass User: name: str = types.str.required owned_teams: list[Team] = types.listof('Team').linkedby('owner')
@jsonclassclass Team: name: str = types.str.required owner: User = types.objof('User').linkto.asopd
#
cancWhether this operator can create on this field.
#
Example@jsonclassclass User: email: str = types.str.canc(types.getop.isthis).required
#
canuWhether this operator can update on this field.
#
Example@jsonclassclass User: password: str = types.str.canu(types.getop.isthis).required
#
canrWhether this operator can read on this field.
#
Usagecanr(Callable | Types)
#
Example@jsonclassclass User: password: str = types.str.canr(types.getop.isthis).required
#
canwWhether this operator can write this field.
#
Usagecanw(Callable | Types)
#
Example@jsonclassclass User: password: str = types.str.canw(types.getop.isthis).required
#
authidentityUse for authorization.
#
Examples@authorized@api@pymongo@jsonclassclass User: username: str = types.str.authidentity.required
#
authbyFields marked with auth by are used for authorization.
#
Examples@authorized@api@pymongo@jsonclassclass User: username: str = types.str.authidentity.required password: str = types.str.authby(types.checkpw(types.passin)).required
#
authbycheckpwUse to authorization and check string value with bcrypt's checkpw function.
#
Example@authorized@api@pymongo@jsonclassclass User: username: str = types.str.authidentity.required password: str = types.str.authbycheckpw.required
#
Calc Pipeline#
passinUse passin value as the result.
#
Examples@authorized@api@pymongo@jsonclassclass User: username: str = types.str.authidentity.required password: str = types.str.authby(types.checkpw(types.passin)).required
#
checkpwCheck string value with bcrypt's checkpw function.
#
Usagecheckpw(Types)
#
Examples@authorized@api@pymongo@jsonclassclass User: username: str = types.str.authidentity.required password: str = types.str.authby(types.checkpw(types.passin)).required
#
randomalnumpuncsGenerate random alnumpuncs as result.
#
Usagerandomalnumpuncs(int | Callable | Types)
#
Examples@jsonclassclass WaterMark: water_mark: str = types.str.randomalnumpuncs(10).required
#
randomintGenerate random int as result.
#
Usagerandomint(int | float | Callable | Types, int | float | Callable | Types)
#
Examples@jsonclassclass SchoolClass: number_of_class: int = types.int.randomint(40, 50).required
#
randomfloatGenerate random int as result.
#
Usagerandomfloat(int | float | Callable | Types, int | float | Callable | Types)
#
Examples@jsonclassclass Math: num: float = types.float.randomfloat(1.1, 9.9)
#
crossfetchFetch a class with value matches this object's value at key.
#
Usagecrossfetch(str, str, Optional[str] = None)
#
Examples@jsonclassclass User: auth_code: Optional[str] = types.str.authby( types.crossfetch('AuthorizationCode', 'email').fval('value').eq(types.passin) ).temp
#
fvalGet value at field from a JSONClass object.
#
Usagefval(str | Callable | Types)
#
Examples@jsonclassclass Student: name: str upper_name: str = types.str.getter(types.this.fval("name").toupper)
#
fobjGet the referenced object at field from a JSONClass object. If the object is not attached yet, include it.
#
Usagefobj(str | Callable | Types)
#
Examples@pymongo@jsonclass( can_create=types.getop.isobj(types.this.fobj('todo_list').fval('owner')),)class TodoEntry: id: str = types.readonly.str.primary.mongoid.required title: str = types.str.default('').required owner: User = types.readonly.objof('User').linkto.asopd.required todo_list: TodoList = types.objof('TodoList').linkto.required created_at: datetime = types.readonly.datetime.tscreated.required updated_at: datetime = types.readonly.datetime.tsupdated.required
#
thisGet the owner object of this field.
#
Examples@jsonclassclass Student: name: str upper_name: str = types.str.getter(types.this.fval("name").toupper).required
#
atReturn result with subscription index.
#
Usageat(Any | Callable | Types)
#
Examples@jsonclassclass Student: scorces: list[int] second_score: int = types.int.getter(types.this.at(1)).required
#
assignAssign value to the current object.
#
Usageassign(str, Any | Callable | Types)
#
Examples@jsonclassclass Student: name: str upper_name: str = types.str .getter(types.this.fval("name").toupper) .setter(types.this.assign("name", types.passin.tolower))
#
uploaderUpload file stream to cloud storage and get the string url back.
#
Usageuploader(str | Callable)
#
Examples@jsonclassclass Teacher: photo: str = types.str.uploader('image').required
#
getopGet the operator of this action.
#
Examples@jsonclassclass User: password: str = types.str.canu(types.getop.isthis).required
#
eqValid the value of field by equal testing.
#
Usageeq(Any | Types | Callable)
#
Examples@jsonclassclass Student: card_identity: str = types.str.eq('az213456').required
#
neqValid value by unequal testing.
#
Usageneq(Any | Types | Callable)
#
Examples@jsonclassclass Student: name: str = types.str.neq('Jack').required
#
isthisCheck whether the current value is the owner object.
#
Examples@jsonclassclass User: password: str = types.str.canu(types.getop.isthis).required
#
oneisvalidValid with subroutines.
#
Usageoneisvalid(list[Callable | Types])
#
Examples@authorized@api@pymongo@jsonclass(can_update=types.oneisvalid([ types.getop.isthis, types.getop.isobjof('Admin')]), can_delete=types.oneisvalid([ types.getop.isthis, types.getop.isobjof('Admin')]))class User: id: str = types.readonly.str.primary.mongoid.required username: str = types.str.unique.authidentity.canu(types.getop.isthis).required
#
isobjofValid if the value is object of a class.
#
Usageisobjof(type[JObject] | str)
#
Examples@jsonclassclass Teacher: student: Student = types.objof("Student").isobjof('Student').required
#
isobjCheck if value is the same with provided object.
#
Usageisobj(Types)
#
Examples@jsonclassclass Student: myself: Student = types.objof('Student').isobj(types.this).required