Skip to content

Commit

Permalink
add nativeint, bytes, ref, unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Khady committed Sep 22, 2024
1 parent ae7afda commit 8de35f8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ Such a type will be turned into a JSON schema like this:

#### Basic types

Types `int`, `int32`, `int64`, `string`, `float`, `bool` are converted to their JSON equivalents.
Types `int`, `int32`, `int64`, `nativeint`, `string`, `bytes`, `float`, `bool` are converted to their JSON equivalents.

Type `char` is converted to `{ "type": "string", "minLength": 1, "maxLength": 1}`.

Type `'a ref` is treated as `'a`.

Type `unit` is converted to `{ "type": "null" }`.

#### List and arrays

OCaml lists and arrays are converted to `{ "type": "array", "items": { "type": "..." } }`.
Expand Down
8 changes: 6 additions & 2 deletions src/ppx_deriving_jsonschema.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ let type_ref ~loc type_name =

let type_def ~loc type_name = [%expr `Assoc [ "type", `String [%e estring ~loc type_name] ]]

let null ~loc = [%expr `Assoc [ "type", `String "null" ]]

let char ~loc = [%expr `Assoc [ "type", `String "string"; "minLength", `Int 1; "maxLength", `Int 1 ]]

let enum ~loc values =
Expand All @@ -67,12 +69,14 @@ let is_optional_type core_type =

