Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support jsonschema's const keyword. #434

Closed
Sean1708 opened this issue Mar 26, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@Sean1708
Copy link
Contributor

commented Mar 26, 2019

Feature Request

https://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1.3

This is a very useful feature for when structural subtyping just doesn't quite cut it and you need to differentiate two semantically different but structurally identical types.

Some ways that this could be implemented:

class Foo(pydantic.BaseModel):
    bar: pydantic.Constant = "baz"
class Foo(pydantic.BaseModel):
    bar: pydantic.constant("baz")

Which would each generate the following schema:

{
    "title": "Foo",
    "type": "object",
    "properties": {
        "bar": {
            "title": "Bar",
            "const": "baz"
        }
    },
    "required": ["bar"]
}

I'm happy to put some work in for this, if it's something that you're willing to support.

@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Mar 26, 2019

Happy to support it, but better to use the Schema(...) class than misusing types.

@tiangolo you did all the JSON schema work, any thoughts?

@tiangolo

This comment has been minimized.

Copy link
Collaborator

commented Mar 26, 2019

Sounds good!

I can imagine something like:

class Foo(BaseModel):
    bar: str = Schema("baz", const=True)
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Mar 26, 2019

makes lots of sense.

Sean1708 added a commit to Sean1708/pydantic that referenced this issue Apr 8, 2019

Sean1708 added a commit to Sean1708/pydantic that referenced this issue Apr 8, 2019

Sean1708 added a commit to Sean1708/pydantic that referenced this issue Apr 9, 2019

Sean1708 added a commit to Sean1708/pydantic that referenced this issue Apr 9, 2019

Sean1708 added a commit to Sean1708/pydantic that referenced this issue Apr 9, 2019

@Sean1708 Sean1708 referenced this issue Apr 9, 2019

Merged

Implement const keyword in Schema. #469

4 of 4 tasks complete
@Sean1708

This comment has been minimized.

Copy link
Contributor Author

commented Apr 9, 2019

BTW, I found the time to have a stab at this yesterday. I'm still not particularly familiar with the code base as a whole, so I might have gone about it in completely the wrong way, but it seems to work at the very least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.