dandi / dandi-schema

Schemata for DANDI archive project
Apache License 2.0
5 stars 8 forks source link

Following example in documentation gives DeprecationWarning #239

Closed johanfrykebrant closed 2 months ago

johanfrykebrant commented 2 months ago

The documentation on how to how to implement custom type formats suggest doing something like this

import re
from jsonschema import draft4_format_checker

MONEY_RE = re.compile('^\$\s*\d+(\.\d\d)?')

@draft4_format_checker.checks('money')
def is_money(val):
    if not isinstance(val, str):
        return True
    return MONEY_RE.match(val)

But doing that results in the following error DeprecationWarning: Accessing jsonschema.draft4_format_checker is deprecated and will be removed in a future release. Instead, use the FORMAT_CHECKER attribute on the corresponding Validator.

I tried to solve it by doing like this instead

import re
from jsonschema import draft4_format_checker

MONEY_RE = re.compile('^\$\s*\d+(\.\d\d)?')

@Draft7Validator.FORMAT_CHECKER.checks('money')
def is_money(val):
    if not isinstance(val, str):
        return True
    return MONEY_RE.match(val)

This seems to be the wrong way of doing it since the custom type is not being validated at all. If I can get some assistance on how to solve this correctly I'd be happy to create a PR altering the documentation as needed.

johanfrykebrant commented 2 months ago

Sorry, disreagard this Issue. Not meant for this repo.

yarikoptic commented 2 months ago

no problem

FTR: search across organization for that function has no hits for us -- https://github.com/search?q=org%3Adandi%20draft4_format_checker&type=code