Skip to content

Commit 3c6d019

Browse files
Merge pull request #55 from data-apis/empty-fields
Empty fields
2 parents f68b3c2 + 15b2191 commit 3c6d019

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

record_api/apis.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,24 @@ def bad_name(name: str) -> bool:
3131

3232

3333
class BaseModel(pydantic.BaseModel):
34+
class Config:
35+
extra = "forbid"
36+
37+
# Set fields set to be all those that are truthy, so only those are expoed with `skip_defaults`
38+
@property # type: ignore
39+
def __fields_set__(self) -> typing.Set[str]: # type: ignore
40+
s = set()
41+
for k, v in self.__values__.items():
42+
if v:
43+
s.add(k)
44+
return s
45+
46+
@__fields_set__.setter
47+
def __fields_set__(self, val: typing.Set[str]) -> None:
48+
pass
49+
3450
def __repr_args__(self) -> pydantic.ReprArgs: # type: ignore
51+
# Dont show empty valyes
3552
for k, v in super().__repr_args__():
3653
if v:
3754
yield k, v
@@ -58,7 +75,7 @@ def __ior__(self, other: API) -> API:
5875
return self
5976

6077
def json(self, **kwargs) -> str:
61-
return super().json(exclude_none=True, **kwargs)
78+
return super().json(exclude_none=True, skip_defaults=True, **kwargs)
6279

6380

6481
class Module(BaseModel):

0 commit comments

Comments
 (0)