| 
5 | 5 | import logging  | 
6 | 6 | import re  | 
7 | 7 | from datetime import datetime  | 
 | 8 | +from functools import partial  | 
 | 9 | +from typing import Annotated  | 
8 | 10 | 
 
  | 
9 |  | -from pydantic import BaseModel, ConfigDict, Field, field_validator  | 
 | 11 | +from pydantic import BaseModel, BeforeValidator, ConfigDict, Field  | 
10 | 12 | 
 
  | 
11 | 13 | logger = logging.getLogger(__name__)  | 
12 | 14 | 
 
  | 
@@ -49,26 +51,29 @@ def normalize_rrid_tags(rrid_tag: str, *, with_prefix: bool = True) -> str:  | 
49 | 51 | 
 
  | 
50 | 52 | 
 
  | 
51 | 53 | class ResourceHit(BaseModel):  | 
52 |  | -    rrid: str = Field(..., alias="rid")  | 
 | 54 | +    rrid: Annotated[str, Field(alias="rid")]  | 
53 | 55 |     name: str  | 
54 | 56 | 
 
  | 
55 | 57 | 
 
  | 
56 | 58 | # webserver API models -----------------------------------------  | 
57 | 59 | class ResearchResource(BaseModel):  | 
58 |  | -    rrid: str = Field(  | 
59 |  | -        ...,  | 
60 |  | -        description="Unique identifier used as classifier, i.e. to tag studies and services",  | 
61 |  | -        pattern=STRICT_RRID_PATTERN,  | 
62 |  | -    )  | 
 | 60 | +    rrid: Annotated[  | 
 | 61 | +        str,  | 
 | 62 | +        BeforeValidator(  | 
 | 63 | +            partial(normalize_rrid_tags, with_prefix=True),  | 
 | 64 | +        ),  | 
 | 65 | +        Field(  | 
 | 66 | +            description="Unique identifier used as classifier, i.e. to tag studies and services",  | 
 | 67 | +            pattern=STRICT_RRID_PATTERN,  | 
 | 68 | +        ),  | 
 | 69 | +    ]  | 
63 | 70 |     name: str  | 
64 | 71 |     description: str  | 
65 | 72 | 
 
  | 
66 |  | -    @field_validator("rrid", mode="before")  | 
67 |  | -    @classmethod  | 
68 |  | -    def _format_rrid(cls, v):  | 
69 |  | -        return normalize_rrid_tags(v, with_prefix=True)  | 
70 |  | - | 
71 |  | -    model_config = ConfigDict(from_attributes=True, str_strip_whitespace=True)  | 
 | 73 | +    model_config = ConfigDict(  | 
 | 74 | +        from_attributes=True,  | 
 | 75 | +        str_strip_whitespace=True,  | 
 | 76 | +    )  | 
72 | 77 | 
 
  | 
73 | 78 | 
 
  | 
74 | 79 | # postgres_database.scicrunch_resources ORM --------------------  | 
 | 
0 commit comments