diff --git a/parser.go b/parser.go index 4eeafbe..ba7dc48 100644 --- a/parser.go +++ b/parser.go @@ -52,7 +52,7 @@ func (p *Parser) GetPayload() (*PayloadSection, error) { // Validate the payload if automatic validation is enabled if p.opts.withAutomaticValidation { if err := p.payload.Validate(); err != nil { - return nil, err + return nil, errors.Wrap(err, "payload validation failed") } } @@ -102,17 +102,17 @@ func parseOcmfMessageFromString(data string) (*PayloadSection, *Signature, error return nil, nil, ErrInvalidFormat } - payloadSection := &PayloadSection{} - err := json.Unmarshal([]byte(splitData[0]), payloadSection) + payloadSection := PayloadSection{} + err := json.Unmarshal([]byte(splitData[0]), &payloadSection) if err != nil { - return nil, nil, err + return nil, nil, errors.Wrap(err, "failed to unmarshal payload") } - signature := &Signature{} - err = json.Unmarshal([]byte(splitData[1]), signature) + signature := Signature{} + err = json.Unmarshal([]byte(splitData[1]), &signature) if err != nil { - return nil, nil, err + return nil, nil, errors.Wrap(err, "failed to unmarshal signature") } - return payloadSection, signature, nil + return &payloadSection, &signature, nil } diff --git a/validation.go b/validation.go index bb9bd02..b2506a3 100644 --- a/validation.go +++ b/validation.go @@ -71,7 +71,7 @@ func currentTypeValidator(fl validator.FieldLevel) bool { return isValidCurrentType(CurrentType(fl.Field().String())) } -var iso8601WithMillisRegex = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2},\d{3}[+-]\d{4}$`) +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)$`) func iso8601WithMillisValidator(fl validator.FieldLevel) bool { return iso8601WithMillisRegex.MatchString(fl.Field().String())