diff --git a/internal/converter/testdata/wellknown.go b/internal/converter/testdata/wellknown.go index 92f23a8f..1fc75eb8 100644 --- a/internal/converter/testdata/wellknown.go +++ b/internal/converter/testdata/wellknown.go @@ -28,9 +28,10 @@ const WellKnown = `{ "type": "array" }, "duration": { - "additionalProperties": true, + "pattern": "^([0-9]+\\.?[0-9]*|\\.[0-9]+)s$", "type": "string", - "description": "This is a duration:" + "description": "This is a duration:", + "format": "regex" } }, "additionalProperties": true, diff --git a/internal/converter/types.go b/internal/converter/types.go index 7d369570..dc2cddfe 100644 --- a/internal/converter/types.go +++ b/internal/converter/types.go @@ -181,6 +181,11 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes case descriptor.FieldDescriptorProto_TYPE_GROUP, descriptor.FieldDescriptorProto_TYPE_MESSAGE: switch desc.GetTypeName() { + // Make sure that durations match a particular string pattern (eg 3.4s): + case ".google.protobuf.Duration": + jsonSchemaType.Type = gojsonschema.TYPE_STRING + jsonSchemaType.Format = "regex" + jsonSchemaType.Pattern = `^([0-9]+\.?[0-9]*|\.[0-9]+)s$` case ".google.protobuf.Timestamp": jsonSchemaType.Type = gojsonschema.TYPE_STRING jsonSchemaType.Format = "date-time"