-
Notifications
You must be signed in to change notification settings - Fork 291
/
validation.go
459 lines (387 loc) · 11.4 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact ask@quickfixengine.org if any conditions of this licensing
// are not clear to you.
package quickfix
import (
"github.com/quickfixgo/quickfix/datadictionary"
)
const (
UserDefinedTagMin int = 5000
)
// Validator validates a FIX message.
type Validator interface {
Validate(*Message) MessageRejectError
}
// ValidatorSettings describe validation behavior.
type ValidatorSettings struct {
CheckFieldsOutOfOrder bool
RejectInvalidMessage bool
AllowUnknownMessageFields bool
CheckUserDefinedFields bool
}
// Default configuration for message validation.
// See http://www.quickfixengine.org/quickfix/doc/html/configuration.html.
var defaultValidatorSettings = ValidatorSettings{
CheckFieldsOutOfOrder: true,
RejectInvalidMessage: true,
AllowUnknownMessageFields: false,
CheckUserDefinedFields: true,
}
type fixValidator struct {
dataDictionary *datadictionary.DataDictionary
settings ValidatorSettings
}
type fixtValidator struct {
transportDataDictionary *datadictionary.DataDictionary
appDataDictionary *datadictionary.DataDictionary
settings ValidatorSettings
}
// NewValidator creates a FIX message validator from the given data dictionaries.
func NewValidator(settings ValidatorSettings, appDataDictionary, transportDataDictionary *datadictionary.DataDictionary) Validator {
if transportDataDictionary != nil {
return &fixtValidator{
transportDataDictionary: transportDataDictionary,
appDataDictionary: appDataDictionary,
settings: settings,
}
}
return &fixValidator{
dataDictionary: appDataDictionary,
settings: settings,
}
}
// Validate tests the message against the provided data dictionary.
func (v *fixValidator) Validate(msg *Message) MessageRejectError {
if !msg.Header.Has(tagMsgType) {
return RequiredTagMissing(tagMsgType)
}
msgType, err := msg.Header.GetString(tagMsgType)
if err != nil {
return err
}
return validateFIX(v.dataDictionary, v.settings, msgType, msg)
}
// Validate tests the message against the provided transport and app data dictionaries.
// If the message is an admin message, it will be validated against the transport data dictionary.
func (v *fixtValidator) Validate(msg *Message) MessageRejectError {
if !msg.Header.Has(tagMsgType) {
return RequiredTagMissing(tagMsgType)
}
msgType, err := msg.Header.GetString(tagMsgType)
if err != nil {
return err
}
if isAdminMessageType([]byte(msgType)) {
return validateFIX(v.transportDataDictionary, v.settings, msgType, msg)
}
return validateFIXT(v.transportDataDictionary, v.appDataDictionary, v.settings, msgType, msg)
}
func validateFIX(d *datadictionary.DataDictionary, settings ValidatorSettings, msgType string, msg *Message) MessageRejectError {
if err := validateMsgType(d, msgType, msg); err != nil {
return err
}
if err := validateRequired(d, d, msgType, msg); err != nil {
return err
}
if settings.CheckFieldsOutOfOrder {
if err := validateOrder(msg); err != nil {
return err
}
}
if settings.RejectInvalidMessage {
if err := validateFields(d, d, settings, msgType, msg); err != nil {
return err
}
if err := validateWalk(d, d, settings, msgType, msg); err != nil {
return err
}
}
return nil
}
func validateFIXT(transportDD, appDD *datadictionary.DataDictionary, settings ValidatorSettings, msgType string, msg *Message) MessageRejectError {
if err := validateMsgType(appDD, msgType, msg); err != nil {
return err
}
if err := validateRequired(transportDD, appDD, msgType, msg); err != nil {
return err
}
if settings.CheckFieldsOutOfOrder {
if err := validateOrder(msg); err != nil {
return err
}
}
if settings.RejectInvalidMessage {
if err := validateFields(transportDD, appDD, settings, msgType, msg); err != nil {
return err
}
if err := validateWalk(transportDD, appDD, settings, msgType, msg); err != nil {
return err
}
}
return nil
}
func validateMsgType(d *datadictionary.DataDictionary, msgType string, _ *Message) MessageRejectError {
if _, validMsgType := d.Messages[msgType]; !validMsgType {
return InvalidMessageType()
}
return nil
}
func validateWalk(transportDD *datadictionary.DataDictionary, appDD *datadictionary.DataDictionary, settings ValidatorSettings, msgType string, msg *Message) MessageRejectError {
remainingFields := msg.fields
iteratedTags := make(datadictionary.TagSet)
var messageDef *datadictionary.MessageDef
var fieldDef *datadictionary.FieldDef
var err MessageRejectError
var ok bool
for len(remainingFields) > 0 {
field := remainingFields[0]
tag := field.tag
switch {
case tag.IsHeader():
messageDef = transportDD.Header
case tag.IsTrailer():
messageDef = transportDD.Trailer
default: // is body
messageDef = appDD.Messages[msgType]
}
if _, duplicate := iteratedTags[int(tag)]; duplicate {
return tagAppearsMoreThanOnce(tag)
}
iteratedTags.Add(int(tag))
if fieldDef, ok = messageDef.Fields[int(tag)]; !ok {
if !checkFieldNotDefined(settings, tag) {
return TagNotDefinedForThisMessageType(tag)
}
remainingFields = remainingFields[1:]
continue
}
if remainingFields, err = validateVisitField(fieldDef, remainingFields); err != nil {
return err
}
}
if len(remainingFields) != 0 {
return TagNotDefinedForThisMessageType(remainingFields[0].tag)
}
return nil
}
func validateVisitField(fieldDef *datadictionary.FieldDef, fields []TagValue) ([]TagValue, MessageRejectError) {
if fieldDef.IsGroup() {
var err MessageRejectError
if fields, err = validateVisitGroupField(fieldDef, fields); err != nil {
return nil, err
}
return fields, nil
}
return fields[1:], nil
}
func validateVisitGroupField(fieldDef *datadictionary.FieldDef, fieldStack []TagValue) ([]TagValue, MessageRejectError) {
numInGroupTag := fieldStack[0].tag
var numInGroup FIXInt
if err := numInGroup.Read(fieldStack[0].value); err != nil {
return nil, IncorrectDataFormatForValue(numInGroupTag)
}
fieldStack = fieldStack[1:]
var childDefs []*datadictionary.FieldDef
groupCount := 0
for len(fieldStack) > 0 {
// Start of repeating group.
if int(fieldStack[0].tag) == fieldDef.Fields[0].Tag() {
childDefs = fieldDef.Fields
groupCount++
}
// Group complete.
if len(childDefs) == 0 {
break
}
if int(fieldStack[0].tag) == childDefs[0].Tag() {
var err MessageRejectError
if fieldStack, err = validateVisitField(childDefs[0], fieldStack); err != nil {
return fieldStack, err
}
} else {
if childDefs[0].Required() {
return fieldStack, RequiredTagMissing(Tag(childDefs[0].Tag()))
}
}
childDefs = childDefs[1:]
}
if groupCount != int(numInGroup) {
return fieldStack, incorrectNumInGroupCountForRepeatingGroup(numInGroupTag)
}
return fieldStack, nil
}
func validateOrder(msg *Message) MessageRejectError {
inHeader := true
inTrailer := false
for _, field := range msg.fields {
t := field.tag
switch {
case inHeader && t.IsHeader():
case inHeader && !t.IsHeader():
inHeader = false
case !inHeader && t.IsHeader():
return tagSpecifiedOutOfRequiredOrder(t)
case t.IsTrailer():
inTrailer = true
case inTrailer && !t.IsTrailer():
return tagSpecifiedOutOfRequiredOrder(t)
}
}
return nil
}
func validateRequired(transportDD *datadictionary.DataDictionary, appDD *datadictionary.DataDictionary, msgType string, message *Message) MessageRejectError {
if err := validateRequiredFieldMap(message, transportDD.Header.RequiredTags, message.Header.FieldMap); err != nil {
return err
}
if err := validateRequiredFieldMap(message, appDD.Messages[msgType].RequiredTags, message.Body.FieldMap); err != nil {
return err
}
if err := validateRequiredFieldMap(message, transportDD.Trailer.RequiredTags, message.Trailer.FieldMap); err != nil {
return err
}
return nil
}
func validateRequiredFieldMap(_ *Message, requiredTags map[int]struct{}, fieldMap FieldMap) MessageRejectError {
for required := range requiredTags {
requiredTag := Tag(required)
if !fieldMap.Has(requiredTag) {
return RequiredTagMissing(requiredTag)
}
}
return nil
}
func validateFields(transportDD *datadictionary.DataDictionary,
appDD *datadictionary.DataDictionary,
settings ValidatorSettings,
msgType string,
message *Message,
) MessageRejectError {
for _, field := range message.fields {
switch {
case field.tag.IsHeader():
if err := validateField(transportDD, settings, transportDD.Header.Tags, field); err != nil {
return err
}
case field.tag.IsTrailer():
if err := validateField(transportDD, settings, transportDD.Trailer.Tags, field); err != nil {
return err
}
default:
if err := validateField(appDD, settings, appDD.Messages[msgType].Tags, field); err != nil {
return err
}
}
}
return nil
}
func getFieldType(d *datadictionary.DataDictionary, field int) (*datadictionary.FieldType, bool) {
fieldType, isMessageField := d.FieldTypeByTag[field]
return fieldType, isMessageField
}
func checkFieldNotDefined(settings ValidatorSettings, field Tag) bool {
fail := false
if int(field) < UserDefinedTagMin {
fail = !settings.AllowUnknownMessageFields
} else {
fail = settings.CheckUserDefinedFields
}
return !fail
}
func validateField(d *datadictionary.DataDictionary,
settings ValidatorSettings,
_ datadictionary.TagSet,
field TagValue,
) MessageRejectError {
if len(field.value) == 0 {
return TagSpecifiedWithoutAValue(field.tag)
}
fieldType, isMessageField := getFieldType(d, int(field.tag))
if !isMessageField && !checkFieldNotDefined(settings, field.tag) {
return InvalidTagNumber(field.tag)
}
if !isMessageField {
return nil
}
allowedValues := d.FieldTypeByTag[int(field.tag)].Enums
if len(allowedValues) != 0 {
if _, validValue := allowedValues[string(field.value)]; !validValue {
return ValueIsIncorrect(field.tag)
}
}
var prototype FieldValue
switch fieldType.Type {
case "MULTIPLESTRINGVALUE", "MULTIPLEVALUESTRING":
fallthrough
case "MULTIPLECHARVALUE":
fallthrough
case "CHAR":
fallthrough
case "CURRENCY":
fallthrough
case "DATA":
fallthrough
case "MONTHYEAR":
fallthrough
case "LOCALMKTDATE", "DATE":
fallthrough
case "EXCHANGE":
fallthrough
case "LANGUAGE":
fallthrough
case "XMLDATA":
fallthrough
case "COUNTRY":
fallthrough
case "UTCTIMEONLY":
fallthrough
case "UTCDATEONLY", "UTCDATE":
fallthrough
case "TZTIMEONLY":
fallthrough
case "TZTIMESTAMP":
fallthrough
case "STRING":
prototype = new(FIXString)
case "BOOLEAN":
prototype = new(FIXBoolean)
case "LENGTH":
fallthrough
case "DAYOFMONTH":
fallthrough
case "NUMINGROUP":
fallthrough
case "SEQNUM":
fallthrough
case "INT":
prototype = new(FIXInt)
case "UTCTIMESTAMP", "TIME":
prototype = new(FIXUTCTimestamp)
case "QTY", "QUANTITY":
fallthrough
case "AMT":
fallthrough
case "PRICE":
fallthrough
case "PRICEOFFSET":
fallthrough
case "PERCENTAGE":
fallthrough
case "FLOAT":
prototype = new(FIXFloat)
}
if err := prototype.Read(field.value); err != nil {
return IncorrectDataFormatForValue(field.tag)
}
return nil
}