let rec type_of_core ~loc core_type =
match core_type with
| [%type: int] | [%type: int32] | [%type: int64] -> type_def ~loc "integer"
| [%type: int] | [%type: int32] | [%type: int64] | [%type: nativeint] -> type_def ~loc "integer"
| [%type: float] -> type_def ~loc "number"
| [%type: string] -> type_def ~loc "string"
| [%type: string] | [%type: bytes] -> type_def ~loc "string"
| [%type: bool] -> type_def ~loc "boolean"
| [%type: char] -> char ~loc
| [%type: unit] -> null ~loc
| [%type: [%t? t] option] -> type_of_core ~loc t
| [%type: [%t? t] ref] -> type_of_core ~loc t
| [%type: [%t? t] list] | [%type: [%t? t] array] ->
let t = type_of_core ~loc t in
array_ ~loc t
Expand Down
26 changes: 19 additions & 7 deletions test/test.expected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,27 @@ type event =
a: float array ;
l: string list ;
t: [ `Foo | `Bar | `Baz ] ;
c: char }[@@deriving jsonschema]
c: char ;
bunch_of_bytes: bytes ;
string_ref: string ref ;
unit: unit ;
native_int: nativeint }[@@deriving jsonschema]
include
struct
let event_jsonschema =
`Assoc
[("type", (`String "object"));
("properties",
(`Assoc
[("c",
(`Assoc
[("type", (`String "string"));
("minLength", (`Int 1));
("maxLength", (`Int 1))]));
[("native_int", (`Assoc [("type", (`String "integer"))]));
("unit", (`Assoc [("type", (`String "null"))]));
("string_ref", (`Assoc [("type", (`String "string"))]));
("bunch_of_bytes", (`Assoc [("type", (`String "string"))]));
("c",
(`Assoc
[("type", (`String "string"));
("minLength", (`Int 1));
("maxLength", (`Int 1))]));
("t",
(`Assoc
[("type", (`String "string"));
Expand All @@ -125,7 +133,11 @@ include
("date", (`Assoc [("type", (`String "number"))]))]));
("required",
(`List
[`String "c";
[`String "native_int";
`String "unit";
`String "string_ref";
`String "bunch_of_bytes";
`String "c";
`String "t";
`String "l";
`String "a";
Expand Down
4 changes: 4 additions & 0 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type event = {
l : string list;
t : [ `Foo | `Bar | `Baz ];
c : char;
bunch_of_bytes : bytes;
string_ref : string ref;
unit : unit;
native_int : nativeint;
}
[@@deriving jsonschema]

Expand Down
63 changes: 56 additions & 7 deletions test/test_schemas.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"native_int": { "type": "integer" },
"unit": { "type": "null" },
"string_ref": { "type": "string" },
"bunch_of_bytes": { "type": "string" },
"c": { "type": "string", "minLength": 1, "maxLength": 1 },
"t": { "type": "string", "enum": [ "Foo", "Bar", "Baz" ] },
"l": { "type": "array", "items": { "type": "string" } },
Expand All @@ -38,14 +42,21 @@
"kind_f": { "type": "string", "enum": [ "Success", "Error", "skipped" ] },
"date": { "type": "number" }
},
"required": [ "c", "t", "l", "a", "comment", "kind_f", "date" ]
"required": [
"native_int", "unit", "string_ref", "bunch_of_bytes", "c", "t", "l", "a",
"comment", "kind_f", "date"
]
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"items": {
"type": "object",
"properties": {
"native_int": { "type": "integer" },
"unit": { "type": "null" },
"string_ref": { "type": "string" },
"bunch_of_bytes": { "type": "string" },
"c": { "type": "string", "minLength": 1, "maxLength": 1 },
"t": { "type": "string", "enum": [ "Foo", "Bar", "Baz" ] },
"l": { "type": "array", "items": { "type": "string" } },
Expand All @@ -58,7 +69,10 @@
},
"date": { "type": "number" }
},
"required": [ "c", "t", "l", "a", "comment", "kind_f", "date" ]
"required": [
"native_int", "unit", "string_ref", "bunch_of_bytes", "c", "t", "l",
"a", "comment", "kind_f", "date"
]
}
}
{
Expand All @@ -69,6 +83,10 @@
"items": {
"type": "object",
"properties": {
"native_int": { "type": "integer" },
"unit": { "type": "null" },
"string_ref": { "type": "string" },
"bunch_of_bytes": { "type": "string" },
"c": { "type": "string", "minLength": 1, "maxLength": 1 },
"t": { "type": "string", "enum": [ "Foo", "Bar", "Baz" ] },
"l": { "type": "array", "items": { "type": "string" } },
Expand All @@ -81,7 +99,10 @@
},
"date": { "type": "number" }
},
"required": [ "c", "t", "l", "a", "comment", "kind_f", "date" ]
"required": [
"native_int", "unit", "string_ref", "bunch_of_bytes", "c", "t", "l",
"a", "comment", "kind_f", "date"
]
}
}
}
Expand All @@ -92,6 +113,10 @@
{
"type": "object",
"properties": {
"native_int": { "type": "integer" },
"unit": { "type": "null" },
"string_ref": { "type": "string" },
"bunch_of_bytes": { "type": "string" },
"c": { "type": "string", "minLength": 1, "maxLength": 1 },
"t": { "type": "string", "enum": [ "Foo", "Bar", "Baz" ] },
"l": { "type": "array", "items": { "type": "string" } },
Expand All @@ -104,7 +129,10 @@
},
"date": { "type": "number" }
},
"required": [ "c", "t", "l", "a", "comment", "kind_f", "date" ]
"required": [
"native_int", "unit", "string_ref", "bunch_of_bytes", "c", "t", "l",
"a", "comment", "kind_f", "date"
]
},
{ "type": "string" }
]
Expand All @@ -118,6 +146,10 @@
{
"type": "object",
"properties": {
"native_int": { "type": "integer" },
"unit": { "type": "null" },
"string_ref": { "type": "string" },
"bunch_of_bytes": { "type": "string" },
"c": { "type": "string", "minLength": 1, "maxLength": 1 },
"t": { "type": "string", "enum": [ "Foo", "Bar", "Baz" ] },
"l": { "type": "array", "items": { "type": "string" } },
Expand All @@ -130,7 +162,10 @@
},
"date": { "type": "number" }
},
"required": [ "c", "t", "l", "a", "comment", "kind_f", "date" ]
"required": [
"native_int", "unit", "string_ref", "bunch_of_bytes", "c", "t",
"l", "a", "comment", "kind_f", "date"
]
},
{ "type": "string" }
]
Expand All @@ -145,6 +180,10 @@
{
"type": "object",
"properties": {
"native_int": { "type": "integer" },
"unit": { "type": "null" },
"string_ref": { "type": "string" },
"bunch_of_bytes": { "type": "string" },
"c": { "type": "string", "minLength": 1, "maxLength": 1 },
"t": { "type": "string", "enum": [ "Foo", "Bar", "Baz" ] },
"l": { "type": "array", "items": { "type": "string" } },
Expand All @@ -157,7 +196,10 @@
},
"date": { "type": "number" }
},
"required": [ "c", "t", "l", "a", "comment", "kind_f", "date" ]
"required": [
"native_int", "unit", "string_ref", "bunch_of_bytes", "c", "t",
"l", "a", "comment", "kind_f", "date"
]
},
{ "type": "integer" }
]
Expand All @@ -171,6 +213,10 @@
"items": {
"type": "object",
"properties": {
"native_int": { "type": "integer" },
"unit": { "type": "null" },
"string_ref": { "type": "string" },
"bunch_of_bytes": { "type": "string" },
"c": { "type": "string", "minLength": 1, "maxLength": 1 },
"t": { "type": "string", "enum": [ "Foo", "Bar", "Baz" ] },
"l": { "type": "array", "items": { "type": "string" } },
Expand All @@ -183,7 +229,10 @@
},
"date": { "type": "number" }
},
"required": [ "c", "t", "l", "a", "comment", "kind_f", "date" ]
"required": [
"native_int", "unit", "string_ref", "bunch_of_bytes", "c", "t", "l",
"a", "comment", "kind_f", "date"
]
}
}
}
Expand Down

0 comments on commit 8de35f8

Please sign in to comment.