sera-2 1.7.2__py3-none-any.whl → 1.8.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -235,6 +235,7 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
235
235
  idprop = cls.get_id_property()
236
236
 
237
237
  draft_clsname = "Draft" + cls.name
238
+ draft_validators = f"draft{cls.name}Validators"
238
239
 
239
240
  program = Program()
240
241
  program.import_(f"@.models.{pkg.dir.name}.{cls.name}.{cls.name}", True)
@@ -259,7 +260,7 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
259
260
  cls_pk = None
260
261
  observable_args: list[tuple[expr.Expr, expr.ExprIdent]] = []
261
262
  prop_defs = []
262
- prop_validators = []
263
+ prop_validators: list[tuple[expr.ExprIdent, expr.Expr]] = []
263
264
  prop_constructor_assigns = []
264
265
  # attrs needed for the cls.create function
265
266
  create_args = []
@@ -267,12 +268,15 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
267
268
  ser_args = []
268
269
  update_field_funcs: list[Callable[[AST], Any]] = []
269
270
 
271
+ prop2tsname = {}
272
+
270
273
  for prop in cls.properties.values():
271
274
  # if prop.data.is_private:
272
275
  # # skip private fields as this is for APIs exchange
273
276
  # continue
274
277
 
275
278
  propname = to_camel_case(prop.name)
279
+ prop2tsname[prop.name] = propname
276
280
 
277
281
  def _update_field_func(
278
282
  prop: DataProperty | ObjectProperty,
@@ -373,8 +377,32 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
373
377
  ser_args.append(
374
378
  (
375
379
  expr.ExprIdent(prop.name),
376
- PredefinedFn.attr_getter(
377
- expr.ExprIdent("this"), expr.ExprIdent(propname)
380
+ (
381
+ expr.ExprTernary(
382
+ PredefinedFn.attr_getter(
383
+ expr.ExprFuncCall(
384
+ PredefinedFn.attr_getter(
385
+ expr.ExprIdent(draft_validators),
386
+ expr.ExprIdent(propname),
387
+ ),
388
+ [
389
+ PredefinedFn.attr_getter(
390
+ expr.ExprIdent("this"),
391
+ expr.ExprIdent(propname),
392
+ )
393
+ ],
394
+ ),
395
+ expr.ExprIdent("isValid"),
396
+ ),
397
+ PredefinedFn.attr_getter(
398
+ expr.ExprIdent("this"), expr.ExprIdent(propname)
399
+ ),
400
+ expr.ExprIdent("undefined"),
401
+ )
402
+ if prop.is_optional
403
+ else PredefinedFn.attr_getter(
404
+ expr.ExprIdent("this"), expr.ExprIdent(propname)
405
+ )
378
406
  ),
379
407
  )
380
408
  )
@@ -506,6 +534,26 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
506
534
  )
507
535
  )
508
536
 
537
+ # TODO: fix me!
538
+ prop_validators.append(
539
+ (
540
+ expr.ExprIdent(propname),
541
+ expr.ExprFuncCall(
542
+ expr.ExprIdent("getValidator"),
543
+ [
544
+ PredefinedFn.list(
545
+ [
546
+ expr.ExprConstant(
547
+ constraint.get_typescript_constraint()
548
+ )
549
+ for constraint in prop.data.constraints
550
+ ]
551
+ ),
552
+ ],
553
+ ),
554
+ )
555
+ )
556
+
509
557
  prop_defs.append(stmt.DefClassVarStatement(propname, tstype.type))
