Skip to content

Commit 497aa1b

Browse files
authored
feat: extend PDL types with json schema types (#947)
Signed-off-by: Louis Mandel <[email protected]>
1 parent 47f4adc commit 497aa1b

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
@@ -2991,6 +2993,13 @@ export interface ListPdlTypeConstraints {
29912993
export interface OptionalPdlType {
29922994
optional: PdlTypeType
29932995
}
2996+
/**
2997+
* Json Schema type
2998+
*/
2999+
export interface JsonSchemaTypePdlType {
3000+
type: Type
3001+
[k: string]: unknown
3002+
}
29943003
/**
29953004
* Optional type.
29963005
*/

src/pdl/pdl-schema.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6493,6 +6493,31 @@
64936493
"title": "JoinText",
64946494
"type": "object"
64956495
},
6496+
"JsonSchemaTypePdlType": {
6497+
"additionalProperties": true,
6498+
"description": "Json Schema type",
6499+
"properties": {
6500+
"type": {
6501+
"anyOf": [
6502+
{
6503+
"type": "string"
6504+
},
6505+
{
6506+
"items": {
6507+
"type": "string"
6508+
},
6509+
"type": "array"
6510+
}
6511+
],
6512+
"title": "Type"
6513+
}
6514+
},
6515+
"required": [
6516+
"type"
6517+
],
6518+
"title": "JsonSchemaTypePdlType",
6519+
"type": "object"
6520+
},
64966521
"LastOfBlock": {
64976522
"additionalProperties": false,
64986523
"description": "Return the value of the last block if the list of blocks.",
@@ -10146,6 +10171,9 @@
1014610171
{
1014710172
"$ref": "#/$defs/OptionalPdlType"
1014810173
},
10174+
{
10175+
"$ref": "#/$defs/JsonSchemaTypePdlType"
10176+
},
1014910177
{
1015010178
"$ref": "#/$defs/ObjPdlType"
1015110179
},

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,
@@ -383,6 +384,8 @@ def type_to_dict(t: PdlTypeType):
383384
assert False, "list must have only one element"
384385
case OptionalPdlType():
385386
d = {"optional": type_to_dict(t.optional)}
387+
case JsonSchemaTypePdlType():
388+
d = t.model_dump()
386389
case ObjPdlType():
387390
if t.obj is None:
388391
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):
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)