question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support jsonschema's const keyword.

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
tiangolocommented, Mar 26, 2019

Sounds good!

I can imagine something like:

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

1reaction
Qazzquimbycommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generic keywords — Understanding JSON Schema 2020-12 ...
The const keyword is used to restrict a value to a single value. For example, if you only support shipping to the United...
Read more >
Supported JSON Schema Keywords - Swagger
Supported JSON Schema Keywords. OpenAPI 3.0 uses an extended subset of JSON Schema Specification Wright Draft 00 (aka Draft 5) to describe the...
Read more >
JSON schema: Why does "constant" not validate the same ...
I started off using a constant in my sub-schemas (inside the oneOf , but that was saying that all the the sub-schemas validated...
Read more >
draft 2020-12 - Ajv JSON schema validator
Ajv supports all new keywords of JSON Schema draft-2019-09: ... If you use enum or const keywords, "nullable": true would not extend the...
Read more >
Generic keywords - Opis JSON Schema
The type , const , and enum generic keywords, allow you to validate JSON data, by checking if its value, or its type,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found