import Ajv from "ajv"; import { generateJsonSchema } from "./generateJsonSchema"; import { foxgloveMessageSchemas } from "./schemas"; import { exampleMessage } from "./testFixtures"; describe("generateJsonSchema", () => { it("generates expected JSON Schema", () => { expect(generateJsonSchema(exampleMessage)).toMatchInlineSnapshot(` { "$comment": "Generated by https://github.com/foxglove/schemas", "description": "An example type", "properties": { "field_boolean": { "description": "boolean field", "type": "boolean", }, "field_boolean_array": { "description": "boolean array field", "items": { "type": "boolean", }, "type": "array", }, "field_boolean_fixed_array": { "description": "boolean fixed-length array field", "items": { "type": "boolean", }, "maxItems": 3, "minItems": 3, "type": "array", }, "field_bytes": { "contentEncoding": "base64", "description": "bytes field", "type": "string", }, "field_bytes_array": { "description": "bytes array field", "items": { "contentEncoding": "base64", "type": "string", }, "type": "array", }, "field_bytes_fixed_array": { "description": "bytes fixed-length array field", "items": { "contentEncoding": "base64", "type": "string", }, "maxItems": 3, "minItems": 3, "type": "array", }, "field_duration": { "description": "duration field", "properties": { "nsec": { "maximum": 999999999, "minimum": 0, "type": "integer", }, "sec": { "type": "integer", }, }, "title": "duration", "type": "object", }, "field_duration_array": { "description": "duration array field", "items": { "properties": { "nsec": { "maximum": 999999999, "minimum": 0, "type": "integer", }, "sec": { "type": "integer", }, }, "title": "duration", "type": "object", }, "type": "array", }, "field_duration_fixed_array": { "description": "duration fixed-length array field", "items": { "properties": { "nsec": { "maximum": 999999999, "minimum": 0, "type": "integer", }, "sec": { "type": "integer", }, }, "title": "duration", "type": "object", }, "maxItems": 3, "minItems": 3, "type": "array", }, "field_enum": { "description": "An enum field", "oneOf": [ { "const": 0, "description": "Value A", "title": "A", }, { "const": 1, "description": "Value B", "title": "B", }, ], "title": "foxglove.ExampleEnum", }, "field_enum_array": { "description": "An enum array field", "items": { "description": "An enum array field", "oneOf": [ { "const": 0, "description": "Value A", "title": "A", }, { "const": 1, "description": "Value B", "title": "B", }, ], "title": "foxglove.ExampleEnum", }, "type": "array", }, "field_float64": { "description": "float64 field", "type": "number", }, "field_float64_array": { "description": "float64 array field", "items": { "type": "number", }, "type": "array", }, "field_float64_fixed_array": { "description": "float64 fixed-length array field", "items": { "type": "number", }, "maxItems": 3, "minItems": 3, "type": "array", }, "field_nested": { "description": "A nested field", "properties": { "field_enum": { "description": "An enum field", "minimum": 0, "type": "integer", }, }, "title": "foxglove.NestedMessage", "type": "object", }, "field_nested_array": { "description": "A nested array field With a very long description", "items": { "description": "An example nested message", "properties": { "field_enum": { "description": "An enum field", "minimum": 0, "type": "integer", }, }, "title": "foxglove.NestedMessage", "type": "object", }, "type": "array", }, "field_string": { "description": "string field", "type": "string", }, "field_string_array": { "description": "string array field", "items": { "type": "string", }, "type": "array", }, "field_string_fixed_array": { "description": "string fixed-length array field", "items": { "type": "string", }, "maxItems": 3, "minItems": 3, "type": "array", }, "field_time": { "description": "time field", "properties": { "nsec": { "maximum": 999999999, "minimum": 0, "type": "integer", }, "sec": { "minimum": 0, "type": "integer", }, }, "title": "time", "type": "object", }, "field_time_array": { "description": "time array field", "items": { "properties": { "nsec": { "maximum": 999999999, "minimum": 0, "type": "integer", }, "sec": { "minimum": 0, "type": "integer", }, }, "title": "time", "type": "object", }, "type": "array", }, "field_time_fixed_array": { "description": "time fixed-length array field", "items": { "properties": { "nsec": { "maximum": 999999999, "minimum": 0, "type": "integer", }, "sec": { "minimum": 0, "type": "integer", }, }, "title": "time", "type": "object", }, "maxItems": 3, "minItems": 3, "type": "array", }, "field_uint32": { "description": "uint32 field", "minimum": 0, "type": "integer", }, "field_uint32_array": { "description": "uint32 array field", "items": { "minimum": 0, "type": "integer", }, "type": "array", }, "field_uint32_fixed_array": { "description": "uint32 fixed-length array field", "items": { "minimum": 0, "type": "integer", }, "maxItems": 3, "minItems": 3, "type": "array", }, }, "title": "foxglove.ExampleMessage", "type": "object", } `); }); it.each(Object.values(foxgloveMessageSchemas))( "generates parseable JSON Schema for $name", (schema) => { const ajv = new Ajv(); expect(() => ajv.compile(generateJsonSchema(schema))).not.toThrow(); }, ); });