python-jsonschema / python-jsonschema/jsonschema

multiple checker functions for a given format

Open
#1,496 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5k
Forks
671
Avg merge
1d 1h
Merged PRs (30d)
10

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the FormatChecker.checks API and the existing date-time checker registration shown in the issue. Trace how registering a checker replaces the current function, then determine the intended behavior for combining custom and built-in checks and how backward compatibility would be preserved; done means the behavior is agreed and covered by the project's checker tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.