Support jsonschema's const keyword.
See original GitHub issueFeature 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:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sounds good!
I can imagine something like:
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.