diff --git a/lib/generator.ml b/lib/generator.ml index fe0fa44..4c54ad6 100644 --- a/lib/generator.ml +++ b/lib/generator.ml @@ -141,7 +141,16 @@ let rec process_schema_type ~ancestors (schema : schema) = | Some schemas -> process_one_of ~ancestors schemas | None -> match schema.enum, schema.typ with - | Some enums, Some String -> process_enums enums + | Some enums, Some String -> process_string_enums enums + | Some _, Some Integer -> + (* this is more lenient than it should *) + maybe_nullable (process_int_type schema) + | Some _, Some Number -> + (* this is more lenient than it should *) + maybe_nullable "float" + | Some _, Some Boolean -> + (* this is more lenient than it should *) + maybe_nullable "bool" | Some _, _ -> failwith "only string enums are supported" | None, _ -> match schema.typ with @@ -217,7 +226,19 @@ and process_one_of ~ancestors (schemas_or_refs : schema or_ref list) = let variants = List.map make_one_of_variant schemas_or_refs |> String.concat "\n" in sprintf "[\n%s\n] " variants -and process_enums enums = +and process_string_enums enums = + let enums = + List.map + (function + | `String s -> s + | value -> + failwith + (sprintf "Invalid value %s in string enum %s" (Yojson.Basic.to_string value) + (Yojson.Basic.to_string (`List enums)) + ) + ) + enums + in let make_enum_variant value = sprintf {| | %s |} (variant_name value) value in let variants = List.map make_enum_variant enums |> String.concat "\n" in sprintf "[\n%s\n]" variants diff --git a/lib/json_schema.atd b/lib/json_schema.atd index 1ddd5bf..4df176f 100644 --- a/lib/json_schema.atd +++ b/lib/json_schema.atd @@ -60,7 +60,7 @@ type schema = { (* 6.1 validation for any instance type *) ~typ : typ nullable; - ~enum : string nonempty_list nullable; + ~enum : json nonempty_list nullable; (* 6.2 validation for numeric instances *) (* ~multiple_of : float nullable; *) diff --git a/tests/mocks/jsonschema_enums.json b/tests/mocks/jsonschema_enums.json new file mode 100644 index 0000000..830768d --- /dev/null +++ b/tests/mocks/jsonschema_enums.json @@ -0,0 +1,36 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema", + "type": "object", + "properties": { + "myProperty": { + "type": "string", + "enum": [ + "foo", + "bar" + ] + }, + "myPropertyInt": { + "type": "integer", + "enum": [ + 1, + 2 + ] + }, + "myPropertyNumber": { + "type": "number", + "enum": [ + 1.2, + 2.3 + ] + }, + "myPropertyBool": { + "type": "boolean", + "enum": [ + true + ] + } + }, + "required": [ + "myProperty" + ] +} diff --git a/tests/smoke.t b/tests/smoke.t index 2b60b36..772103a 100644 --- a/tests/smoke.t +++ b/tests/smoke.t @@ -88,3 +88,27 @@ Generate ATD out of JSON Schema that uses references ?ee : address option; ?ff : address option; } + +Generate ATD out of JSON Schema that uses enums + $ jsonschema2atd --format=jsonschema ./mocks/jsonschema_enums.json + (* Generated by jsonschema2atd *) + type json = abstract + type int64 = int + + type rootMyProperty = [ + | Foo + | Bar + ] + + type rootMyPropertyInt = int + + type rootMyPropertyNumber = float + + type rootMyPropertyBool = bool + + type root = { + myProperty : rootMyProperty; + ?myPropertyInt : rootMyPropertyInt option; + ?myPropertyNumber : rootMyPropertyNumber option; + ?myPropertyBool : rootMyPropertyBool option; + }