Skip to content

Commit

Permalink
add schema validator v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
capjamesg committed Aug 20, 2024
1 parent b1496f9 commit ff2474e
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from __future__ import annotations

from enum import Enum
from typing import Dict, Optional

from pydantic import BaseModel, ConfigDict, model_validator

VALID_QUERY_TYPES = ["contains", "equals", "starts_with"]
VALID_OPERATOR_QUERY_TYPES = ["or", "and"]


class QueryType(str, Enum):
contains = "contains"
equals = "equals"
starts_with = "starts_with"


class AndOperatorQueryType(str, Enum):
and_ = "and"


class OrOperatorQueryType(str, Enum):
or_ = "or"


class QueryItem(BaseModel):
model_config = ConfigDict(extra="forbid")

contains: Optional[str] = None
equals: Optional[str] = None
starts_with: Optional[str] = None

strict: Optional[bool] = False
boost: Optional[int] = 1

# ensure that only one of the query types is used
@model_validator(mode="after")
def validate_query_type(cls, v):
query_types = [v.contains, v.equals, v.starts_with]

if len([qt for qt in query_types if qt]) > 1:
raise ValueError("Only one query type can be used")

return v


class RootQuery(BaseModel):
query: (
Dict[AndOperatorQueryType, Dict[str, QueryItem]]
| Dict[OrOperatorQueryType, Dict[str, QueryItem]]
| Dict[str, QueryItem]
)
limit: Optional[int] = 10
sort_by: Optional[str] = "score"


query = {
"query": {
"or": {
"post": {"contains": "taylor swift", "strict": False, "boost": 1},
"title": {"contains": "my desk", "strict": True, "boost": 25},
}
},
"limit": 4,
"sort_by": "score",
}

# validate query
print(RootQuery(**query))

0 comments on commit ff2474e

Please sign in to comment.