510
558
  prop_constructor_assigns.append(
511
559
  stmt.AssignStatement(
@@ -570,10 +618,6 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
570
618
  lambda ast10: ast10.class_(draft_clsname)(
571
619
  *prop_defs,
572
620
  stmt.LineBreak(),
573
- stmt.DefClassVarStatement(
574
- "validators", type=None, value=validators, is_static=True
575
- ),
576
- stmt.LineBreak(),
577
621
  lambda ast10: ast10.func(
578
622
  "constructor",
579
623
  [
@@ -648,16 +692,37 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
648
692
  *update_field_funcs,
649
693
  stmt.LineBreak(),
650
694
  lambda ast14: ast14.func(
695
+ "isValid",
696
+ [],
697
+ expr.ExprIdent("boolean"),
698
+ comment="Check if the draft is valid (only check the required fields as the non-required fields if it's invalid will be set to undefined)",
699
+ )(
700
+ stmt.ReturnStatement(
701
+ stmt.TypescriptStatement(
702
+ " && ".join(
703
+ f"{draft_validators}.{prop2tsname[prop.name]}(this.{prop2tsname[prop.name]}).isValid"
704
+ for prop in cls.properties.values()
705
+ if not prop.is_optional
706
+ )
707
+ )
708
+ )
709
+ ),
710
+ stmt.LineBreak(),
711
+ lambda ast15: ast15.func(
651
712
  "ser",
652
713
  [],
653
714
  expr.ExprIdent("any"),
654
- comment="Serialize the draft to communicate with the server",
715
+ comment="Serialize the draft to communicate with the server. `isValid` must be called first to ensure all data is valid",
655
716
  )(
656
717
  stmt.ReturnStatement(
657
718
  PredefinedFn.dict(ser_args),
658
719
  ),
659
720
  ),
660
721
  ),
722
+ stmt.LineBreak(),
723
+ stmt.TypescriptStatement(
724
+ f"export const {draft_validators} = " + validators.to_typescript() + ";"
725
+ ),
661
726
  )
662
727
 
663
728
  pkg.module("Draft" + cls.name).write(program)
@@ -904,6 +969,9 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
904
969
  program.import_(
905
970
  f"@.models.{pkg.dir.name}.Draft{cls.name}.Draft{cls.name}", True
906
971
  )
972
+ program.import_(
973
+ f"@.models.{pkg.dir.name}.Draft{cls.name}.draft{cls.name}Validators", True
974
+ )
907
975
  program.root(
908
976
  stmt.LineBreak(),
909
977
  stmt.TypescriptStatement(
@@ -995,6 +1063,10 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
995
1063
  + "}"
996
1064
  ),
997
1065
  ),
1066
+ (
1067
+ expr.ExprIdent("validators"),
1068
+ expr.ExprIdent(f"draft{cls.name}Validators"),
1069
+ ),
998
1070
  ]
999
1071
  + (
1000
1072
  [
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sera-2
3
- Version: 1.7.2
3
+ Version: 1.8.0
4
4
  Summary:
5
5
  Author: Binh Vu
6
6
  Author-email: bvu687@gmail.com
@@ -12,7 +12,7 @@ sera/make/make_app.py,sha256=n9NtW73O3s_5Q31VHIRmnd-jEIcpDO7ksAsOdovde2s,5999
12
12
  sera/make/make_python_api.py,sha256=RuJUm9z-4plBEtjobeOPr12o27OT-0tSeXI4ZlM3IY0,29433
13
13
  sera/make/make_python_model.py,sha256=xf4revAwVWEnF6QhxbbqPyUGgXOOB--Gu3jPxsESg0Y,36593
14
14
  sera/make/make_python_services.py,sha256=RsinYZdfkrTlTn9CT50VgqGs9w6IZawsJx-KEmqfnEY,2062
15
- sera/make/make_typescript_model.py,sha256=27lk4YgfJ6_7xxBirxJKgK0-wBNowMWZkvcMUFdmStE,46865
15
+ sera/make/make_typescript_model.py,sha256=9OyNG9hy_lEvlQFFbuOqg9iarNKVwzmpQXgZLXRyvoM,50315
16
16
  sera/misc/__init__.py,sha256=Dh4uDq0D4N53h3zhvmwfa5a0TPVRSUvLzb0hkFuPirk,411
17
17
  sera/misc/_formatter.py,sha256=aCGYL08l8f3aLODHxSocxBBwkRYEo3K1QzCDEn3suj0,1685
18
18
  sera/misc/_utils.py,sha256=V5g4oLGHOhUCR75Kkcn1w01pAvGvaepK-T8Z3pIgHjI,1450
@@ -29,6 +29,6 @@ sera/models/_parse.py,sha256=sJYfQtwek96ltpgxExG4xUbiLnU3qvNYhTP1CeyXGjs,9746
29
29
  sera/models/_property.py,sha256=CmEmgOShtSyNFq05YW3tGupwCIVRzPMKudXWld8utPk,5530
30
30
  sera/models/_schema.py,sha256=r-Gqg9Lb_wR3UrbNvfXXgt_qs5bts0t2Ve7aquuF_OI,1155
31
31
  sera/typing.py,sha256=Q4QMfbtfrCjC9tFfsZPhsAnbNX4lm4NHQ9lmjNXYdV0,772
32
- sera_2-1.7.2.dist-info/METADATA,sha256=xzCJfINCVbrv5Xd3oAKXJnhF_tYm7A51PpdTpdVqUW8,856
33
- sera_2-1.7.2.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
34
- sera_2-1.7.2.dist-info/RECORD,,
32
+ sera_2-1.8.0.dist-info/METADATA,sha256=fd-bIlMNnGNKyNDR8A2K2NiTCNbWiSTYyaX13R8H-nU,856
33
+ sera_2-1.8.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
34
+ sera_2-1.8.0.dist-info/RECORD,,
File without changes