From cd4ff2f197c77b367ad30891c21d4ba94ff17600 Mon Sep 17 00:00:00 2001 From: Prawn Date: Tue, 20 Jul 2021 20:21:26 +1200 Subject: [PATCH] Format and pattern (regex) for duration (#67) * Format and pattern (regex) for duration * We only need seconds (not other time units) Co-authored-by: Chrusty <> Co-authored-by: chris --- internal/converter/testdata/wellknown.go | 5 +++-- internal/converter/types.go | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) 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"