dplex 0.3.4__tar.gz → 0.3.6__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dplex
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary:
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -412,6 +412,49 @@ class DPService[
412
412
  """
413
413
  return [self._model_to_schema(model) for model in models]
414
414
 
415
+ # ==================== ВАЛИДАЦИОННЫЕ ХУКИ ====================
416
+ async def validate_create(self, create_data: CreateSchemaType) -> None:
417
+ """
418
+ Валидация перед созданием сущности.
419
+ Вызывается до преобразования схемы в модель и сохранения.
420
+ Переопределите в наследниках для доменной валидации.
421
+ При ошибке валидации выбросьте исключение.
422
+ """
423
+ ...
424
+
425
+ async def validate_update(self, update_data: UpdateSchemaType) -> None:
426
+ """
427
+ Валидация перед обновлением сущности.
428
+ Вызывается до применения изменений в БД.
429
+ Переопределите в наследниках для доменной валидации.
430
+ При ошибке валидации выбросьте исключение.
431
+ """
432
+ ...
433
+
434
+ async def transform_create_schema(
435
+ self, create_data: CreateSchemaType
436
+ ) -> CreateSchemaType:
437
+ """
438
+ Преобразование схемы создания перед сохранением.
439
+ Вызывается до валидации и преобразования в модель.
440
+ Переопределите в наследниках для нормализации данных
441
+ (trim полей, приведение регистра, подстановка значений и т.п.).
442
+ Возвращает схему (новый экземпляр или изменённую копию).
443
+ """
444
+ return create_data
445
+
446
+ async def transform_update_schema(
447
+ self, update_data: UpdateSchemaType
448
+ ) -> UpdateSchemaType:
449
+ """
450
+ Преобразование схемы обновления перед применением.
451
+ Вызывается до валидации и записи в БД.
452
+ Переопределите в наследниках для нормализации данных
453
+ (trim полей, приведение регистра и т.п.).
454
+ Возвращает схему (новый экземпляр или изменённую копию).
455
+ """
456
+ return update_data
457
+
415
458
  # ==================== CRUD ОПЕРАЦИИ ====================
416
459
  async def get_by_id(self, entity_id: KeyType | None) -> ResponseSchemaType | None:
417
460
  """
@@ -527,6 +570,8 @@ class DPService[
527
570
  Returns:
528
571
  Схема ответа с созданной сущностью
529
572
  """
573
+ create_data = await self.transform_create_schema(create_data)
574
+ await self.validate_create(create_data)
530
575
  model = self._create_schema_to_model(create_data)
531
576
  created_model = await self.repository.create(model)
532
577
  await self.session.flush()
@@ -545,7 +590,12 @@ class DPService[
545
590
  if not create_data_list:
546
591
  return []
547
592
 
548
- models = [self._create_schema_to_model(data) for data in create_data_list]
593
+ transformed_list: list[CreateSchemaType] = []
594
+ for data in create_data_list:
595
+ data = await self.transform_create_schema(data)
596
+ await self.validate_create(data)
597
+ transformed_list.append(data)
598
+ models = [self._create_schema_to_model(data) for data in transformed_list]
549
599
  created_models = await self.repository.create_bulk(models)
550
600
  await self.session.flush()
551
601
  return self._models_to_schemas(created_models)
@@ -565,12 +615,14 @@ class DPService[
565
615
  Returns:
566
616
  None
567
617
  """
618
+ update_data = await self.transform_update_schema(update_data)
568
619
  update_dict = self._make_update_dict(update_data)
569
620
  if not update_dict:
570
621
  raise ValueError(
571
622
  "DPService.update: Данные для обновления не могут быть пустыми"
572
623
  )
573
624
 
625
+ await self.validate_update(update_data)
574
626
  qb = self.repository.query()
575
627
  qb = self._apply_filter_to_query(qb, filter_data)
576
628
  await self.repository.update_by_query_builder(qb, update_dict)
@@ -589,12 +641,14 @@ class DPService[
589
641
  Returns:
590
642
  None
591
643
  """
644
+ update_data = await self.transform_update_schema(update_data)
592
645
  update_dict = self._make_update_dict(update_data)
593
646
  if not update_dict:
594
647
  raise ValueError(
595
648
  "DPService.update_by_id: Данные для обновления не могут быть пустыми"
596
649
  )
597
650
 
651
+ await self.validate_update(update_data)
598
652
  await self.repository.update_by_id(entity_id, update_dict)
599
653
  await self.session.flush()
600
654
 
@@ -614,12 +668,14 @@ class DPService[
614
668
  if not entity_ids:
615
669
  raise ValueError("DPService.update_by_ids: Список ID не может быть пустым")
616
670
 
671
+ update_data = await self.transform_update_schema(update_data)
617
672
  update_dict = self._make_update_dict(update_data)
618
673
  if not update_dict:
619
674
  raise ValueError(
620
675
  "DPService.update_by_ids: Данные для обновления не могут быть пустыми"
621
676
  )
622
677
 
678
+ await self.validate_update(update_data)
623
679
  await self.repository.update_by_ids(entity_ids, update_dict)
624
680
  await self.session.flush()
625
681
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "dplex"
3
- version = "0.3.4"
3
+ version = "0.3.6"
4
4
  description = ""
5
5
  authors = [
6
6
  {name = "Igor Chesnokov",email = "front-gold@mail.ru"}
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes