Skip to content

Mermeid-Designs/pydantic-hooks

Repository files navigation

pydantic-pre-commit-hooks

Pydantic v1 Pydantic v2 versions license

Hooks which handle the exportation of Pydantic models.

Hook ID Description
pydantic-to-json Convert instanciated pydantic models to their corresponding JSON representation
pydantic-to-schema Convert pydantic model declarations to corresponding JSON schemas

For example ...

from pydantic import BaseModel

class ExampleClass(BaseModel):
  x: int

example_instance = ExampleClass(x=2)

Would export to ...

example_instance.json
(pydantic-to-json)
ExampleClass.json
(pydantic-to-schema)
{
  "x": 2
}
{
  "properties": {
    "x": {
      "title": "X",
      "type": "integer"
    }
  },
  "required": [
    "x"
  ],
  "title": "ExampleClass",
  "type": "object"
}

pydantic-to-json

Pydantic v1 Pydantic v2 versions license

A pre-commit hook to convert instanciated pydantic classes to their respective JSON information dumps.

By default, only the Pydantic models located within changed files are exported;
this behavior is overridable using both/either input and all

Minimal usage:

repos:
  - repo: https://github.com/Mermeid-Designs/pre-commit-hooks
    rev: ""  # can specify specific revision if needed
    hooks:
    - id: pydantic-to-json
      args:
        [
          "--output",
          "path/to/output/folder",
        ]

Parameters:

  • output ($\color{red}Required$): str, path to the desired folder where you would like the exported JSON dictionaries to be stored. If the folder does not exist, it will be created. Any existing JSONs will be overwritten, if applicable.
  • input ($Optional$): str, absolute or relative path to a Python package, sub-package, or module that contains the Pydantic model class declaration(s) you wish to export. The input may reference files outside the scope of the repo invoking this pre-commit call.
  • all ($Optional$): flag denoting all Pydantic model class declaration(s) within input shall be exported, (not just ones within changed files).

Output: Naming convention is as follows {YourClassName}.json

📌 NOTE:

  • ALL Pydantic model class declaration(s) within input can potentially be exported, regardless of if they are normally visible to input (e.g. private class declarations)
  • --all is necessary if input references files outside the scope of the repo invoking this pre-commit call

pydantic-to-schema

Pydantic v1 Pydantic v2 versions license

A pre-commit hook to convert pydantic class declarations to their respective JSON schemas

By default, only the Pydantic models located within changed files are exported;
this behavior is overridable using both/either input and all

Minimal usage:

repos:
  - repo: https://github.com/Mermeid-Designs/pre-commit-hooks
    rev: ""  # can specify specific revision if needed
    hooks:
    - id: pydantic-to-schema
      args:
        [
          "--output",
          "path/to/output/folder",
        ]

Parameters:

  • output ($\color{red}Required$): str, path to the desired folder where you would like the exported JSON dictionaries to be stored. If the folder does not exist, it will be created. Any existing JSONs will be overwritten, if applicable.
  • input ($Optional$): str, absolute or relative path to a Python package, sub-package, or module that contains the Pydantic model class declaration(s) you wish to export. The input may reference files outside the scope of the repo invoking this pre-commit call.
  • all ($Optional$): flag denoting all Pydantic model class declaration(s) within input shall be exported, (not just ones within changed files).

Output: Naming convention is as follows {YourClassName}.json

📌 NOTE:

  • ALL Pydantic model class declaration(s) within input can potentially be exported, regardless of if they are normally visible to input (e.g. private class declarations)
  • --all is necessary if input references files outside the scope of the repo invoking this pre-commit call

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages