Skip to content

feat: extend PDL types with json schema types #947

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

Merged
merged 3 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pdl-live-react/src/pdl_ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type PdlTypeType =
| ListPdlType
| PdlTypeType[]
| OptionalPdlType
| JsonSchemaTypePdlType
| ObjPdlType
| {
[k: string]: PdlTypeType
Expand All @@ -69,6 +70,7 @@ export type Exclusivemaximum1 = number | null
export type List = PdlTypeType | ListPdlTypeConstraints
export type Minitems = number | null
export type Maxitems = number | null
export type Type = string | string[]
export type Obj = {
[k: string]: PdlTypeType
} | null
Expand Down Expand Up @@ -2991,6 +2993,13 @@ export interface ListPdlTypeConstraints {
export interface OptionalPdlType {
optional: PdlTypeType
}
/**
* Json Schema type
*/
export interface JsonSchemaTypePdlType {
type: Type
[k: string]: unknown
}
/**
* Optional type.
*/
Expand Down
28 changes: 28 additions & 0 deletions src/pdl/pdl-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6493,6 +6493,31 @@
"title": "JoinText",
"type": "object"
},
"JsonSchemaTypePdlType": {
"additionalProperties": true,
"description": "Json Schema type",
"properties": {
"type": {
"anyOf": [
{
"type": "string"
},
{
"items": {
"type": "string"
},
"type": "array"
}
],
"title": "Type"
}
},
"required": [
"type"
],
"title": "JsonSchemaTypePdlType",
"type": "object"
},
"LastOfBlock": {
"additionalProperties": false,
"description": "Return the value of the last block if the list of blocks.",
Expand Down Expand Up @@ -10146,6 +10171,9 @@
{
"$ref": "#/$defs/OptionalPdlType"
},
{
"$ref": "#/$defs/JsonSchemaTypePdlType"
},
{
"$ref": "#/$defs/ObjPdlType"
},
Expand Down
8 changes: 8 additions & 0 deletions src/pdl/pdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ class OptionalPdlType(PdlType):
optional: "PdlTypeType"


class JsonSchemaTypePdlType(PdlType):
"""Json Schema type"""

model_config = ConfigDict(extra="allow")
type: str | list[str]


class ObjPdlType(PdlType):
"""Optional type."""

Expand All @@ -245,6 +252,7 @@ class ObjPdlType(PdlType):
" ListPdlType,"
" list['PdlTypeType'],"
" OptionalPdlType,"
" JsonSchemaTypePdlType,"
" ObjPdlType,"
" dict[str, 'PdlTypeType']]",
Field(union_mode="left_to_right"),
Expand Down
3 changes: 3 additions & 0 deletions src/pdl/pdl_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
IntPdlType,
JoinText,
JoinType,
JsonSchemaTypePdlType,
LastOfBlock,
ListPdlType,
ListPdlTypeConstraints,
Expand Down Expand Up @@ -383,6 +384,8 @@ def type_to_dict(t: PdlTypeType):
assert False, "list must have only one element"
case OptionalPdlType():
d = {"optional": type_to_dict(t.optional)}
case JsonSchemaTypePdlType():
d = t.model_dump()
case ObjPdlType():
if t.obj is None:
d = "obj"
Expand Down
7 changes: 7 additions & 0 deletions src/pdl/pdl_schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
EnumPdlType,
FloatPdlType,
IntPdlType,
JsonSchemaTypePdlType,
ListPdlType,
ListPdlTypeConstraints,
ObjPdlType,
Expand Down Expand Up @@ -111,6 +112,12 @@ def pdltype_to_jsonschema(
case OptionalPdlType(optional=t):
t_schema = pdltype_to_jsonschema(t, additional_properties)
schema = {"anyOf": [t_schema, "null"]}
case JsonSchemaTypePdlType(type=t):
if pdl_type.__pydantic_extra__ is None:
extra = {}
else:
extra = pdl_type.__pydantic_extra__
schema = {"type": t, **extra}
case ObjPdlType(obj=pdl_props):
if pdl_props is None:
schema = {"type": "object"}
Expand Down
20 changes: 20 additions & 0 deletions tests/test_type_checking.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@
"pdl_type": "{enum: [red, green, blue]}",
"json_schema": {"enum": ["red", "green", "blue"]},
},
{
"pdl_type": "{ type: string }",
"json_schema": {"type": "string"},
},
{
"pdl_type": "{ type: [number, string] }",
"json_schema": {"type": ["number", "string"]},
},
{
"pdl_type": "{ type: array, prefixItems: [ { type: number }, { type: string }, { enum: [Street, Avenue, Boulevard] }, { enum: [NW, NE, SW, SE] } ]}",
"json_schema": {
"type": "array",
"prefixItems": [
{"type": "number"},
{"type": "string"},
{"enum": ["Street", "Avenue", "Boulevard"]},
{"enum": ["NW", "NE", "SW", "SE"]},
],
},
},
]


Expand Down