Skip to content

Python enum validation #316

@blacha

Description

@blacha

Hi @Stranger6667, Another issue that I'm running into is with Python enum's

Given the following piece of code by default the Enum is not converted to a string

from jsonschema_rs import is_valid
import orjson

from enum import Enum

class Foo(Enum):
   bar = "bar"
   foo = "foo"

schema = {"properties": {"foo": {"type": "string"}}}
instance = {"foo": Foo.bar}

converted = orjson.loads(orjson.dumps(instance))
print(is_valid(schema, converted)) # True

print(is_valid(schema, instance)) # ValueError: Unsupported type: 'Foo'

I am thinking of two options and wondering which you would prefer

  1. Automatically converting Enums to their corresponding type (str, int, etc) like how orjson does it
  2. Creating a string based interface validate_str(orjson.dumps(instance))

I like the simplicity of 1 for the end user as they don't have to worry about converting their enum's with Foo.bar.value before validating.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions