Skip to content

Commit 51da367

Browse files
committed
feat: extend PDL types with json schema types
Signed-off-by: Louis Mandel <[email protected]>
1 parent bcfabe8 commit 51da367

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

pdl-live-react/src/pdl_ast.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type PdlTypeType =
4949
| ListPdlType
5050
| PdlTypeType[]
5151
| OptionalPdlType
52+
| JsonSchemaTypePdlType
5253
| ObjPdlType
5354
| {
5455
[k: string]: PdlTypeType
@@ -69,6 +70,7 @@ export type Exclusivemaximum1 = number | null
6970
export type List = PdlTypeType | ListPdlTypeConstraints
7071
export type Minitems = number | null
7172
export type Maxitems = number | null
73+
export type Type = string | string[]
7274
export type Obj = {
7375
[k: string]: PdlTypeType
7476
} | null
@@ -2779,6 +2781,13 @@ export interface ListPdlTypeConstraints {
27792781
export interface OptionalPdlType {
27802782
optional: PdlTypeType
27812783
}
2784+
/**
2785+
* Json Schema type
2786+
*/
2787+
export interface JsonSchemaTypePdlType {
2788+
type: Type
2789+
[k: string]: unknown
2790+
}
27822791
/**
27832792
* Optional type.
27842793
*/

src/pdl/pdl-schema.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6116,6 +6116,31 @@
61166116
"title": "JoinText",
61176117
"type": "object"
61186118
},
6119+
"JsonSchemaTypePdlType": {
6120+
"additionalProperties": true,
6121+
"description": "Json Schema type",
6122+
"properties": {
6123+
"type": {
6124+
"anyOf": [
6125+
{
6126+
"type": "string"
6127+
},
6128+
{
6129+
"items": {
6130+
"type": "string"
6131+
},
6132+
"type": "array"
6133+
}
6134+
],
6135+
"title": "Type"
6136+
}
6137+
},
6138+
"required": [
6139+
"type"
6140+
],
6141+
"title": "JsonSchemaTypePdlType",
6142+
"type": "object"
6143+
},
61196144
"LastOfBlock": {
61206145
"additionalProperties": false,
61216146
"description": "Return the value of the last block if the list of blocks.",
@@ -9624,6 +9649,9 @@
96249649
{
96259650
"$ref": "#/$defs/OptionalPdlType"
96269651
},
9652+
{
9653+
"$ref": "#/$defs/JsonSchemaTypePdlType"
9654+
},
96279655
{
96289656
"$ref": "#/$defs/ObjPdlType"
96299657
},

src/pdl/pdl_ast.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ class OptionalPdlType(PdlType):
228228
optional: "PdlTypeType"
229229

230230

231+
class JsonSchemaTypePdlType(PdlType):
232+
"""Json Schema type"""
233+
234+
model_config = ConfigDict(extra="allow")
235+
type: str | list[str]
236+
237+
231238
class ObjPdlType(PdlType):
232239
"""Optional type."""
233240

@@ -245,6 +252,7 @@ class ObjPdlType(PdlType):
245252
" ListPdlType,"
246253
" list['PdlTypeType'],"
247254
" OptionalPdlType,"
255+
" JsonSchemaTypePdlType,"
248256
" ObjPdlType,"
249257
" dict[str, 'PdlTypeType']]",
250258
Field(union_mode="left_to_right"),

src/pdl/pdl_dumper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
IntPdlType,
3333
JoinText,
3434
JoinType,
35+
JsonSchemaTypePdlType,
3536
LastOfBlock,
3637
ListPdlType,
3738
ListPdlTypeConstraints,
@@ -377,6 +378,8 @@ def type_to_dict(t: PdlTypeType):
377378
assert False, "list must have only one element"
378379
case OptionalPdlType():
379380
d = {"optional": type_to_dict(t.optional)}
381+
case JsonSchemaTypePdlType():
382+
d = t.model_dump()
380383
case ObjPdlType():
381384
if t.obj is None:
382385
d = "obj"

src/pdl/pdl_schema_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
EnumPdlType,
66
FloatPdlType,
77
IntPdlType,
8+
JsonSchemaTypePdlType,
89
ListPdlType,
910
ListPdlTypeConstraints,
1011
ObjPdlType,
@@ -111,6 +112,12 @@ def pdltype_to_jsonschema(
111112
case OptionalPdlType(optional=t):
112113
t_schema = pdltype_to_jsonschema(t, additional_properties)
113114
schema = {"anyOf": [t_schema, "null"]}
115+
case JsonSchemaTypePdlType(type=t) as s:
116+
if pdl_type.__pydantic_extra__ is None:
117+
extra = {}
118+
else:
119+
extra = pdl_type.__pydantic_extra__
120+
schema = {"type": t, **extra}
114121
case ObjPdlType(obj=pdl_props):
115122
if pdl_props is None:
116123
schema = {"type": "object"}

tests/test_type_checking.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@
140140
"pdl_type": "{enum: [red, green, blue]}",
141141
"json_schema": {"enum": ["red", "green", "blue"]},
142142
},
143+
{
144+
"pdl_type": "{ type: string }",
145+
"json_schema": {"type": "string"},
146+
},
147+
{
148+
"pdl_type": "{ type: [number, string] }",
149+
"json_schema": {"type": ["number", "string"]},
150+
},
151+
{
152+
"pdl_type": "{ type: array, prefixItems: [ { type: number }, { type: string }, { enum: [Street, Avenue, Boulevard] }, { enum: [NW, NE, SW, SE] } ]}",
153+
"json_schema": {
154+
"type": "array",
155+
"prefixItems": [
156+
{"type": "number"},
157+
{"type": "string"},
158+
{"enum": ["Street", "Avenue", "Boulevard"]},
159+
{"enum": ["NW", "NE", "SW", "SE"]},
160+
],
161+
},
162+
},
143163
]
144164

145165

0 commit comments

Comments
 (0)