Skip to content

Commit

Permalink
Fixed time regex and validation to be up to spec
Browse files Browse the repository at this point in the history
  • Loading branch information
xBlaz3kx committed Dec 13, 2024
1 parent d54ba33 commit 4922969
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *builderTestSuite) TestBuilder_Valid() {
WithIdentificationStatus(true).
WithIdentificationType(string(RfidNone)).
AddReading(Reading{
Time: "2018-07-24T13:22:04,000+0200",
Time: "2018-07-24T13:22:04,000+0200 S",
ReadingValue: 123,
ReadingUnit: string(UnitskWh),
Status: string(MeterOk),
Expand All @@ -56,7 +56,7 @@ func (s *builderTestSuite) TestBuilder_Valid() {
s.Equal(true, builder.payload.IdentificationStatus)
s.Equal(string(RfidNone), builder.payload.IdentificationType)
s.Len(builder.payload.Readings, 1)
s.Equal("2018-07-24T13:22:04,000+0200", builder.payload.Readings[0].Time)
s.Equal("2018-07-24T13:22:04,000+0200 S", builder.payload.Readings[0].Time)
s.Equal(float64(123), builder.payload.Readings[0].ReadingValue)
s.Equal(string(UnitskWh), builder.payload.Readings[0].ReadingUnit)
s.Equal(string(MeterOk), builder.payload.Readings[0].Status)
Expand Down
2 changes: 1 addition & 1 deletion validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func currentTypeValidator(fl validator.FieldLevel) bool {
return isValidCurrentType(CurrentType(fl.Field().String()))
}

var iso8601WithMillisRegex = regexp.MustCompile(`^(?:19|20)\d{2}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:Z|-0[1-9]|-1\d|-2[0-3]|-00:?(?:0[1-9]|[1-5]\d)|\+[01]\d|\+2[0-3])(?:|:?[0-5]\d)$`)
var iso8601WithMillisRegex = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2},\d{3}[+-]\d{4} [S|U|I|R]$`)

func iso8601WithMillisValidator(fl validator.FieldLevel) bool {
return iso8601WithMillisRegex.MatchString(fl.Field().String())
Expand Down

0 comments on commit 4922969

Please sign in to comment.