Skip to content

Commit

Permalink
Added validators to nanopub model
Browse files Browse the repository at this point in the history
  • Loading branch information
wshayes committed Feb 4, 2021
1 parent 99630c9 commit 67a0d70
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion bel/schemas/nanopubs.py
Expand Up @@ -4,7 +4,7 @@

# Third Party
import pydantic
from pydantic import AnyUrl, BaseModel, Field, HttpUrl, validator
from pydantic import AnyUrl, BaseModel, Field, HttpUrl, root_validator, validator

# Local
from bel.schemas.bel import ValidationErrors
Expand All @@ -27,6 +27,13 @@ class Annotation(BaseModel):
class Config:
extra = "allow"

@validator("type")
def clean_type(cls, v):
"""Clean types which are merged together - taking first instance"""
if ";" in v:
v = v.split(";")[0]
return v


class Assertion(BaseModel):
subject: str
Expand All @@ -37,6 +44,11 @@ class Assertion(BaseModel):
class Config:
extra = "allow"

@validator("subject", "object")
def clean_assertion(cls, v):
v = v.replace("“", '"').replace("”", '"').strip()
return v


class CitationDatabase(BaseModel):
name: str
Expand All @@ -59,6 +71,33 @@ class Citation(BaseModel):
class Config:
extra = "allow"

@root_validator
def create_id(cls, values):
"""Generate citation id from database, uri or reference string"""
citation_id, database, uri, reference = (
values.get("id", None),
values.get("database", None),
values.get("uri"),
values.get("reference", None),
)
if not citation_id:
citation_id = ""
if database:
citation_id = f"{database.name}:{database.id}"
elif uri:
citation_id = uri
elif reference:
citation_id = reference

values.id = citation_id

return values


class Metadata(BaseModel):
class Config:
extra = "allow"


class NanopubBody(BaseModel):
"""Nanopub content"""
Expand Down

0 comments on commit 67a0d70

Please sign in to comment.