multiple checker functions for a given format

未關閉
#1,496 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 6 天內回覆

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
35/100
Issue 類型
功能
描述清晰度
需要釐清
活躍度
冷清
技術堆疊
python
領域
tooling

研究方向

先閱讀 FormatChecker.checks API,以及 issue 中顯示的現有 date-time checker 註冊方式。追蹤註冊 checker 如何取代目前的函式,然後確定結合自訂檢查與內建檢查時的預期行為,以及如何維持向後相容性;當行為達成共識並由專案的 checker 測試涵蓋時,即表示完成。

由索引模型根據 Issue 內容生成。

描述

I think this is more of a discussion rather than a bug.

I've looked in multiple places and did not find an agreed-upon common solution to what seems like quite a trivial problem:

I'd like to extend a built-in format checkers with an additional, application-related, logic. It seems that the only thing I can do at the moment is to register a custom format checking function which overwrites the default checks:


import jsonschema as js

formatter = js.FormatChecker()

@formatter.checks("date-time")
def my_date_time(instance):
  # custom logic verifying whether the string field tagged as `format: date-time` is of this format
  return True

However this is unwanted as I just want to extend the existing checks with my logic, not to rewrite it fully. I want to e.g. additionally validate that the date-time is not too stale. The only workaround I've come up with is sth like this:

import yaml, json
import jsonschema as js
from datetime import datetime as dt

formatter = js.FormatChecker()
date_time_default_check = formatter.checkers["date-time"][0]

@formatter.checks("date-time")
def my_date_time(instance):
  if not date_time_default_check(instance):
    return False
  now = dt.now()
  if (now - dt.fromisoformat(instance)).days > 365:
    return False
  return True

It seems to me that a really intuitive way to solve such problem should be the abillity to register additional custom functions to existing checks per format.
Meaning the formatter.checks functions should append new functions rather than replace them.
This can be made further optional in order not be backward incompatible.

The overall behaviour would be something close to how pydantic resolves it's validator methods.
Are there any obvious contraindications against such approach?

主要語言
Python
星號
5k
分支
741
平均合併
4 天 17 小時
30 天內合併 PR
6

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python-jsonschema/jsonschema 的其他 Issue

查看 python-jsonschema/jsonschema 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。