Skip to content

Extract validators from pydantic models #12

Open

Description

@kdp-cloud

Currently, there is no way to export field validators from pydantic to JSON schema.

E.g.: For assays there are two validators

  • filename => Must start with 'a_'
  • comments => At least one comment must be a valid target repository
class Assay(CommentedIsaBase):
    id: Optional[str] = Field(alias="@id", default=None)
    characteristicCategories: List[MaterialAttribute] = []
    dataFiles: List[Data] = []
    filename: Optional[str] = None
    materials: Optional[AssayMaterialType] = None
    measurementType: Optional[OntologyAnnotation] = None
    processSequence: List[Process] = []
    technologyPlatform: Optional[str] = None
    technologyType: Optional[OntologyAnnotation] = None
    unitCategories: List[OntologyAnnotation] = []

    @field_validator("filename")
    def validate_filename(cls, v: str) -> Union[str, None]:
        if v is None:
            return v
        elif re.match(r"^a_", v):
            return v
        else:
            raise ValueError("'filename' should start with 'a_'")

    @field_validator("comments")
    def detect_target_repo_comments(cls, v: List[Comment]) -> Optional[List[Comment]]:
        target_repo_comments = [
            comment for comment in v if comment.name == TARGET_REPO_KEY
        ]
        if len(target_repo_comments) == 0:
            raise ValueError(f"'{TARGET_REPO_KEY}' comment is missing")
        elif len(target_repo_comments) > 1:
            raise ValueError(f"Multiple '{TARGET_REPO_KEY}' comments found")
        else:
            if target_repo_comments[0].value in [
                item.value for item in TargetRepository
            ]:
                return v
            else:
                raise ValueError(
                    f"Invalid '{TARGET_REPO_KEY}' value: '{target_repo_comments[0].value}'"
                )

Pydantic is not able to export these contstraints to json schema:

{
  "additionalProperties": false,
  "properties": {
    "comments": {
      "default": [],
      "items": {
        "$ref": "#/$defs/Comment"
      },
      "title": "Comments",
      "type": "array"
    },
    "@id": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "@Id"
    },
    "characteristicCategories": {
      "default": [],
      "items": {
        "$ref": "#/$defs/MaterialAttribute"
      },
      "title": "Characteristiccategories",
      "type": "array"
    },
    "dataFiles": {
      "default": [],
      "items": {
        "$ref": "#/$defs/Data"
      },
      "title": "Datafiles",
      "type": "array"
    },
    "filename": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Filename"
    },
    "materials": {
      "anyOf": [
        {
          "$ref": "#/$defs/AssayMaterialType"
        },
        {
          "type": "null"
        }
      ],
      "default": null
    },
    "measurementType": {
      "anyOf": [
        {
          "$ref": "#/$defs/OntologyAnnotation"
        },
        {
          "type": "null"
        }
      ],
      "default": null
    },
    "processSequence": {
      "default": [],
      "items": {
        "$ref": "#/$defs/Process"
      },
      "title": "Processsequence",
      "type": "array"
    },
    "technologyPlatform": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Technologyplatform"
    },
    "technologyType": {
      "anyOf": [
        {
          "$ref": "#/$defs/OntologyAnnotation"
        },
        {
          "type": "null"
        }
      ],
      "default": null
    },
    "unitCategories": {
      "default": [],
      "items": {
        "$ref": "#/$defs/OntologyAnnotation"
      },
      "title": "Unitcategories",
      "type": "array"
    }
  },
  "title": "Assay",
  "type": "object"
}

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationgood first issueGood for newcomers

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions