multiple checker functions for a given format

Offen
#1,496 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 6 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
35/100
Issue-Typ
Feature
Klarheit
Muss geklärt werden
Aktivitätsstatus
Ruhig
Tech-Stack
python
Bereich
tooling

Rechercherichtung

Beginne mit dem Lesen der FormatChecker.checks-API und der im Issue gezeigten bestehenden date-time-Checker-Registrierung. Verfolge, wie die Registrierung eines Checkers die aktuelle Funktion ersetzt, und bestimme anschließend das beabsichtigte Verhalten für die Kombination benutzerdefinierter und integrierter Checks sowie, wie die Abwärtskompatibilität erhalten bleiben soll; als abgeschlossen gilt die Aufgabe, wenn das Verhalten vereinbart und durch die Checker-Tests des Projekts abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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?

Vorherrschende Sprache
Python
Sterne
5k
Forks
741
Ø Merge
4 T. 17 Std.
Gemergte PRs (30 T.)
6

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python-jsonschema/jsonschema

Alle Issues in python-jsonschema/jsonschema

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.