otsuvalidator 2.0.2__tar.gz → 2.0.3__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.5
2
2
  Name: otsuvalidator
3
- Version: 2.0.2
3
+ Version: 2.0.3
4
4
  Summary: 単体でもディスクリプタとしても使用できるバリデータライブラリ
5
5
  Author-email: Otsuhachi <agequodagis.tufuiegoeris@gmail.com>
6
6
  License: MIT License
@@ -59,4 +59,4 @@ __all__ = [
59
59
 
60
60
  logging.getLogger(__name__).addHandler(logging.NullHandler())
61
61
 
62
- __version__ = "2.0.2"
62
+ __version__ = "2.0.3"
@@ -88,6 +88,7 @@ class VChain[OutT](Validator[OutT]):
88
88
  def __init__(self, *validators: Validator[Any], pass_type: Literal["all", "any"] = "any") -> None:
89
89
  super().__init__()
90
90
  self.validators: tuple[Validator[Any], ...] = validators
91
+ self.__target_loggers = [logging.getLogger(mn) for mn in {v.__module__ for v in validators}]
91
92
  self.pass_type: Literal["all", "any"] = pass_type
92
93
 
93
94
  def validate(self, value: Any) -> OutT:
@@ -97,18 +98,28 @@ class VChain[OutT](Validator[OutT]):
97
98
  for validator in validators:
98
99
  current_value = validator.validate(current_value)
99
100
  else:
100
- passed = False
101
- exceptions = []
102
- for validator in validators:
103
- try:
104
- tmp = validator.validate(current_value)
105
- current_value = tmp
106
- passed = True
107
- except Exception as ex:
108
- exceptions.append(ex)
109
- if not passed:
110
- logger.error(f"全てのバリデータでの検証に失敗しました: {value}")
111
- raise ValueError(*exceptions)
101
+ # 複数のバリデータのいずれかが通過すればOKなので途中の検証エラーログを無効化する
102
+ original_levels: dict[logging.Logger, int] = {}
103
+ for tl in self.__target_loggers:
104
+ original_levels[tl] = tl.level
105
+ tl.setLevel(logging.CRITICAL + 1)
106
+
107
+ try:
108
+ passed = False
109
+ exceptions = []
110
+ for validator in validators:
111
+ try:
112
+ tmp = validator.validate(current_value)
113
+ current_value = tmp
114
+ passed = True
115
+ except Exception as ex:
116
+ exceptions.append(ex)
117
+ if not passed:
118
+ logger.error(f"全てのバリデータでの検証に失敗しました: {value}")
119
+ raise ValueError(*exceptions)
120
+ finally:
121
+ for tl, level in original_levels.items():
122
+ tl.setLevel(level)
112
123
  return current_value
113
124
 
114
125
  def __or__[NextOutT](self, other: Validator[NextOutT]) -> "VChain[NextOutT]":
@@ -1,10 +1,17 @@
1
1
  import io
2
+ import logging
2
3
  import sys
3
4
  import unittest
4
5
  from typing import Any
5
6
 
6
7
  from otsuvalidator import VChain, VChoice, VDict, VInt, VList, VNone, VString
7
8
 
9
+ logger = logging.getLogger(__name__)
10
+ logging.basicConfig(
11
+ level=logging.DEBUG,
12
+ format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
13
+ )
14
+
8
15
 
9
16
  class Student:
10
17
  name = VString(min_len=1, max_len=50, checker=str.istitle, pattern=r"^[a-zA-Z]+ [a-zA-Z]+$") # 1~50文字, タイトルケース, 正規表現
File without changes
File without changes
File without changes