[Python] Jsonschema, validating object keys with a custom function

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am using python-jsonschema for json validation. I have an object with localised texts that are specified inside rfc1766 language code keys as followings:

    "Description": {
    "en": "English Description",
    "sv": "Swedish Description",
    "fr": "French Description"
    },


    I've read in the documentation that I could use the 'format' attribute to check a custom format using a function. So, I wrote a method which takes a string as a parameter and returns True if it is an RFC1766 language string.

    @_checks_drafts('rfc1766lang')
    def rfc1766lang(instance):
    """some logic, return True if rfc1766"""


    However I couldn't find any example on how to apply this to do validation on an object key, not a value.

    Is this possible?

    I have tried something like below but I couldn't succeed

    rfc1766_string_schema_v2 = {
    'type': 'object',
    'format': 'rfc1766lang',
    'additionalProperties': False
    }


    I know that it would be much easier if I had the json string as follows. However, this is not an option for now.

    "Description": [{
    "lan": "en",
    "text": "Description in English"
    }, {
    "lan": "sv",
    "name": "Description in Swedish"
    }]

    Continue reading...

Compartilhe esta Página