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 · 7 comments
Closed

Support jsonschema's const keyword. #434

Sean1708 opened this issue Mar 26, 2019 · 7 comments

Comments

@Sean1708
Copy link
Contributor

@Sean1708 Sean1708 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
Copy link
Owner

@samuelcolvin samuelcolvin 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
Copy link
Collaborator

@tiangolo tiangolo commented Mar 26, 2019

Sounds good!

I can imagine something like:

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

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin 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
Copy link
Contributor Author

@Sean1708 Sean1708 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.

@Qazzquimby
Copy link

@Qazzquimby Qazzquimby commented Feb 11, 2020

If I interpret right foo: str = Field('bar', const=True) results in bar being an optional property since it has a default. How could I make it required?

I have structurally identical types, and want a required name variable that is constant for each type.

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Feb 11, 2020

foo: str = Field(..., const=True)

This is in the docs, please read them first.

@Qazzquimby
Copy link

@Qazzquimby Qazzquimby commented Feb 11, 2020

That doesn't set the constant value to 'bar' as I had in my example, unless I misunderstand you.
foo: typing.Literal['bar'] seems to be what I was looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants