justmars / statute-patterns

Construction of regex patterns for Philippine statutory law.
https://lawsql.com
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Add fields for sqlpyd use #4

Closed justmars closed 1 year ago

justmars commented 1 year ago
class Rule(BaseModel):
    """Used to identify the statute based on a category and identifier. The `col` and `index` field attributes are not native to Pydantic; these are populated in anticipation of future use via the `sqlpyd` library."""

    cat: StatuteCategory = Field(
        col=str,
        index=True,
    )
    idx: constr(to_lower=True) = Field(  # type: ignore
        ...,
        regex="[a-z0-9-]+",
        col=str,
        index=True,
    )

This becomes usable in the following context:

class CodeRow(Page, Rule, TableConfig):  # see Rule use here
    __prefix__ = "lex"
    __tablename__ = "codifications"
    __indexes__ = [["statute_category", "statute_serial_id"]]
    statute_id: str | None = Field(
        None, col=str, fk=(StatuteRow.__tablename__, "id")
    )

TableConfig is sqlpyd-native and detects that the fields cat and idx are going to be part of the database table "codifications".

justmars commented 1 year ago

See use here: https://github.com/justmars/lawrgx/blob/7b8ed0012e713282fda83db60a32b0300e0627cb/lawrgx/rules.py#L29