diff --git a/pdl-live-react/src/pdl_ast.d.ts b/pdl-live-react/src/pdl_ast.d.ts index cee63e81..7a47607b 100644 --- a/pdl-live-react/src/pdl_ast.d.ts +++ b/pdl-live-react/src/pdl_ast.d.ts @@ -49,6 +49,7 @@ export type PdlTypeType = | ListPdlType | PdlTypeType[] | OptionalPdlType + | JsonSchemaTypePdlType | ObjPdlType | { [k: string]: PdlTypeType @@ -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 @@ -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. */ diff --git a/src/pdl/pdl-schema.json b/src/pdl/pdl-schema.json index f2fffef5..300a0a6e 100644 --- a/src/pdl/pdl-schema.json +++ b/src/pdl/pdl-schema.json @@ -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.", @@ -10146,6 +10171,9 @@ { "$ref": "#/$defs/OptionalPdlType" }, + { + "$ref": "#/$defs/JsonSchemaTypePdlType" + }, { "$ref": "#/$defs/ObjPdlType" }, diff --git a/src/pdl/pdl_ast.py b/src/pdl/pdl_ast.py index 7b155ac5..9abcb24d 100644 --- a/src/pdl/pdl_ast.py +++ b/src/pdl/pdl_ast.py @@ -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.""" @@ -245,6 +252,7 @@ class ObjPdlType(PdlType): " ListPdlType," " list['PdlTypeType']," " OptionalPdlType," + " JsonSchemaTypePdlType," " ObjPdlType," " dict[str, 'PdlTypeType']]", Field(union_mode="left_to_right"), diff --git a/src/pdl/pdl_dumper.py b/src/pdl/pdl_dumper.py index dae3c126..912ae06e 100644 --- a/src/pdl/pdl_dumper.py +++ b/src/pdl/pdl_dumper.py @@ -32,6 +32,7 @@ IntPdlType, JoinText, JoinType, + JsonSchemaTypePdlType, LastOfBlock, ListPdlType, ListPdlTypeConstraints, @@ -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" diff --git a/src/pdl/pdl_schema_utils.py b/src/pdl/pdl_schema_utils.py index 402c38a7..78071c5e 100644 --- a/src/pdl/pdl_schema_utils.py +++ b/src/pdl/pdl_schema_utils.py @@ -5,6 +5,7 @@ EnumPdlType, FloatPdlType, IntPdlType, + JsonSchemaTypePdlType, ListPdlType, ListPdlTypeConstraints, ObjPdlType, @@ -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"} diff --git a/tests/test_type_checking.py b/tests/test_type_checking.py index b82e5823..23c24b75 100644 --- a/tests/test_type_checking.py +++ b/tests/test_type_checking.py @@ -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"]}, + ], + }, + }, ]