forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
381 lines (315 loc) · 9.15 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
package quickfix
import (
"github.com/quickfixgo/quickfix/datadictionary"
)
type validator interface {
Validate(Message) MessageRejectError
}
type validatorSettings struct {
CheckFieldsOutOfOrder bool
}
//Default configuration for message validation.
//See http://www.quickfixengine.org/quickfix/doc/html/configuration.html.
var defaultValidatorSettings = validatorSettings{
CheckFieldsOutOfOrder: true,
}
type fixValidator struct {
dataDictionary *datadictionary.DataDictionary
settings validatorSettings
}
type fixtValidator struct {
transportDataDictionary *datadictionary.DataDictionary
appDataDictionary *datadictionary.DataDictionary
settings validatorSettings
}
//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(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 err := validateFields(d, d, msgType, msg); err != nil {
return err
}
if err := validateWalk(d, d, 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 err := validateWalk(transportDD, appDD, msgType, msg); err != nil {
return err
}
if err := validateFields(transportDD, appDD, msgType, msg); err != nil {
return err
}
return nil
}
func validateMsgType(d *datadictionary.DataDictionary, msgType string, msg Message) MessageRejectError {
if _, validMsgType := d.Messages[msgType]; validMsgType == false {
return InvalidMessageType()
}
return nil
}
func validateWalk(transportDD *datadictionary.DataDictionary, appDD *datadictionary.DataDictionary, 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 fieldDef, ok = messageDef.Fields[int(tag)]; !ok {
return TagNotDefinedForThisMessageType(tag)
}
if _, duplicate := iteratedTags[int(tag)]; duplicate {
return tagAppearsMoreThanOnce(tag)
}
iteratedTags.Add(int(tag))
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(msg 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, msgType string, message Message) MessageRejectError {
for _, field := range message.fields {
switch {
case field.tag.IsHeader():
if err := validateField(transportDD, transportDD.Header.Tags, field); err != nil {
return err
}
case field.tag.IsTrailer():
if err := validateField(transportDD, transportDD.Trailer.Tags, field); err != nil {
return err
}
default:
if err := validateField(appDD, appDD.Messages[msgType].Tags, field); err != nil {
return err
}
}
}
return nil
}
func validateField(d *datadictionary.DataDictionary, validFields datadictionary.TagSet, field TagValue) MessageRejectError {
if len(field.value) == 0 {
return TagSpecifiedWithoutAValue(field.tag)
}
if _, valid := d.FieldTypeByTag[int(field.tag)]; !valid {
return InvalidTagNumber(field.tag)
}
allowedValues := d.FieldTypeByTag[int(field.tag)].Enums
if len(allowedValues) != 0 {
if _, validValue := allowedValues[string(field.value)]; !validValue {
return ValueIsIncorrect(field.tag)
}
}
fieldType := d.FieldTypeByTag[int(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
}