使用 Cerberus 模式自动化文档编制

发布于 2025-01-13 00:58:29 字数 453 浏览 1 评论 0原文

我们广泛使用 cerberus 来验证 json 配置文件。因此,我们有各种各样的模式,它们定义了这些 json 文档的格式。

我们希望能够使用这些模式为可能需要创建这些 json 文件的用户自动生成一些文档。

目前,我们针对我们拥有的所有不同的配置格式保留单独的文档页面,但是,如果对代码进行的更改未被采纳,则这些文档有时可能会过时,需要更改文档,并且还需要在两个地方更新同一条信息。通过从模式自动生成,文档将始终是最新的并保证反映实际代码,并且可以避免在多个位置进行更新的需要。

我们还使用 Sphinx 根据 .py 文件和单独的 .md 文件中的文档字符串从我们的代码库自动生成文档。

感觉使用 Cerberus 模式文件为每个模式创建“人类可读”文档文件应该相当简单,但我在 Cerberus 或 Sphinx 中找不到任何相关文档。有谁知道这些包中是否有一些本机功能可以实现我所缺少的功能,或者实现此目的的另一种方法或示例?

We are using cerberus extensively to validate json configuration files. We therefore have a wide range of schemas, which define how these json documents should be formatted.

We would like to be able to use these schemas to auto-generate some documentation for the users who may need to create these json files.

At the moment, we keep separate pages of documentation around all of the different config formats that we have, however these can sometimes drift out of date if a change in made to the code that is not picked up that it requires a change to the documentation, and also requires the same piece of information to be updated in two places. By auto-generating from the schemas, the docs would always be up to date and guaranteed to reflect the actual code, and it would avoid the need to update in multiple places.

We are also using Sphinx to auto generate docs from our codebase, based on Docstrings in out .py files, and separate .md files.

It feels that using the Cerberus schema files to create a "human readable" documentation file for each schema should be reasonably straight forward, but I have not been able to find any documentation for this within either Cerberus or Sphinx. Does anyone know if there is some native functionality in either of these packages that would achieve this that I am missing, or another way or example of achieving this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-01-20 00:58:29

我也有过类似的想法,不过还没有完全实现。首先,我所做的是使用自定义验证“描述”创建我自己的自定义验证器子类。此验证不执行任何操作,但允许向我的模式添加“描述”字段,我打算稍后将其用于文档目的。

class ConfigValidator(Validator):
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

def _validate_description(self, constraint:str, field:str, value:str) -> None:
    """
    Enables the use of the key 'description' in schemas in order to add descriptions to fields. Useful for automated documentation in the future. 
    Does no validation.
    
    Args:
        constraint (str): The name constraint applied.
        field (str): The name of the field.
        value (dict): The value of the field.

    The rule's arguments are validated against this schema:
    {'type': 'string'}
    """
    pass

I've had a similar idea, though I haven't fully implemented it yet. As a start what I've done is create my own custom validator subclass with a custom validation 'description'. This validation does nothing, but allows adding a 'description' field to my schemas, which I intend to use later for documentation purposes.

class ConfigValidator(Validator):
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

def _validate_description(self, constraint:str, field:str, value:str) -> None:
    """
    Enables the use of the key 'description' in schemas in order to add descriptions to fields. Useful for automated documentation in the future. 
    Does no validation.
    
    Args:
        constraint (str): The name constraint applied.
        field (str): The name of the field.
        value (dict): The value of the field.

    The rule's arguments are validated against this schema:
    {'type': 'string'}
    """
    pass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文